ADD CONSTRAINT
The ADD CONSTRAINT command is used to create a constraint after a table is already created.
The following SQL adds a constraint named "PK_Person" that is a PRIMARY KEY constraint on multiple columns (ID and LastName):
Example
sql
ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);
DROP CONSTRAINT
The DROP CONSTRAINT command is used to delete a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK constraint.
DROP a UNIQUE Constraint
To drop a UNIQUE constraint, use the following SQL:
sql
ALTER TABLE Persons
DROP CONSTRAINT UC_Person;
sql
ALTER TABLE Persons
DROP INDEX UC_Person;
SQL Server / Oracle / MS Access:
MySQL:
DROP a PRIMARY KEY Constraint
To drop a PRIMARY KEY constraint, use the following SQL:
sql
ALTER TABLE Persons
DROP CONSTRAINT PK_Person;
sql
ALTER TABLE Persons
DROP PRIMARY KEY;
SQL Server / Oracle / MS Access:
MySQL:
DROP a FOREIGN KEY Constraint
To drop a FOREIGN KEY constraint, use the following SQL:
sql
ALTER TABLE Orders
DROP CONSTRAINT FK_PersonOrder;
sql
ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder;
SQL Server / Oracle / MS Access:
MySQL:
DROP a CHECK Constraint
To drop a CHECK constraint, use the following SQL:
sql
ALTER TABLE Persons
DROP CONSTRAINT CHK_PersonAge;
sql
ALTER TABLE Persons
DROP CHECK CHK_PersonAge;
SQL Server / Oracle / MS Access:
MySQL: