![]() |
The primary key is the key that may contain one or more columns that uniquely identify each row in a table. We can add a primary key constraint during table creation. To remove a primary key from a table, use the ALTER TABLE statement with the DROP CONSTRAINT clause. Here, we will see how to remove a primary key constraint from a table in SQL. How to Remove Primary Key in SQL?To remove a primary key from a column in a table, use the ALTER TABLE query with the DROP CONSTRAINT clause:
Removing Primary Key Constraint in SQL ExampleLet’s look at an example of removing the primary key constraint from a table in SQL. Step 1: Create a Table with a Primary Key ConstraintCreate a table with a primary key constraint using the following SQL query: CREATE DATABASE GeeksforGeeks; USE GeeksforGeeks; CREATE TABLE student_details( stu_id VARCHAR(8) NOT NULL PRIMARY KEY, stu_name VARCHAR(20), stu_branch VARCHAR(20) ); Step 2: Check the Constraints of the TableUse the following SQL query to check the constraints of the table: SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ; Output: Here in output ‘PK’ means primary key. The constraint can be known from the object explorer as well. Step 3: Remove the Primary Key ConstraintNow, we will use the DROP CONSTRAINT to remove the primary key from the database. ALTER TABLE student_details DROP CONSTRAINT PK__student___E53CAB21F07312DD Step 4: Verify the Removal of the Primary Key ConstraintSELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ; Output: ConclusionRemoving a primary key constraint from a table in SQL can be useful in various scenarios. By following these step-by-step instructions, you can effectively remove a primary key constraint from a table. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |