Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql cannot delete the primary key in the table

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Mysql deletes the error of the primary key in the table, as shown in the following code:

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. The cause of the problem:

When you look at the type of the student table, you find that there is an auto_increment (incremental) type option in the primary key column. To delete the primary key in the table, you need to delete the auto_increment type first. The code is as follows:

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. Delete the auto_increment type above the primary key column in the student table. The code is as follows:

Mysql > alter table student change id id int;Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0

Tip: the alter table student change id id int; command modifies the type of the column in the student table, and the auto_increment type is naturally deleted.

4. Check the type of the student table and find that the auto_increment type has been deleted. The code is as follows:

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. Delete the primary key in student. The code is as follows:

Mysql > alter table student drop primary key;Query OK, 0 rows affected (0.10 sec) Records: 0 Duplicates: 0 Warnings: 0

6. Finally, look at the type of student table and find that the primary key in the table has been deleted. The code is as follows:

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report