如果我嘗試從 AUTO_INCREMENT 欄位中刪除 PRIMARY KEY 約束,會發生什麼?
眾所周知,AUTO_INCREMENT 欄位也必須具有 PRIMARY KEY 約束,因此,當我們嘗試從 AUTO_INCREMENT 欄位中刪除 PRIMARY KEY 約束時,MySQL 會返回一條有關錯誤表定義的錯誤訊息。以下示例將對此進行演示 −
示例
假設我們有一個“Accounts”表,其描述如下 −
mysql> Describe accounts; +--------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+----------------+ | Sr | int(10) | NO | PRI | NULL | auto_increment | | Name | varchar(20) | YES | | NULL | | | amount | int(15) | YES | | NULL | | +--------+-------------+------+-----+---------+----------------+ 3 rows in set (0.10 sec)
它有一個帶有 AUTO_INCREMENT 和 PRIMARY KEY 定義的欄位“Sr”。現在,如果我們嘗試刪除此 PRIMARY KEY,MySQL 將丟擲以下錯誤 −
mysql> Alter table Accounts DROP PRIMARY KEY; ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
廣告