You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
10
11
11
## Synopsis
12
12
@@ -21,21 +21,39 @@ TableToTable ::=
21
21
## Examples
22
22
23
23
```sql
24
-
mysql> CREATE TABLE t1 (a int);
24
+
CREATETABLEt1 (a int);
25
+
```
26
+
27
+
```
25
28
Query OK, 0 rows affected (0.12 sec)
29
+
```
30
+
31
+
```sql
32
+
SHOW TABLES;
33
+
```
26
34
27
-
mysql> SHOW TABLES;
35
+
```
28
36
+----------------+
29
37
| Tables_in_test |
30
38
+----------------+
31
39
| t1 |
32
40
+----------------+
33
41
1 row in set (0.00 sec)
42
+
```
43
+
44
+
```sql
45
+
RENAME TABLE t1 TO t2;
46
+
```
34
47
35
-
mysql> RENAME TABLE t1 TO t2;
48
+
```
36
49
Query OK, 0 rows affected (0.08 sec)
50
+
```
51
+
52
+
```sql
53
+
SHOW TABLES;
54
+
```
37
55
38
-
mysql> SHOW TABLES;
56
+
```
39
57
+----------------+
40
58
| Tables_in_test |
41
59
+----------------+
@@ -44,6 +62,103 @@ mysql> SHOW TABLES;
44
62
1 row in set (0.00 sec)
45
63
```
46
64
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.
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