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

The method of deleting primary key by mysql

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

Share

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

This article will explain in detail the method of deleting the primary key in mysql. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First, let's look at the syntax for deleting a primary key:

ALTER TABLE TABLE_NAME DROP PRIMARY KEY

There are two situations to consider when deleting a primary key in MySQL:

1. The primary key column does not have any constraints and can be deleted directly.

Example:

Mysql > create table test1_3 (- > id int not null primary key,-> name char (10)->); Query OK, 0 rows affected (0.01 sec)

We can use drop directly to delete the primary key

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

2. If it is the primary key of self-increment (AUTO_INCREMENT attribute)

Example:

Mysql > create table test1_2 (- > id int not null auto_increment,-> name char (10),-> primary key (id)->); Query OK, 0 rows affected (0.00 sec) mysql > desc test1_2 +-+ | Field | Type | Null | Key | Default | Extra | +-+ -+ | id | int (11) | NO | PRI | NULL | auto_increment | | name | char (10) | YES | | NULL | | +-+-+ 2 rows in set (0.00 sec)

If you delete it directly, an error will be reported.

Mysql > alter table test1_2 drop primary key

Output:

ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key # this means that this column is auto-growing and cannot be deleted directly

The property of the column also has an AUTO_INCREMENT, so the auto-grow attribute of this column must be removed before the primary key can be deleted.

Mysql > alter table test1_2 modify id int;Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0mysql > alter table test1_2 drop primary key;Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 about the method of deleting the primary key by mysql, that's all. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 222

*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