Introduction
Today, we will learn how to find a Primary Key, a Unique key, or a Foreign Key in a table using SQL. We will also learn how to delete a primary key, unique key, or a foreign key from a table using SQL. SQL Server supports ten types of keys. Sometimes, we find the below issues while working on keys,
- We don’t have the database access to delete the keys on the Server by SSMS.
- The keys nare available on production database and in the development database keys are not the same.
- There are several Unique Keys on a table and we need to delete them all.
- How to delete several Foreign Keys from a table and their references using SQL.
To resolve the above issues, we can simply use SQL queries and update them on the live server. We know only a table name there so we need to perform all the tasks by table name only.
We have the below database tables and their diagrams.

tb_country has the below definition:
tbl_state has the below definition:
bl_city has the below definition:
I created a lot of Unique/Foreign Keys on the above tables. And only 3 Primary Keys for 3 tables (Why there is only 1 Primary key per table, readers know very well).
Get the list of primary key, unique key, foreign key on a table
To find the list of all Primary/Unique/Foreign Keys in a database, use the below query. It will provide all sets of keys in the database.
![Get the list of primary key, unique key, foreign key on a table]()
To find the list of all Primary/Unique/Foreign Keys in a table, use the below query. It will provide all sets of keys in the table. We are working on tb_city.
![Get the list of primary key, unique key, foreign key on a table]()
In tb_city I have one Unique key, one Primary Key, and two Foreign keys there. You can filter these keys by using CONSTRAINT_TYPE in WHERE clause.
Delete Primary Key from a Table using SQL
To delete the primary key of a table, use the below query. Kindly confirm before that this Primary key is not attached to another table in reference.
In the above query, I used IS EXISTS to find if there are any primary keys on the table. After execution of the above query Primary key will be deleted. When we search keys on that table, the result will be like below.
![Delete Primary Key from a Table using SQL]()
Now we have Unique and Foreign Keys on the table.
Delete Unique Keys from a Table using SQL
To delete Unique keys of a table, use the below query. Kindly confirm before that these Unique keys are not attached to another table in reference.
In the above query, I used WHILE loop to check that if any Unique key exists or not. We used the loop because there may be multiple unique keys in our table. After execution of the above query, all Unique keys will be deleted. When we search keys on that table, the result will be like below,
![Delete Unique Keys from a Table using SQL]()
Now, we have only Foreign keys on a table.
Delete Foreign Keys from a Table using SQL
To delete Foreign keys of a table, use the below query.
In the above query, I used WHILE loop to check that if any Foreign key exists or not. We used the loop because there may be multiple Foreign keys in our table. After execution of the above query, all Foreign keys will be deleted. When we search keys on that table, the result will be like below,
![Delete Foreign Keys from a Table using SQL]()
Now we don't have any keys in table.
Delete All Keys from a Table using SQL (Final Code)
Summary
Issues which we discussed in the introduction have been resolved successfully by queries. If you have some alternate way to achieve this kind of requirement then please let me know, or if you have some query then please leave your comments.