
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Primary Key from MySQL Table
Yes, we can remove a primary key using the DROP in MySQL. Following is the syntax −
alter table yourTableName drop primary key;
Let us first create a table −
mysql> create table DemoTable ( UserId int NOT NULL PRIMARY KEY ); Query OK, 0 rows affected (0.58 sec)
Following is the query to check the description of table −
mysql> desc DemoTable;
This will produce the following output displaying the Primary Key −
+--------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+-------+ | UserId | int(11) | NO | PRI | NULL | | +--------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec)
Following is the query to remove primary key from table −
mysql> alter table DemoTable drop primary key; Query OK, 0 rows affected (1.51 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of the table once again −
mysql> desc DemoTable;
This will produce the following output. Now the primary key isn’t visible −
+--------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+-------+ | UserId | int(11) | NO | | NULL | | +--------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec)
Advertisements