Home > Database > Mysql Tutorial > body text

How can we remove a compound PRIMARY KEY constraint that applies to multiple columns of an existing MySQL table?

WBOY
Release: 2023-08-24 13:37:02
forward
1322 people have browsed it

我们如何删除应用于现有 MySQL 表的多个列的复合 PRIMARY KEY 约束?

We can remove compound PRIMARY KEY constraints from multiple columns of an existing table by using DROP keyword and ALTER TABLE statement.

Example

Suppose we have a table "Room_allotment" with composite primary key constraints on "ID" and "RoomNo" columns as follows -

mysql> describe room_allotment;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | 0       |       |
| Name   | varchar(20) | NO   | PRI |         |       |
| RoomNo | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.06 sec)
Copy after login

Now, if we If we want to delete the compound PRIMARY KEY constraint, then we can use the ALTER TABLE statement as shown below -

mysql> Alter table room_allotment DROP PRIMARY KEY;
Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0  

mysql> describe room_allotment;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   |     | 0       |       |
| Name   | varchar(20) | NO   |     |         |       |
| RoomNo | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.07 sec)
Copy after login

The above result set shows that the compound PRIMARY KEY constraint of columns "ID" and "RoomNo" has been deleted.

The above is the detailed content of How can we remove a compound PRIMARY KEY constraint that applies to multiple columns of an existing MySQL table?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!