Back to cookbooks list Articles Cookbook

How to Remove a Primary Key in SQL

  • DROP CONSTRAINT
  • DROP PRIMARY KEY
  • ALTER TABLE

Problem:

You want to drop a primary key from a table in a database.

Example:

We want to remove the primary key from the table product.

Solution 1:

ALTER TABLE product
DROP PRIMARY KEY;

Discussion:

To drop a primary key from a table, use an ALTER TABLE clause with the name of the table (in our example, product) followed by the clause DROP PRIMARY KEY.

Since a table can have only one primary key, you don’t need to specify the primary key column(s).

Solution 2 (dropping the primary key constraint):

ALTER TABLE product
DROP CONSTRAINT PK_product_id;

Discussion:

Another way is to use ALTER TABLE with the DROP CONSTRAINT clause with the name of the primary key constraint.

This is a good way to remove the primary key if you know the name of the primary key constraint. However, you must specify the name of the primary key constraint.

Recommended courses:

Recommended articles:

See also: