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.
|Check||Values must return
|Default Value||If a value is not defined for the constrained column in an
|Foreign Keys||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 Null and Unique constraints 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.|
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 Key constraint to
> 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 Key constraint to
> CREATE TABLE bar (a INT, b INT, PRIMARY KEY (a,b));The Default Value and Not Null constraints cannot be applied to multiple columns.
Existing tables can have the following constraints added:
Check, Foreign Key, and Unique constraints can be added through
ALTER TABLE...ADD CONSTRAINT. For example, this statement adds the Unique constraint to
> ALTER TABLE baz ADD CONSTRAINT id_unique UNIQUE (id);
Default Values can be added through
ALTER TABLE...ALTER COLUMN. For example, this statement adds the Default Value constraint to
> ALTER TABLE baz ALTER COLUMN bool SET DEFAULT true;
Primary Key and Not Null constraints 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));
The procedure for removing a constraint depends on its type:
|Primary Key||Primary Keys cannot be removed. However, you can move the table’s data to a new table with this process.|
|Unique||The Unique constraint cannot be dropped directly. However, you can use
The procedure for changing a constraint depends on its type:
|Check||Issue a transaction that adds a new Check constraint (
|Default Value||The Default Value can be changed through
|Foreign Keys||Issue a transaction that adds a new Foreign Key constraint (
|Not Null||The Not Null constraint 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 Unique 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
- Issue a transaction that drops the old table, and then renames the new table to the old name.