UNIQUE specifies that each non-NULL value in the constrained column must be unique.
Details
-
You can insert
NULLvalues into columns with theUNIQUEconstraint becauseNULLis the absence of a value, so it is never equal to otherNULLvalues 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 isNULL. If you need to strictly enforce uniqueness, use the in addition to theUNIQUEconstraint. You can also achieve the same behavior through the table’s . -
Columns with the
UNIQUEconstraint automatically have an created with the name_<columns_key. To avoid having two identical indexes, you should not create indexes that exactly match theUNIQUEconstraint’s columns and order. TheUNIQUEconstraint depends on the automatically created index, so dropping the index also drops theUNIQUEconstraint. Conversely, also drops the automatically created index. -
When using the
UNIQUEconstraint 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 theUNIQUEconstraint to each column individually. -
You can define the
UNIQUEconstraint when you create a table, or you can add it to an existing table through .
REGIONAL BY ROW table, it is automatically partitioned on the . Explicit index partitioning is not required.
While CockroachDB process an or statement on a particular database, creating or modifying an index will throw an error. Similarly, all and statements will be blocked while an index is being modified on a REGIONAL BY ROW table within the same database.
For an example that uses unique indexes, see .
Syntax
You can defineUNIQUE constraints at the table level and at the column level.
Table level
| Parameter | Description |
|---|---|
table_name | The name of the table you are creating. |
column_def | Definitions for any other columns in the table. |
name | The name you want to use for the constraint, which must be unique to its table and follow these . |
column_name | The name of the column you want to constrain. |
table_constraints | Any other table-level you want to apply. |
Column level
| Parameter | Description |
|---|---|
table_name | The name of the table you are creating. |
column_name | The name of the constrained column. |
column_type | The constrained column’s . |
column_constraints | Any other column-level you want to apply to this column. |
column_def | Definitions for any other columns in the table. |
table_constraints | Any table-level you want to apply. |
Usage example
UNIQUE constraint alone to insert NULL values in a way that causes rows to appear to have rows with duplicate values.

