Removing Primary Key Constraint in SQL Example

Let’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 Constraint

Create a table with a primary key constraint using the following SQL query:

CREATE DATABASE w3wiki;
USE w3wiki;
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 Table

Use 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 Constraint

Now, 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 Constraint

SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ;

Output:

SQL Query to Remove Primary Key

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.

Similar Reads

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 Example

Let’s look at an example of removing the primary key constraint from a table in SQL....

Conclusion

Removing 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....

Contact Us