|
1 | 1 | ---
|
2 | 2 | title: Constraints
|
3 |
| -summary: Constraints offer additional data integrity by enforcing conditions on the data within a column or row. |
| 3 | +summary: Constraints offer additional data integrity by enforcing conditions on the data within a column. |
4 | 4 | toc: false
|
5 | 5 | ---
|
6 | 6 |
|
7 |
| -Constraints offer additional data integrity by enforcing conditions on the data within a column or row. They are checked during DML operations and restrict the data values within a column to those specified within the constraint. |
| 7 | +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. |
8 | 8 |
|
9 |
| -If a constraint refers to only one column (column-level constraint), it can be defined against the column as part of its definition. If a constraint refers to more than one column (table-level constraint), it needs to be defined as a separate entry in the tables definition. |
| 9 | +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. |
10 | 10 |
|
11 |
| -The order of the constraints within the table definition is not important and does not determine the order in which the constraints are checked. Use the [`SHOW CONSTRAINTS`](show-constraints.html) or [`SHOW CREATE TABLE`](show-create-table.html) statement to show the constraints defined on a table. |
| 11 | +<div id="toc"></div> |
12 | 12 |
|
13 |
| -The different types of constraints are: |
| 13 | +## Supported Constraints |
14 | 14 |
|
15 |
| -| Constraint Type | Description | |
16 |
| -|-----------------|-------------| |
17 |
| -| [Check](check.html) | Specifies that the column value must satisfy a Boolean expression. | |
18 |
| -| [Default Value](default-value.html) | Specifies a value to populate a column with if none is provided.| |
19 |
| -| [Foreign Keys](foreign-key.html) | Specifies a column can only contain values exactly matching existing values from the column it references. | |
20 |
| -| [Not Null](not-null.html) | Specifies the column **may not** contain *NULL* values.| |
21 |
| -| [Primary Key](primary-key.html) | Specifies that the columns values are unique and that the columns **may not** contain *NULL* values. | |
22 |
| -| [Unique](unique.html) | Specifies that the columns values are unique and that the columns **may** contain *NULL* values. | |
| 15 | +| Constraint | Description | |
| 16 | +|------------|-------------| |
| 17 | +| [Check](check.html) | Values must return `TRUE` or `NULL` for a Boolean expression. | |
| 18 | +| [Default Value](default-value.html) | If a value is not defined for the constrained column in an `INSERT` statement, the Default Value is written to the column. | |
| 19 | +| [Foreign Keys](foreign-key.html) | Values must exactly match existing values from the column it references. | |
| 20 | +| [Not Null](not-null.html) | Values may not be *NULL*. | |
| 21 | +| [Primary Key](primary-key.html) | 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](indexes.html) for the table using the constrained columns. | |
| 22 | +| [Unique](unique.html) | Each non-*NULL* value must be unique. This also automatically creates an [index](indexes.html) for the table using the constrained columns. | |
| 23 | + |
| 24 | +## Using Constraints |
| 25 | + |
| 26 | +### Add Constraints |
| 27 | + |
| 28 | +How you add constraints depends on the number of columns you want to constrain, as well as whether or not the table is new. |
| 29 | + |
| 30 | +- **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 `foo.a`: |
| 31 | + |
| 32 | + ``` sql |
| 33 | + > CREATE TABLE foo (a INT PRIMARY KEY); |
| 34 | + ``` |
| 35 | +- **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 `foo`'s columns `a` and `b`: |
| 36 | + |
| 37 | + ``` sql |
| 38 | + > CREATE TABLE bar (a INT, b INT, PRIMARY KEY (a,b)); |
| 39 | + ``` |
| 40 | + |
| 41 | + {{site.data.alerts.callout_info}}The Default Value and Not Null constraints cannot be applied to multiple columns.{{site.data.alerts.end}} |
| 42 | + |
| 43 | +- **Existing tables** can have the following constraints added: |
| 44 | + - **Check**, **Foreign Key**, and **Unique** constraints can be added through [`ALTER TABLE...ADD CONSTRAINT`](add-constraint.html). For example, this statement adds the Unique constraint to `baz.id`: |
| 45 | + |
| 46 | + ~~~ sql |
| 47 | + > ALTER TABLE baz ADD CONSTRAINT id_unique UNIQUE (id); |
| 48 | + ~~~ |
| 49 | + |
| 50 | + - **Default Values** can be added through [`ALTER TABLE...ALTER COLUMN`](alter-column.html#set-or-change-a-default-value). For example, this statement adds the Default Value constraint to `baz.bool`: |
| 51 | + |
| 52 | + ~~~ sql |
| 53 | + > ALTER TABLE baz ALTER COLUMN bool SET DEFAULT true; |
| 54 | + ~~~ |
| 55 | + |
| 56 | + - **Primary Key** and **Not Null** constraints cannot be added or changed. However, you can go through [this process](#table-migrations-to-add-or-change-immutable-constraints) to migrate data from your current table to a new table with the constraints you want to apply. |
| 57 | + |
| 58 | +#### Order of Constraints |
| 59 | + |
| 60 | +The order in which you list constraints is not important because constraints are applied to every modification of their respective tables or columns. |
| 61 | + |
| 62 | +#### Name Constraints on New Tables |
| 63 | + |
| 64 | +You can name constraints applied to new tables using the `CONSTRAINT` clause before defining the constraint: |
| 65 | + |
| 66 | +``` sql |
| 67 | +> CREATE TABLE foo (a INT CONSTRAINT another_name PRIMARY KEY); |
| 68 | +
|
| 69 | +> CREATE TABLE bar (a INT, b INT, CONSTRAINT yet_another_name PRIMARY KEY (a,b)); |
| 70 | +``` |
| 71 | + |
| 72 | +### View Constraints |
| 73 | + |
| 74 | +To view a table's constraints, use [`SHOW CONSTRAINTS`](show-constraints.html) or [`SHOW CREATE TABLE`](show-create-table.html). |
| 75 | +
|
| 76 | +### Remove Constraints |
| 77 | +
|
| 78 | +The procedure for removing a constraint depends on its type: |
| 79 | +
|
| 80 | +| Constraint Type | Procedure | |
| 81 | +|-----------------|-----------| |
| 82 | +| [Check](check.html) | Use [`DROP CONSTRAINT`](drop-constraint.html) | |
| 83 | +| [Default Value](default-value.html) | Use [`ALTER COLUMN`](alter-column.html#remove-default-constraint) | |
| 84 | +| [Foreign Keys](foreign-key.html) | Use [`DROP CONSTRAINT`](drop-constraint.html) | |
| 85 | +| [Not Null](not-null.html) | Use [`ALTER COLUMN`](alter-column.html#remove-not-null-constraint) | |
| 86 | +| [Primary Key](primary-key.html) | Primary Keys cannot be removed. However, you can move the table's data to a new table with [this process](#table-migrations-to-add-or-change-immutable-constraints). | |
| 87 | +| [Unique](unique.html) | The Unique constraint cannot be dropped directly. However, you can use [`DROP INDEX`](drop-index.html) to remove the index automatically created by the Unique constraint (whose name ends in `_key`) to remove the constraint. | |
| 88 | + |
| 89 | +### Change Constraints |
| 90 | + |
| 91 | +The procedure for changing a constraint depends on its type: |
| 92 | + |
| 93 | +| Constraint Type | Procedure | |
| 94 | +|-----------------|-----------| |
| 95 | +| [Check](check.html) | [Issue a transaction](transactions.html#syntax) that adds a new Check constraint ([`ADD CONSTRAINT`](add-constraint.html)), and then remove the existing one ([`DROP CONSTRAINT`](drop-constraint.html)). | |
| 96 | +| [Default Value](default-value.html) | The Default Value can be changed through [`ALTER COLUMN`](alter-column.html). | |
| 97 | +| [Foreign Keys](foreign-key.html) | [Issue a transaction](transactions.html#syntax) that adds a new Foreign Key constraint ([`ADD CONSTRAINT`](add-constraint.html)), and then remove the existing one ([`DROP CONSTRAINT`](drop-constraint.html)). | |
| 98 | +| [Not Null](not-null.html) | The Not Null constraint cannot be changed, only removed. However, you can move the table's data to a new table with [this process](#table-migrations-to-add-or-change-immutable-constraints). | |
| 99 | +| [Primary Key](primary-key.html) | Primary Keys cannot be modified. However, you can move the table's data to a new table with [this process](#table-migrations-to-add-or-change-immutable-constraints). | |
| 100 | +| [Unique](unique.html) | [Issue a transaction](transactions.html#syntax) that adds a new Unique constraint ([`ADD CONSTRAINT`](add-constraint.html)), and then remove the existing one ([`DROP CONSTRAINT`](drop-constraint.html)). | |
| 101 | + |
| 102 | +#### Table Migrations to Add or Change Immutable Constraints |
| 103 | + |
| 104 | +If you want to make a change to an immutable constraint, you can use the following process: |
| 105 | + |
| 106 | +1. [Create a new table](create-table.html) with the constraints you want to apply. |
| 107 | +2. Move the data from the old table to the new one using [`INSERT` from a `SELECT` statement](insert.html#insert-from-a-select-statement). |
| 108 | +3. [Issue a transaction](transactions.html#syntax) that [drops the old table](drop-table.html), and then [renames the new table to the old name](rename-table.html). |
23 | 109 |
|
24 | 110 | ## See Also
|
25 |
| -- [`ALTER TABLE`](alter-table.html) |
26 |
| -- [`SHOW CONSTRAINTS`](show-constraints.html) |
| 111 | + |
27 | 112 | - [`CREATE TABLE`](create-table.html)
|
| 113 | +- [`ADD CONSTRAINT`](add-constraint.html) |
| 114 | +- [`DROP CONSTRAINT`](drop-constraint.html) |
| 115 | +- [`SHOW CONSTRAINTS`](show-constraints.html) |
| 116 | +- [`SHOW CREATE TABLE`](show-create-table.html) |
0 commit comments