.rename table 和 .rename tables.rename table and .rename tables

更改现有表的名称。Changes the name of an existing table.

.rename tables 命令将数据库中多个表的名称作为单个事务进行更改。The .rename tables command changes the name of a number of tables in the database as a single transaction.

需要数据库管理员权限Requires Database admin permission.


.rename table OldName to NewName .rename table OldName to NewName

.rename tables NewName = OldName [ifexists][, ...] .rename tables NewName = OldName [ifexists][, ...]


  • OldName 是现有表的名称。OldName is the name of an existing table. 如果 OldName 不是现有表的名称,那么除非指定 ifexists(在这种情况下将忽略重命名命令的这一部分),否则会引发错误,并且整个命令会失败(不起作用)。An error is raised and the whole command fails (has no effect) if OldName does not name an existing table, unless ifexists is specified (in which case this part of the rename command is ignored).
  • NewName 是现有表的新名称,之前的名称为 OldName 。NewName is the new name of the existing table that used to be called OldName.
  • 如果指定了 ifexists,它会修改此命令的行为,以忽略重命名不存在的表的部分。If ifexists is specified, it modifies the behavior of the command to ignore renaming parts of non-existent tables.


此命令仅对范围内的数据库表起作用。This command operates on tables of the database in scope only. 无法使用群集或数据库名称限定表名。Table names cannot be qualified with cluster or database names.

此命令不会创建新表,也不会删除现有表。This command doesn't create new tables, nor does it remove existing tables. 此命令描述的转换必须确保数据库中的表数量保持不变。The transformation described by the command must be such that the number of tables in the database does not change.

只要遵守上述规则,此命令就支持交换表名或更复杂的排列。The command does support swapping table names, or more complex permutations, as long as they adhere to the rules above. 例如,将数据引入多个临时表,然后在单个事务中将它们与现有表交换。For example, ingest data into multiple staging tables, and then swap them with existing tables in a single transaction.


假设数据库中包含以下表:ABCA_TEMPImagine a database with the following tables: A, B, C, and A_TEMP. 以下命令将交换 AA_TEMP(现在 A_TEMPA 表的名称相互对调)、将 B 重命名为 NEWB 并保持 C 不变。The following command will swap A and A_TEMP (so that the A_TEMP table will now be called A, and the other way around), rename B to NEWB, and keep C as-is.

.rename tables A=A_TEMP, NEWB=B, A_TEMP=A

命令的顺序如下:The following sequence of commands:

  1. 创建新的临时表Creates a new temporary table
  2. 将现有或不存在的表替换为新表Replaces an existing or non-existing table with the new table
// Drop the temporary table if it exists
.drop table TempTable ifexists

// Create a new table
.set TempTable <| ...

// Swap the two tables
.rename tables TempTable=Table ifexists, Table=TempTable

// Drop the temporary table (which used to be Table) if it exists
.drop table TempTable ifexists