---
title: CONSTRAINT clause
description: Define primary key, foreign key, unique, and check constraints on a table with the CONSTRAINT clause in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-table-constraint
---

# CONSTRAINT clause

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Adds an informational primary key, an informational foreign key, or an informational unique constraint as part of a [CREATE TABLE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-table-using.md) or [CREATE MATERIALIZED VIEW](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-materialized-view.md) statement.

To add a check constraint to a Delta Lake table use [ALTER TABLE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table.md) after the table has been created.

To add an informational relationship constraint to a metric view, use [ALTER VIEW](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-view.md) (Beta). See [Relationships between metric views](https://docs.databricks.com/aws/en/uc-semantics/metric-views/relationships.md).

## Syntax

Use the `table_constraint` clause to define constraints which span multiple columns or to separate the syntax away from the column definition.

```
table_constraint
   { [ CONSTRAINT name ]
     { PRIMARY KEY ( key_column [ TIMESERIES ] [, ...] ) [ constraint_option [...] ] |
       UNIQUE ( key_column [, ...] ) [ constraint_option [...] ] |
       { FOREIGN KEY ( foreign_key_column [, ...] )
         REFERENCES parent_table [ ( parent_column [, ...] ) ]
         [ foreign_key_option | constraint_option ] [...]
       }
     }
   }
```

Use the `column_constraint`clause to define constraints specific to a single column definition.

```
column_constraint
  { [ CONSTRAINT name ]
    { PRIMARY KEY [ constraint_option ] [...] |
      UNIQUE [ constraint_option ] [...] |
      { [ FOREIGN KEY ]
        REFERENCES parent_table [ ( parent_column [, ...] ) ]
        [ foreign_key_option | constraint_option ] [...]
      }
    }
  }
```

```
constraint_option
    { NOT ENFORCED |
      DEFERRABLE |
      INITIALLY DEFERRED |
      { RELY | NORELY } }

foreign_key_option
    { MATCH FULL |
      ON UPDATE NO ACTION |
      ON DELETE NO ACTION }
```

For compatibility with non-standard SQL dialects you can specify `ENABLE NOVALIDATE` instead of `NOT ENFORCED DEFERRABLE INITIALLY DEFERRED`.

## Parameters

* **`CONSTRAINT` [name](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-identifiers.md)**

  Optionally specifies a name for the constraint.
  The name must be unique within the schema.
  If no name is provided Databricks will generate one.

* **`PRIMARY KEY` ( key\_column \[ TIMESERIES ] \[, …] ) \[ constraint\_option \[…] ]**

  **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 11.3 LTS and above ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Unity Catalog only

  Adds an informational primary key constraint to the table or materialized view. A table or materialized view can have at most one primary key.

  Primary key columns are implicitly defined as `NOT NULL`.

  Primary key constraints are not supported for tables in the `hive_metastore` catalog.

  * **[key\_column](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-identifiers.md)**

    A column of the subject table or materialized view. Column names must not be repeated.

  * **`TIMESERIES`**

    **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 13.3 LTS and above

    Optionally labels the primary key column component as representing a timeseries.

* **`PRIMARY KEY [ constraint_option ] [...]`**

  Adds a single column primary key constraint to the table or materialized view, using the preceding key column definition.

  This `column_constraint` is equivalent to the `table_constraint`

  `PRIMARY KEY (key_column) [ constraint_option ] [...]`

* `UNIQUE ( key_column [, ...] ) [ constraint_option [...] ]`

  **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 18.2 and above ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Unity Catalog only

  Adds an informational unique constraint to the table. A table can have multiple unique constraints.

  Unique constraints are not supported for tables in the `hive_metastore` catalog.

  Columns in a `UNIQUE` constraint can be nullable, including when the constraint is referenced by a foreign key. `NULL` values in `UNIQUE` columns are treated as distinct from each other, so duplicate `NULL` values do not violate the semantics of a `UNIQUE` constraint.

  * **[key\_column](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-identifiers.md)**

    A column of the subject table. Column names must not be repeated.

* **`UNIQUE [ constraint_option ] [...]`**

  Adds a single column unique constraint to the table, using the preceding key column definition.

  This `column_constraint` is equivalent to the `table_constraint`

  `UNIQUE (key_column) [ constraint_option ] [...]`

* **`FOREIGN KEY (foreign_key_column [, ...] ) REFERENCES parent_table [ ( parent_column [, ...] ) ] foreign_key_option`**

  **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 11.3 LTS and above ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Unity Catalog only

  Adds an informational foreign key (referential integrity) constraint to the table or materialized view.

  Foreign key constraints are not supported for tables in the `hive_metastore` catalog.

  Foreign key constraints which only differ in the permutation of the foreign key columns are not allowed.

  * **[foreign\_key\_column](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-identifiers.md)**

    A column of the subject table or materialized view. Column names must not be repeated.
    The data type of each column must match the type of the matching `parent_column`.
    The number of columns must match the number of `parent_column`s.
    Two foreign keys cannot share an identical set of foreign key columns.

  * **[parent\_table](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#table-name)**

    Specifies the table or materialized view the foreign key refers to.
    The table must have a defined `PRIMARY KEY` constraint or, in Databricks Runtime 18.2 and above, a defined `UNIQUE` constraint, and you must have the `SELECT` privilege on the table.

  * **[parent\_column](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-identifiers.md)**

    A column in the parent table or materialized view which is part of its primary key or, in Databricks Runtime 18.2 and above, part of a unique constraint.
    All primary key columns or unique constraint columns of the parent table or materialized view must be listed.

    If parent columns are not listed, they are specified by the order given in the `PRIMARY KEY` definition.

  * **`FOREIGN KEY REFERENCES parent_table [ ( parent_column ) ] foreign_key_option`**

    Adds a single column foreign key constraint to the table or materialized view, using the preceding foreign key column definition.

    This `column_constraint` is equivalent to the `table_constraint`

    `FOREIGN KEY ( foreign_key_column ) REFERENCES parent_table [ ( parent_column ) ] foreign_key_option`

* **constraint\_option**

  Lists the properties of the constraints.
  All properties are optional but implied by default.
  Each property can at most be specified once.

  * **`NOT ENFORCED`**

    Databricks takes no action to enforce it for existing or new rows.

  * **`DEFERRABLE`**

    The constraint enforcement can be deferred.

  * **`INITIALLY DEFERRED`**

    Constraint enforcement is deferred.

  * **`NORELY` or `RELY`**

    **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 14.2 and above for `PRIMARY KEY` constraints

    **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 15.4 and above for `FOREIGN KEY` constraints

    **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 18.2 and above for `UNIQUE` constraints

    If `RELY`, Databricks may exploit the constraint to rewrite and optimize queries.
    It is the user's responsibility to ensure the constraint is satisfied.
    Relying on a constraint that is not satisfied may lead to incorrect query results.
    Query optimizations associated with `RELY` require pro or serverless SQL warehouses, or serverless compute, and aren't supported on classic SQL warehouses.

    The default is `NORELY`.

* **foreign\_key\_option**

  Lists the properties specific to foreign key constraints.
  All properties are optional but implied by default.
  Each property can at most be specified once.

  * **`MATCH FULL`**

    For the constraint to be considered true all column values must be `NOT NULL`.

  * **`ON UPDATE NO ACTION`**

    If the parent `PRIMARY KEY` is updated, Databricks takes no action to restrict the update or update the foreign key.

  * **`ON DELETE NO ACTION`**

    If the parent row is deleted, Databricks takes no action to restrict the action, update the foreign key, or delete the dependent row.

> [!IMPORTANT]
>
> Databricks does not enforce primary key, foreign key, or unique constraints. Confirm key constraints before adding a primary key, foreign key, or unique constraint. Your ingest process may provide such assurance, or you can run checks against your data.

## Examples

```sql
-- Create a table with a primary key
> CREATE TABLE persons(first_name STRING NOT NULL, last_name STRING NOT NULL, nickname STRING,
                       CONSTRAINT persons_pk PRIMARY KEY(first_name, last_name));

-- create a table with a foreign key
> CREATE TABLE pets(name STRING, owner_first_name STRING, owner_last_name STRING,
                    CONSTRAINT pets_persons_fk FOREIGN KEY (owner_first_name, owner_last_name) REFERENCES persons);

-- Create a table with a single column primary key and system generated name
> CREATE TABLE customers(customerid STRING NOT NULL PRIMARY KEY, name STRING);

-- Create a table with a names single column primary key and a named single column foreign key
> CREATE TABLE orders(orderid BIGINT NOT NULL CONSTRAINT orders_pk PRIMARY KEY,
                      customerid STRING CONSTRAINT orders_customers_fk REFERENCES customers);

-- Create a table with a column unique constraint
> CREATE TABLE person_contacts(first_name STRING NOT NULL, last_name STRING NOT NULL,
                               nickname STRING UNIQUE);

-- Create a table with a multi-column unique constraint
> CREATE TABLE person_accounts(first_name STRING NOT NULL, last_name STRING NOT NULL, account_id STRING,
                               CONSTRAINT person_accounts_uq UNIQUE(first_name, last_name));

-- Create a table with a foreign key that references a unique column
> CREATE TABLE pet_tags(tag STRING NOT NULL, owner_nickname STRING,
                        CONSTRAINT pet_tags_owner_fk FOREIGN KEY (owner_nickname) REFERENCES person_contacts(nickname));
```

## Related articles

* [ADD CONSTRAINT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table-add-constraint.md)
* [ALTER TABLE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table.md)
* [CREATE TABLE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-table-using.md)
* [DROP CONSTRAINT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table-drop-constraint.md)

- [CREATE MATERIALIZED VIEW](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-materialized-view.md)
