在删除自增长列主键时,需要先改变自增长属性,然后才能删除主键。
-
mysql> show create table t1\\G
-
*************************** 1. row ***************************
-
Table: t1
-
Create Table: CREATE TABLE `t1` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`name` varchar(50) DEFAULT NULL,
-
PRIMARY KEY (`id`)
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1
-
1 row in set (0.01 sec)
表t1的id列有自增长属性,且为主键
-
mysql> alter table t1 drop primary key;
-
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
直接删除主键报错,需要先改变表的定义
-
mysql> alter table t1 change id id int(10);
-
Query OK, 0 rows affected (0.13 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
-
-
mysql> show create table t1\\G
-
*************************** 1. row ***************************
-
Table: t1
-
Create Table: CREATE TABLE `t1` (
-
`id` int(10) NOT NULL DEFAULT \'0\',
-
`name` varchar(50) DEFAULT NULL,
-
PRIMARY KEY (`id`)
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1
-
1 row in set (0.00 sec)
然后在删除自增长属性列上的主键
-
mysql> alter table t1 drop primary key;
-
Query OK, 0 rows affected (0.06 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
-
-
mysql> show create table t1\\G
-
*************************** 1. row ***************************
-
Table: t1
-
Create Table: CREATE TABLE `t1` (
-
`id` int(10) NOT NULL DEFAULT \'0\',
-
`name` varchar(50) DEFAULT NULL
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1
-
1 row in set (0.00 sec)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12219480/viewspace-1732531/
,如需转载,请注明出处,否则将追究法律责任。