- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?
We can remove composite PRIMARY KEY constraint from multiple columns of an existing table by using DROP keyword along with ALTER TABLE statement.
Example
Suppose we have a table ‘Room_allotment’ having a composite PRIMARY KEY constraint on columns ‘ID’ and ‘RoomNo’ 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)
Now if we want to remove the composite PRIMARY KEY constraint then we can use ALTER TABLE statement as follows −
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)
The above result set shows that composite PRIMARY KEY constraint from columns ‘ID’ and ‘RoomNo’ has been removed.
Advertisements