Unique Constraint

The UNIQUE constraint specifies that each non-NULL value in the constrained column must be unique.

Details

  • You can insert NULL values into columns with the UNIQUE constraint because NULL is the absence of a value, so it is never equal to other NULL values and not considered a duplicate value. This means that it's possible to insert rows that appear to be duplicates if one of the values is NULL.

If you need to strictly enforce uniqueness, use the NOT NULL constraint in addition to the UNIQUE constraint. You can also achieve the same behavior through the table's Primary Key.

  • Columns with the UNIQUE constraint automatically have an index created with the name <table name>_<columns>_key. To avoid having two identical indexes, you should not create indexes that exactly match the UNIQUE constraint's columns and order.

The UNIQUE constraint depends on the automatically created index, so dropping the index also drops the UNIQUE constraint.

  • When using the UNIQUE constraint on multiple columns, the collective values of the columns must be unique. This does not mean that each value in each column must be unique, as if you had applied the UNIQUE constraint to each column individually.

  • You can define the UNIQUE constraint when creating a table, or you can add it to existing tables through ADD CONSTRAINT.

Syntax

UNIQUE constraints can be defined at the table level. However, if you only want the constraint to apply to a single column, it can be applied at the column level.

Column level

CREATETABLEtable_name(column_namecolumn_typeUNIQUEcolumn_constraints,column_deftable_constraints))
ParameterDescription
table_nameThe name of the table you're creating.
column_nameThe name of the constrained column.
column_typeThe constrained column's data type.
column_constraintsAny other column-level constraints you want to apply to this column.
column_defDefinitions for any other columns in the table.
table_constraintsAny table-level constraints you want to apply.

Example

  1. > CREATE TABLE warehouses (
  2. warehouse_id INT PRIMARY KEY NOT NULL,
  3. warehouse_name STRING(35) UNIQUE,
  4. location_id INT
  5. );

Table level

CREATETABLEtable_name(column_def,CONSTRAINTnameUNIQUE(column_name,)table_constraints)
ParameterDescription
table_nameThe name of the table you're creating.
column_defDefinitions for any other columns in the table.
nameThe name you want to use for the constraint, which must be unique to its table and follow these identifier rules.
column_nameThe name of the column you want to constrain.
table_constraintsAny other table-level constraints you want to apply.

Example

  1. > CREATE TABLE logon (
  2. login_id INT PRIMARY KEY,
  3. customer_id INT,
  4. logon_date TIMESTAMP,
  5. UNIQUE (customer_id, logon_date)
  6. );

Usage example

  1. > CREATE TABLE IF NOT EXISTS logon (
  2. login_id INT PRIMARY KEY,
  3. customer_id INT NOT NULL,
  4. sales_id INT,
  5. UNIQUE (customer_id, sales_id)
  6. );
  1. > INSERT INTO logon (login_id, customer_id, sales_id) VALUES (1, 2, 1);
  1. > INSERT INTO logon (login_id, customer_id, sales_id) VALUES (2, 2, 1);
  1. duplicate key value (customer_id,sales_id)=(2,1) violates unique constraint "logon_customer_id_sales_id_key"

As mentioned in the details above, it is possible when using the UNIQUE constraint alone to insert NULL values in a way that causes rows to appear to have rows with duplicate values.

  1. > INSERT INTO logon (login_id, customer_id, sales_id) VALUES (3, 2, NULL);
  1. > INSERT INTO logon (login_id, customer_id, sales_id) VALUES (4, 2, NULL);
  1. > SELECT customer_id, sales_id FROM logon;
  1. +-------------+----------+
  2. | customer_id | sales_id |
  3. +-------------+----------+
  4. | 2 | 1 |
  5. | 2 | NULL |
  6. | 2 | NULL |
  7. +-------------+----------+

See also

Was this page helpful?
YesNo