Home > Database > Mysql Tutorial > body text

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

王林
Release: 2023-08-23 21:21:02
forward
1316 people have browsed it

如果我尝试从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) 
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template