mysql无法删除表中的主键
1、mysql删除表中的主键时报错,如下代码所示:
mysql>altertablestudentdropprimarykey;ERROR1075(42000):Incorrecttabledefinition;therecanbeonlyoneautocolumnanditmustbedefinedasakey
2、问题原因:
查看student表的类型时,发现主键列中有auto_increment(递增)类型选项。如要删除表中的主键,需要先删除auto_increment类型。代码如下所示:
mysql>descstudent;+-------+-------------+------+-----+---------+----------------+|Field|Type|Null|Key|Default|Extra|+-------+-------------+------+-----+---------+----------------+|id|int(11)|NO|PRI|NULL|auto_increment||name|char(20)|NO|MUL|NULL|||age|tinyint(2)|NO|MUL|0|||dept|varchar(16)|YES||NULL||+-------+-------------+------+-----+---------+----------------+4rowsinset(0.02sec)
3、删除student表中的主键列上面的auto_increment类型。代码如下所示:
mysql>altertablestudentchangeididint;QueryOK,0rowsaffected(0.04sec)Records:0Duplicates:0Warnings:0
提示:alter table student change id id int;命令修改student表中列的类型,auto_increment类型自然就会被删除。
4、查看student表的类型,发现auto_increment类型已被删除。代码如下所示:
mysql>descstudent;+-------+-------------+------+-----+---------+-------+|Field|Type|Null|Key|Default|Extra|+-------+-------------+------+-----+---------+-------+|id|int(11)|NO|PRI|0|||name|char(20)|NO|MUL|NULL|||age|tinyint(2)|NO|MUL|0|||dept|varchar(16)|YES||NULL||+-------+-------------+------+-----+---------+-------+4rowsinset(0.01sec)
5、删除student中的主键。代码如下所示:
mysql>altertablestudentdropprimarykey;QueryOK,0rowsaffected(0.10sec)Records:0Duplicates:0Warnings:0
6、最后查看student表的类型,发现表中的主键已被删除。代码如下:
mysql>descstudent;+-------+-------------+------+-----+---------+-------+|Field|Type|Null|Key|Default|Extra|+-------+-------------+------+-----+---------+-------+|id|int(11)|NO||0|||name|char(20)|NO|MUL|NULL|||age|tinyint(2)|NO|MUL|0|||dept|varchar(16)|YES||NULL||+-------+-------------+------+-----+---------+-------+4rowsinset(0.00sec)
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。