跳转到主要内容
以原子方式交换两个表或字典的名称。 此任务也可以通过使用临时名称的 RENAME 查询来完成,但在这种情况下,该操作不具有原子性。
EXCHANGE 查询仅受 AtomicShared 数据库引擎支持。
语法
EXCHANGE TABLES|DICTIONARIES [db0.]name_A AND [db1.]name_B [ON CLUSTER cluster]

EXCHANGE TABLES

互换两个表的名称。 语法
EXCHANGE TABLES [db0.]table_A AND [db1.]table_B [ON CLUSTER cluster]

EXCHANGE 多个表

你可以在单个查询中用逗号分隔多个表对,一次交换多对表。
交换多个表对时,这些交换操作会按顺序执行,而非原子执行。如果操作过程中发生错误,某些表对可能已经交换,而其他表对则尚未交换。
示例
Query
-- 创建表
CREATE TABLE a (a UInt8) ENGINE=Memory;
CREATE TABLE b (b UInt8) ENGINE=Memory;
CREATE TABLE c (c UInt8) ENGINE=Memory;
CREATE TABLE d (d UInt8) ENGINE=Memory;

-- 在一个查询中交换两对表
EXCHANGE TABLES a AND b, c AND d;

SHOW TABLE a;
SHOW TABLE b;
SHOW TABLE c;
SHOW TABLE d;
Response
-- 现在表 'a' 具有 'b' 的结构,表 'b' 具有 'a' 的结构
┌─statement──────────────┐
CREATE TABLE default.a↴│
│↳(                     ↴│
│↳    `b` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘
┌─statement──────────────┐
CREATE TABLE default.b↴│
│↳(                     ↴│
│↳    `a` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘

-- 现在表 'c' 具有 'd' 的结构,表 'd' 具有 'c' 的结构
┌─statement──────────────┐
CREATE TABLE default.c↴│
│↳(                     ↴│
│↳    `d` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘
┌─statement──────────────┐
CREATE TABLE default.d↴│
│↳(                     ↴│
│↳    `c` UInt8         ↴│
│↳)                     ↴│
│↳ENGINE = Memory        │
└────────────────────────┘

EXCHANGE 字典

交换两个字典的名称。 语法
EXCHANGE DICTIONARIES [db0.]dict_A AND [db1.]dict_B [ON CLUSTER cluster]
另请参阅
最后修改于 2026年6月10日