SQL Keyword - CONSTRAINT

CONSTRAINT Keyword

The Constraints are the rules that we can apply on the type of data in a table. like not null, unique, check the limit, primary, etc.

Add Constraint

The ADD CONSTRAINT keyword or command is used to add a constraint to an existing column.

We create a table and then realise that we have forgotten to add constraint in a specific column then Add Constraint Commnad is used.

The ADD CONSTRAINT is a combination of two keywords ADD and CONSTRAINT. they both are different keywords.

But whenever we have to create a constraint to an existing column we use ADD CONSTRAINT keyword.

Syntax

ALTER TABLE <TABLE_NAME>
ADD CONSTRAINT <CONSTRAINT_NAME> <SPECIFIC_CONTRAINT> (<COLUMN_NAME1>,<COLUMN_NAME2>);

Example 1

ALTER TABLE Employee ADD CONSTRAINT UniqueName UNIQUE(Emp_Name);

Example 2

ALTER TABLE Employee ADD CONSTRAINT PK_Employee PRIMARY KEY (Emp_ID);

Drop Constraint

The DROP CONSTRAINT keyword or command is used to delete a constraint to an existing column.

The DROP CONSTRAINT is a combination of two keywords DROP and CONSTRAINT. they both are different keywords.

But whenever we have to create a constraint to an existing column we use DROP CONSTRAINT keyword.

Syntax

ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <CONSTRAINT_NAME>;

Example

ALTER TABLE Employee DROP CONSTRAINT PK_Employee;

Summary

The Constraints are the rules that we can apply on the type of data in a table.

Next Recommended Reading SQL Constraint - Foreign Key