Allow changing a column to be an AUTO_INCREMENT
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Feature Request
Changing an integer column to be an `AUTO_INCREMENT` after the table is created isn't supported by TiDB, but is supported by MySQL.
## TiDB
```
sql> CREATE TABLE t1 (id INT PRIMARY KEY);
Query OK, 0 rows affected (0.1691 sec)
sql> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(11) NOT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
1 row in set (0.0014 sec)
sql> ALTER TABLE t1 MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT;
ERROR: 8200 (HY000): Unsupported modify column: can't set auto_increment
sql> SELECT tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v6.5.0-alpha
Edition: Community
Git Commit Hash: bdcb85031e07d5a75f4e7742dcf4f7fed7dbdfe3
Git Branch: heads/refs/tags/v6.5.0-alpha
UTC Build Time: 2022-11-14 14:33:27
GoVersion: go1.19.2
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv
1 row in set (0.0014 sec)
````
And for `AUTO_RANDOM`:
```
sql> ALTER TABLE t1 MODIFY COLUMN `id` int(11) NOT NULL AUTO_RANDOM;
ERROR: 8216 (HY000): Invalid auto random: auto_random can only be converted from auto_increment clustered primary key
```
## MySQL 8.0
```
sql> CREATE TABLE t1 (id INT PRIMARY KEY);
Query OK, 0 rows affected (0.0524 sec)
sql> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.0027 sec)
sql> ALTER TABLE t1 MODIFY COLUMN `id` int NOT NULL AUTO_INCREMENT;
Query OK, 0 rows affected (0.1036 sec)
Records: 0 Duplicates: 0 Warnings: 0
sql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.0.31 |
+-----------+
1 row in set (0.0007 sec)
```
## PHPMyAdmin
As an example about why users would need/want this:
A backup made by phpMyAdmin 4.9.7 has this:
- A `CREATE TABLE` statement for a table. The table doesn't have indexes.
- A number of `INSERT INTO` statements to load the data
- An `ALTER TABLE` statement to add multiple indexes and set a primary key
- An `ALTER TABLE` statement to change the `id` column to be `AUTO_INCREMENT`
This way of re-creating a table with data was probably done to optimize for MySQL with the MyISAM storage engine and/or very old InnoDB versions. However this backup can still be loaded into MySQL 8.0 today.
On TiDB all statements work fine, except for the very last one.
There are probably other situations where changing a column to be an `AUTO_INCREMENT` or `AUTO_RANDOM` after the initial creation.
A similar case where changing a table after creation can be a problem is #38453
Contributor guide
Assessment
This issue has not been assessed yet.