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
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 tofoo.a: -
Multiple columns of a new table have their constraints defined after the table’s columns. For example, this statement applies the
PRIMARY KEYconstraint tofoo’s columnsaandb:
The
DEFAULT and NOT NULL constraints cannot be applied to multiple columns.- Existing tables can have the following constraints added:
-
CHECK,FOREIGN KEY, andUNIQUEconstraints can be added through . For example, this statement adds theUNIQUEconstraint tobaz.id: -
DEFAULTvalues andNOT NULLconstraints can be added through . For example, this statement adds the tobaz.bool: -
constraints can be added with / in the following circumstances:
- A statement precedes the
ADD CONSTRAINT/ADD PRIMARY KEYstatement in the same transaction. For examples, see and . - The current , the default primary key created if none is explicitly defined at table creation.
- The
ADD CONSTRAINT/ADD PRIMARY KEYis in the same transaction as aCREATE TABLEstatement with no primary key defined.
- A statement precedes the
-
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 theCONSTRAINT clause before defining the constraint:

