Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

CONSTRAINT

ADD CONSTRAINT

The ADD CONSTRAINT command is utilized to establish a constraint after the creation of a table.

Here’s an example SQL statement that adds a constraint named “PK_Person”, which is a PRIMARY KEY constraint on multiple columns (ID and LastName).

Example

ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);

DROP CONSTRAINT

The DROP CONSTRAINT command is employed to remove a constraint, such as UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK, from a table.

DROP a UNIQUE Constraint

To remove a UNIQUE constraint, employ the following SQL:

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT UC_Person; 

MySQL:

ALTER TABLE Persons
DROP INDEX UC_Person; 

DROP a PRIMARY KEY Constraint

To remove a PRIMARY KEY constraint, utilize the following SQL:

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT PK_Person; 

MySQL:

ALTER TABLE Persons
DROP PRIMARY KEY

DROP a FOREIGN KEY Constraint

For removing a FOREIGN KEY constraint, execute the following SQL statement:

SQL Server / Oracle / MS Access:

ALTER TABLE Orders
DROP CONSTRAINT FK_PersonOrder; 

MySQL:

ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder; 

DROP a CHECK Constraint

To eliminate a CHECK constraint, employ the following SQL:

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT CHK_PersonAge; 

MySQL:

ALTER TABLE Persons
DROP CHECK CHK_PersonAge;