File tree Expand file tree Collapse file tree 2 files changed +49
-1
lines changed
docs/cn/sql-reference/10-sql-commands/00-ddl/01-table Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change 1- Translation initialization: 2025-10-10T04:22:52.845761
1+ Translation initialization: 2025-10-10T09:31:23.332884
Original file line number Diff line number Diff line change 1+ ---
2+ title : ALTER TABLE SWAP WITH
3+ sidebar_position : 5
4+ ---
5+ import FunctionDescription from '@site/src /components/FunctionDescription';
6+
7+ <FunctionDescription description =" 引入或更新于:v1.2.821 " />
8+
9+ 在单个事务(Transaction)中原子地交换两个表的所有元数据和数据。此操作会交换表结构(schema),包括所有列、约束和数据,从而有效地使每个表承担另一个表的身份。
10+
11+ ## 语法
12+
13+ ``` sql
14+ ALTER TABLE [ IF EXISTS ] < source_table_name> SWAP WITH < target_table_name>
15+ ```
16+
17+ | 参数 | 说明 |
18+ | ----------------------| ------------------------------------------------|
19+ | ` source_table_name ` | 要交换的第一个表的名称 |
20+ | ` target_table_name ` | 要与之交换的第二个表的名称 |
21+
22+ ## 使用说明
23+
24+ - ** 引擎支持** :仅适用于 Fuse 引擎表。不支持外部表、系统表和其他非 Fuse 表。
25+ - ** 表类型** :临时表不能与永久表或瞬时表交换。
26+ - ** 权限** :当前角色必须是两个表的所有者才能执行交换操作。
27+ - ** 数据库范围** :两个表必须在同一个数据库中。不支持跨数据库交换。
28+ - ** 原子性** :交换操作是原子性的。要么两个表都成功交换,要么两个表都不变。
29+ - ** 数据保留** :交换期间会保留所有数据和元数据。不会丢失或修改任何数据。
30+
31+ ## 示例
32+
33+ ``` sql
34+ -- 创建两个具有不同表结构的表
35+ CREATE OR REPLACE TABLE t1 (a1 INT , a2 VARCHAR , a3 DATE );
36+ CREATE OR REPLACE TABLE t2 (b1 VARCHAR );
37+
38+ -- 交换前检查表结构
39+ DESC t1;
40+ DESC t2;
41+
42+ -- 交换表
43+ ALTER TABLE t1 SWAP WITH t2;
44+
45+ -- 交换后,t1 拥有 t2 的表结构,t2 拥有 t1 的表结构
46+ DESC t1;
47+ DESC t2;
48+ ```
You can’t perform that action at this time.
0 commit comments