Can we remove a 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)
Updated on: 2019-08-22T06:31:37+05:30

482 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements