RENAME INDEX

ALTER TABLE .. RENAME INDEX 语句用于对已有索引进行重命名。这在 TiDB 中是即时操作的,仅需更改元数据。

语法图

AlterTableStmt

RENAME INDEX - 图1

RenameIndexSpec

RENAME INDEX - 图2

  1. AlterTableStmt
  2. ::= 'ALTER' 'IGNORE'? 'TABLE' TableName RenameIndexSpec ( ',' RenameIndexSpec )*
  3. RenameIndexSpec
  4. ::= 'RENAME' ( 'KEY' | 'INDEX' ) Identifier 'TO' Identifier

示例

  1. CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, c1 INT NOT NULL, INDEX col1 (c1));
  1. Query OK, 0 rows affected (0.11 sec)
  1. SHOW CREATE TABLE t1;
  1. *************************** 1. row ***************************
  2. Table: t1
  3. Create Table: CREATE TABLE `t1` (
  4. `id` int(11) NOT NULL AUTO_INCREMENT,
  5. `c1` int(11) NOT NULL,
  6. PRIMARY KEY (`id`),
  7. KEY `col1` (`c1`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
  9. 1 row in set (0.00 sec)
  1. ALTER TABLE t1 RENAME INDEX col1 TO c1;
  1. Query OK, 0 rows affected (0.09 sec)
  1. SHOW CREATE TABLE t1;
  1. *************************** 1. row ***************************
  2. Table: t1
  3. Create Table: CREATE TABLE `t1` (
  4. `id` int(11) NOT NULL AUTO_INCREMENT,
  5. `c1` int(11) NOT NULL,
  6. PRIMARY KEY (`id`),
  7. KEY `c1` (`c1`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
  9. 1 row in set (0.00 sec)

MySQL 兼容性

RENAME INDEX 语句与 MySQL 完全兼容。如发现任何兼容性差异,请在 GitHub 上提交 issue

另请参阅