Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: RENAME TABLE supports renaming multiple tables and across dbs #19452

Merged
merged 7 commits into from
Jan 2, 2025
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 58 additions & 10 deletions sql-statements/sql-statement-rename-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ aliases: ['/docs-cn/dev/sql-statements/sql-statement-rename-table/','/docs-cn/de

# RENAME TABLE

`RENAME TABLE` 语句用于对已有表进行重命名
`RENAME TABLE` 语句用于重命名现有表,支持同时重命名多个表及跨数据库重命名
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

## 语法图

Expand All @@ -20,8 +20,6 @@ TableToTable ::=

## 示例

{{< copyable "sql" >}}

```sql
CREATE TABLE t1 (a int);
```
Expand All @@ -30,8 +28,6 @@ CREATE TABLE t1 (a int);
Query OK, 0 rows affected (0.12 sec)
```

{{< copyable "sql" >}}

```sql
SHOW TABLES;
```
Expand All @@ -42,11 +38,9 @@ SHOW TABLES;
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
1 row in set (0.00 sec)77
hfxsd marked this conversation as resolved.
Show resolved Hide resolved
```

{{< copyable "sql" >}}

```sql
RENAME TABLE t1 TO t2;
```
Expand All @@ -55,8 +49,6 @@ RENAME TABLE t1 TO t2;
Query OK, 0 rows affected (0.08 sec)
```

{{< copyable "sql" >}}

```sql
SHOW TABLES;
```
Expand All @@ -70,6 +62,62 @@ SHOW TABLES;
1 row in set (0.00 sec)
```

以下示例演示了如何跨数据库重命名多个表:
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

```sql
RENAME TABLE db1.t1 To db2.t2, db3.t3 To db4.t4;
```

```
Query OK, 0 rows affected (0.08 sec)
```

```sql
USE db1; SHOW TABLES;
```

```
Database changed
Empty set (0.00 sec)
```

```sql
USE db2; SHOW TABLES;
```

```
Database changed
+---------------+
| Tables_in_db2 |
+---------------+
| t2 |
+---------------+
1 row in set (0.00 sec)
```

```sql
USE db3; SHOW TABLES;
```

```
Database changed
Empty set (0.00 sec)
```

```sql
USE db4; SHOW TABLES;
```

```
Database changed
+---------------+
| Tables_in_db4 |
+---------------+
| t4 |
+---------------+
1 row in set (0.00 sec)
```

## MySQL 兼容性

`RENAME TABLE` 语句与 MySQL 完全兼容。如发现任何兼容性差异,请尝试 [TiDB 支持资源](/support.md)。
Expand Down
Loading