Home >Database >Mysql Tutorial >What happens if I try to remove the PRIMARY KEY constraint from the AUTO_INCREMENT column?

What happens if I try to remove the PRIMARY KEY constraint from the AUTO_INCREMENT column?

王林
王林forward
2023-08-23 21:21:021351browse

如果我尝试从AUTO_INCREMENT列中删除PRIMARY KEY约束会发生什么?

As we know, AUTO_INCREMENT column must also have PRIMARY KEY constraint, so when we try to remove PRIMARY KEY constraint from AUTO_INCREMENT column, MySQL returns incorrect information about the table Defined error message. The following example will demonstrate it −

Example

Suppose we have a table called “Accounts” with the following description −

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) 

It has a table with AUTO_INCREMENT and Field 'Sr' defined by PRIMARY KEY. Now, if we try to delete this PRIMARY KEY, MySQL will throw the following error -

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

The above is the detailed content of What happens if I try to remove the PRIMARY KEY constraint from the AUTO_INCREMENT column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete