Remove Primary Key in MySQL?


To remove primary key in MySQL, use tje drop primary key command. To understand the concept, let us create a table with column as primary key.

mysql> create table PrimaryKeyDemo
   -> (
   -> id int not null,
   -> Primary key(id)
   -> );
Query OK, 0 rows affected (0.60 sec)

Let us check the description of the table with the help of DESC command. The query is as follows.

mysql> desc PrimaryKeyDemo;

The following is the output.

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.06 sec)

Look at the output above, there is a column ‘Key’ which has the PRI keyword. This itself says that the “id” column is primary key. Now, let us remove the primary key with the help of ALTER and DROP command. The query is as follows.

mysql>  alter table PrimaryKeyDemo drop primary key;
Query OK, 0 rows affected (1.70 sec)
Records: 0  Duplicates: 0  Warnings: 0

Let us now check whether primary key is removed or not successfully.

mysql> DESC PrimaryKeyDemo;

The following is the output that won’t display primary key now, since we have deleted it above.

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements