以原子方式交换两个表或字典的名称。
此任务也可以通过使用临时名称的 RENAME 查询来完成,但在这种情况下,该操作不具有原子性。
语法
EXCHANGE TABLES|DICTIONARIES [db0.]name_A AND [db1.]name_B [ON CLUSTER cluster]
互换两个表的名称。
语法
EXCHANGE TABLES [db0.]table_A AND [db1.]table_B [ON CLUSTER cluster]
你可以在单个查询中用逗号分隔多个表对,一次交换多对表。
交换多个表对时,这些交换操作会按顺序执行,而非原子执行。如果操作过程中发生错误,某些表对可能已经交换,而其他表对则尚未交换。
示例
-- 创建表
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;
-- 现在表 '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 DICTIONARIES [db0.]dict_A AND [db1.]dict_B [ON CLUSTER cluster]
另请参阅