bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/SQL/SQL References
SQL•SQL References

SQL CONSTRAINT Keyword

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind SQL CONSTRAINT Keyword?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ TABLE Persons
3Order

Put the learning moves in the order that makes the concept easiest to apply.

DROP a FOREIGN KEY Constraint
DROP a PRIMARY KEY Constraint
DROP a UNIQUE Constraint

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

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 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 drop a PRIMARY KEY constraint, use 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

To drop a FOREIGN KEY constraint, use the following SQL:

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 drop a CHECK constraint, use the following SQL:

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT CHK_PersonAge;

MySQL

ALTER TABLE Persons
DROP CHECK CHK_PersonAge;

Previous

SQL COLUMN Keyword

Next

SQL CREATE Keyword