简体   繁体   中英

I need to truncate a table then insert new register, but users cant see this

这是一个很难理解的问题,但是,我需要截断一个表,同时插入新的寄存器,但是在此过程中,网站访问者看不到网站内容没有任何内容,我该如何在SQL中像--single-transation在新的缓存出现之前有“缓存的缓存”之类的东西吗?

Use rename table to switch 2 tables.

RENAME TABLE, unlike ALTER TABLE, can rename multiple tables within a single statement:

  • prepare your new table the_table_next (can be a long operation)
  • RENAME TABLE the_table TO the_table_prev, the_table_next To the_table; (very fast operation)
  • drop/truncate table the_table_prev

When you execute RENAME TABLE, you cannot have any locked tables or active transactions. With that condition satisfied, the rename operation is done atomically; no other session can access any of the tables while the rename is in progress.

If any errors occur during a RENAME TABLE, the statement fails and no changes are made.

Note :

with alter table rename you need use 2 command ==> 2 different transaction (DDL instruction)

with rename table => single transaction

If you can truncate the table then it's OK to assume that there are no key restrictions on that table. If that is so I would:

  • 1) Supposing that the original table is Table1 I would create Table2 with the exact same structure.
  • 2) Then I would populate Table2 with whatever I need
  • 3) After that, in one transaction, I would change the name of Table1 to Table3 and the name of Table2 to Table1.
  • 4) Finally I would whatever I need with Table3 (which was originally Table1).

In MySQL you can rename tables with:

ALTER TABLE table_name
   RENAME TO new_table_name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM