Constraints offer additional data integrity by enforcing conditions on the data within a column. Whenever values are manipulated (inserted, deleted, or updated), constraints are checked and modifications that violate constraints are rejected.
For example, the UNIQUE constraint requires that all values in a column be unique from one another (except NULL values). If you attempt to write a duplicate value, the constraint rejects the entire statement.
Supported constraints
| Constraint | Description | 
|---|---|
| CHECK | Values must return TRUEorNULLfor a Boolean expression. | 
| DEFAULTvalue | If a value is not defined for the constrained column in an INSERTstatement, theDEFAULTvalue is written to the column. | 
| FOREIGN KEY | Values must exactly match existing values from the column it references. | 
| NOT NULL | Values may not be NULL. | 
| PRIMARY KEY | Values must uniquely identify each row (one per table). This behaves as if the NOT NULLandUNIQUEconstraints are applied, as well as automatically creates an index for the table using the constrained columns. | 
| UNIQUE | Each non-NULL value must be unique. This also automatically creates an index for the table using the constrained columns. | 
Using constraints
Add constraints
How you add constraints depends on the number of columns you want to constrain, as well as whether or not the table is new.
- One column of a new table has its constraints defined after the column's data type. For example, this statement applies the - PRIMARY KEYconstraint to- foo.a:- > CREATE TABLE foo (a INT PRIMARY KEY);
- Multiple columns of a new table have their constraints defined after the table's columns. For example, this statement applies the - PRIMARY KEYconstraint to- foo's columns- aand- b:- > CREATE TABLE bar (a INT, b INT, PRIMARY KEY (a,b));
DEFAULT and NOT NULL constraints cannot be applied to multiple columns.
  - Existing tables can have the following constraints added: - CHECK,- FOREIGN KEY, and- UNIQUEconstraints can be added through- ALTER TABLE...ADD CONSTRAINT. For example, this statement adds the- UNIQUEconstraint to- baz.id:
 - > ALTER TABLE baz ADD CONSTRAINT id_unique UNIQUE (id);- DEFAULTvalues can be added through- ALTER TABLE...ALTER COLUMN. For example, this statement adds the Default Value constraint to- baz.bool:
 - > ALTER TABLE baz ALTER COLUMN bool SET DEFAULT true;- PRIMARY KEYand- NOT NULLconstraints cannot be added or changed. However, you can go through this process to migrate data from your current table to a new table with the constraints you want to apply.
 
Order of constraints
The order in which you list constraints is not important because constraints are applied to every modification of their respective tables or columns.
Name constraints on new tables
You can name constraints applied to new tables using the CONSTRAINT clause before defining the constraint:
> CREATE TABLE foo (a INT CONSTRAINT another_name PRIMARY KEY);
> CREATE TABLE bar (a INT, b INT, CONSTRAINT yet_another_name PRIMARY KEY (a,b));
View constraints
To view a table's constraints, use SHOW CONSTRAINTS or SHOW CREATE.
Remove constraints
The procedure for removing a constraint depends on its type:
| Constraint Type | Procedure | 
|---|---|
| CHECK | Use DROP CONSTRAINT | 
| DEFAULTvalue | Use ALTER COLUMN | 
| FOREIGN KEY | Use DROP CONSTRAINT | 
| NOT NULL | Use ALTER COLUMN | 
| PRIMARY KEY | Primary Keys cannot be removed. However, you can move the table's data to a new table with this process. | 
| UNIQUE | The UNIQUEconstraint cannot be dropped directly.  To remove the constraint, drop the index that was created by the constraint, e.g.,DROP INDEX my_unique_constraint CASCADE(note thatCASCADEis required for dropping indexes used by unique constraints). | 
Change constraints
The procedure for changing a constraint depends on its type:
| Constraint Type | Procedure | 
|---|---|
| CHECK | Issue a transaction that adds a new CHECKconstraint (ADD CONSTRAINT), and then remove the existing one (DROP CONSTRAINT). | 
| DEFAULTvalue | The DEFAULTvalue can be changed throughALTER COLUMN. | 
| FOREIGN KEY | Issue a transaction that adds a new FOREIGN KEYconstraint (ADD CONSTRAINT), and then remove the existing one (DROP CONSTRAINT). | 
| NOT NULL | The NOT NULLconstraint cannot be changed, only removed. However, you can move the table's data to a new table with this process. | 
| PRIMARY KEY | Primary Keys cannot be modified. However, you can move the table's data to a new table with this process. | 
| UNIQUE | Issue a transaction that adds a new UNIQUEconstraint (ADD CONSTRAINT), and then remove the existing one (DROP CONSTRAINT). | 
Table migrations to add or change immutable constraints
If you want to make a change to an immutable constraint, you can use the following process:
- Create a new table with the constraints you want to apply.
- Move the data from the old table to the new one using INSERTfrom aSELECTstatement.
- Drop the old table, and then rename the new table to the old name. This cannot be done transactionally.