Horje
How to Remove a Primary Key in SQL?

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:

ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

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 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 Table

Use the following SQL query to check the constraints of the table:

SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ;

Output:

constraints in table

Here in output ‘PK’ means primary key. The constraint can be known from the object explorer as well.

primary key

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

removing primary key

Step 4: Verify the Removal of the Primary Key Constraint

SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ;

Output:

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.




Reffered: https://www.geeksforgeeks.org


Databases

Related
SQL Server PATINDEX() Function SQL Server PATINDEX() Function
How to Enable Authentication on MongoDB ? How to Enable Authentication on MongoDB ?
MongoDB - bulkWrite() Method MongoDB - bulkWrite() Method
How to Use Go With MongoDB? How to Use Go With MongoDB?
SQL Query to Calculate Total Number of Weeks Between Two Dates SQL Query to Calculate Total Number of Weeks Between Two Dates

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14