如果我尝试从AUTO_INCREMENT列中删除PRIMARY KEY约束会怎样?
众所周知,AUTO_INCREMENT列上必须具有PRIMARYKEY约束,因此,当我们尝试从AUTO_INCREMENT列中删除PRIMARYKEY约束时,MySQL返回有关错误表定义的错误消息。以下示例将演示它-
示例
假设我们有以下描述的“帐户”表-
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)
它的字段“Sr”带有AUTO_INCREMENT和PRIMARYKEY定义。现在,如果我们尝试删除此PRIMARYKEY,则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