您好,登錄后才能下訂單哦!
1、mysql刪除表中的主鍵時報錯,如下代碼所示:
mysql> alter table student drop primary key; ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
2、問題原因:
查看student表的類型時,發現主鍵列中有auto_increment(遞增)類型選項。如要刪除表中的主鍵,需要先刪除auto_increment類型。代碼如下所示:
mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | 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 | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.02 sec)
3、刪除student表中的主鍵列上面的auto_increment類型。代碼如下所示:
mysql> alter table student change id id int; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0
提示:alter table student change id id int;命令修改student表中列的類型,auto_increment類型自然就會被刪除。
4、查看student表的類型,發現auto_increment類型已被刪除。代碼如下所示:
mysql> desc student; +-------+-------------+------+-----+---------+-------+ | 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 | | +-------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
5、刪除student中的主鍵。代碼如下所示:
mysql> alter table student drop primary key; Query OK, 0 rows affected (0.10 sec) Records: 0 Duplicates: 0 Warnings: 0
6、最后查看student表的類型,發現表中的主鍵已被刪除。代碼如下:
mysql> desc student; +-------+-------------+------+-----+---------+-------+ | 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 | | +-------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。