我需要截断 table 然后插入新寄存器,但用户看不到这个

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

有点难理解,但是,有一个table我需要截断,同时插入新的寄存器,但是在这个过程中,网站访问者看不到网站内容,我怎么能在 SQL 中做到这一点,比如 --single-transation 在新版本到来之前有类似 "cached registers" 的东西?

使用rename table切换2tables.

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

  • 准备好你的新 table the_table_next (可能是一个很长的操作)
  • RENAME TABLE the_table TO the_table_prev, the_table_next To the_table; (运算速度非常快)
  • 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.

注:

对于 alter table rename 你需要使用 2 个命令 ==> 2 个不同的事务(DDL 指令)

rename table => 单笔交易

如果您可以 截断table,那么可以假设table 没有密钥限制。如果是这样我会:

  • 1) 假设原始 table 是 Table1,我将创建具有完全相同结构的 Table2。
  • 2) 然后我会用我需要的任何东西填充 Table2
  • 3) 之后,在一次事务中,我将Table1 的名称更改为Table3,将Table2 的名称更改为Table1。
  • 4) 最后我会用 Table3(原来是 Table1)做任何我需要的。

在 MySQL 中,您可以将 table 重命名为:

ALTER TABLE table_name
   RENAME TO new_table_name;