Skip to content

Commit d3f97df

Browse files
authored
sql: RENAME TABLE supports renaming multiple tables and across dbs (#19871)
1 parent 1bb072b commit d3f97df

File tree

1 file changed

+120
-5
lines changed

1 file changed

+120
-5
lines changed

sql-statements/sql-statement-rename-table.md

Lines changed: 120 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ aliases: ['/docs/dev/sql-statements/sql-statement-rename-table/','/docs/dev/refe
66

77
# RENAME TABLE
88

9-
This statement renames an existing table to a new name.
9+
This statement is used to rename existing tables and views, supporting renaming multiple tables at once and renaming across databases.
1010

1111
## Synopsis
1212

@@ -21,21 +21,39 @@ TableToTable ::=
2121
## Examples
2222

2323
```sql
24-
mysql> CREATE TABLE t1 (a int);
24+
CREATE TABLE t1 (a int);
25+
```
26+
27+
```
2528
Query OK, 0 rows affected (0.12 sec)
29+
```
30+
31+
```sql
32+
SHOW TABLES;
33+
```
2634

27-
mysql> SHOW TABLES;
35+
```
2836
+----------------+
2937
| Tables_in_test |
3038
+----------------+
3139
| t1 |
3240
+----------------+
3341
1 row in set (0.00 sec)
42+
```
43+
44+
```sql
45+
RENAME TABLE t1 TO t2;
46+
```
3447

35-
mysql> RENAME TABLE t1 TO t2;
48+
```
3649
Query OK, 0 rows affected (0.08 sec)
50+
```
51+
52+
```sql
53+
SHOW TABLES;
54+
```
3755

38-
mysql> SHOW TABLES;
56+
```
3957
+----------------+
4058
| Tables_in_test |
4159
+----------------+
@@ -44,6 +62,103 @@ mysql> SHOW TABLES;
4462
1 row in set (0.00 sec)
4563
```
4664

65+
The following example demonstrates how to rename multiple tables across databases, assuming that the databases `db1`, `db2`, `db3`, and `db4` already exist, and that the tables `db1.t1` and `db3.t3` already exist:
66+
67+
```sql
68+
RENAME TABLE db1.t1 To db2.t2, db3.t3 To db4.t4;
69+
```
70+
71+
```
72+
Query OK, 0 rows affected (0.08 sec)
73+
```
74+
75+
```sql
76+
USE db1; SHOW TABLES;
77+
```
78+
79+
```
80+
Database changed
81+
Empty set (0.00 sec)
82+
```
83+
84+
```sql
85+
USE db2; SHOW TABLES;
86+
```
87+
88+
```
89+
Database changed
90+
+---------------+
91+
| Tables_in_db2 |
92+
+---------------+
93+
| t2 |
94+
+---------------+
95+
1 row in set (0.00 sec)
96+
```
97+
98+
```sql
99+
USE db3; SHOW TABLES;
100+
```
101+
102+
```
103+
Database changed
104+
Empty set (0.00 sec)
105+
```
106+
107+
```sql
108+
USE db4; SHOW TABLES;
109+
```
110+
111+
```
112+
Database changed
113+
+---------------+
114+
| Tables_in_db4 |
115+
+---------------+
116+
| t4 |
117+
+---------------+
118+
1 row in set (0.00 sec)
119+
```
120+
121+
The atomic rename can be used to swap out a table without having any moment in which the table does not exist.
122+
123+
```sql
124+
CREATE TABLE t1(id int PRIMARY KEY);
125+
```
126+
127+
```
128+
Query OK, 0 rows affected (0.04 sec)
129+
```
130+
131+
```sql
132+
CREATE TABLE t1_new(id int PRIMARY KEY, n CHAR(0));
133+
````
134+
135+
```
136+
Query OK, 0 rows affected (0.04 sec)
137+
```
138+
139+
```sql
140+
RENAME TABLE t1 TO t1_old, t1_new TO t1;
141+
```
142+
143+
```
144+
Query OK, 0 rows affected (0.07 sec)
145+
```
146+
147+
```sql
148+
SHOW CREATE TABLE t1\G
149+
```
150+
151+
```
152+
*************************** 1. row ***************************
153+
Table: t1
154+
Create Table: CREATE TABLE `t1` (
155+
`id` int NOT NULL,
156+
`n` char(0) DEFAULT NULL,
157+
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
158+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
159+
1 row in set (0.00 sec)
160+
```
161+
47162
## MySQL compatibility
48163

49164
The `RENAME TABLE` statement in TiDB is fully compatible with MySQL. If you find any compatibility differences, [report a bug](https://docs.pingcap.com/tidb/stable/support).

0 commit comments

Comments
 (0)