Skip to content

Commit 40a4186

Browse files
authored
Merge pull request #964 from cockroachdb/copy-edit-constraints
copy editing constraint pages
2 parents 2b6e5b6 + 22c6f41 commit 40a4186

26 files changed

+286
-153
lines changed

_data/sidebar_doc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ entries:
232232
- title: Default Value
233233
url: /default-value.html
234234

235-
- title: Foreign Keys
235+
- title: Foreign Key
236236
url: /foreign-key.html
237237

238238
- title: Not Null

add-constraint.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ The `ADD CONSTRAINT` [statement](sql-statements.html) is part of `ALTER TABLE` a
1111
- [Unique](unique.html)
1212

1313
{{site.data.alerts.callout_info}}
14-
The <a href="primary-key.html">Primary Key</a> constraint can only be applied through <a href="create-table.html"><code>CREATE TABLE</code></a>. The <a href="default-value.html">Default</a> constraint is managed through <a href="alter-column.html"><code>ALTER COLUMN</code>.</a>
15-
{{site.data.alerts.end}}
14+
The <a href="primary-key.html">Primary Key</a> and <a href="not-null.html">Not Null</a> constraints can only be applied through <a href="create-table.html"><code>CREATE TABLE</code></a>. The <a href="default-value.html">Default</a> constraint is managed through <a href="alter-column.html"><code>ALTER COLUMN</code>.</a>{{site.data.alerts.end}}
1615

1716
<div id="toc"></div>
1817

alter-column.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ The user must have the `CREATE` [privilege](privileges.html) on the table.
2222

2323
| Parameter | Description |
2424
|-----------|-------------|
25-
| `table_name` | The name of the table with the column whose Default value you want to modify. |
25+
| `table_name` | The name of the table with the column whose Default Value you want to modify. |
2626
| `name` | The name of the column you want to modify. |
27-
| `a_expr` | The new Default value you want to use. |
27+
| `a_expr` | The new Default Value you want to use. |
2828

2929
## Examples
3030

3131
### Set or Change a Default Value
3232

33-
Setting the [Default value constraint](default-value.html) inserts the value when data's written to the table without explicitly defining the value for the column. If the column already has a Default value set, you can use this statement to change it.
33+
Setting the [Default Value constraint](default-value.html) inserts the value when data's written to the table without explicitly defining the value for the column. If the column already has a Default Value set, you can use this statement to change it.
3434

3535
The below example inserts the Boolean value `true` whenever you inserted data to the `subscriptions` table without defining a value for the `newsletter` column.
3636

@@ -40,7 +40,7 @@ The below example inserts the Boolean value `true` whenever you inserted data to
4040

4141
### Remove Default Constraint
4242

43-
If the column has a defined [Default value](default-value.html), you can remove the constraint, which means the column will no longer insert a value by default if one is not explicitly defined for the column.
43+
If the column has a defined [Default Value](default-value.html), you can remove the constraint, which means the column will no longer insert a value by default if one is not explicitly defined for the column.
4444

4545
``` sql
4646
> ALTER TABLE subscriptions ALTER COLUMN newsletter DROP DEFAULT;

check.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
---
22
title: Check Constraint
3-
summary: The Check constraint specifies that the column value must satisfy a Boolean expression.
3+
summary: The Check constraint specifies that values for the column in INSERT or UPDATE statements must satisfy a Boolean expression.
44
toc: false
55
---
66

7-
The Check [constraints](constraints.html) specifies that the column value must satisfy a Boolean expression.
7+
The Check [constraint](constraints.html) specifies that values for the column in [`INSERT`](insert.html) or [`UPDATE`](update.html) statements must return `TRUE` or `NULL` for a Boolean expression. If any values return `FALSE`, the entire statement is rejected.
88

99
<div id="toc"></div>
1010

1111
## Details
1212

13-
- Check constraints requires that the column values satisfy a Boolean expression within the constraint. The expression must evaluate to `TRUE` (or *NULL*) for every row affected by an `INSERT` or `UPDATE` statement. The DML statement will fail if the condition evaluates to `FALSE` for any row.
14-
- Check constraints may be specified at the column or table level and can reference other columns within the table. Internally, all column level Check constrints are converted to table level constraints so they can be handled in a consistent fashion.
15-
- You can have multiple Check constraints on a single column but ideally, for performance optimization, these should be combined using the logical operators. So, for example,
13+
- If you add a Check constraint to an existing table, existing values are not checked. However, any updates to those values will be.
14+
{{site.data.alerts.callout_info}}In the future we plan to expand the Check constraint to include a check on any existing values in the column.{{site.data.alerts.end}}
15+
- Check constraints may be specified at the column or table level and can reference other columns within the table. Internally, all column-level Check constraints are converted to table-level constraints so they can be handled consistently.
16+
- You can have multiple Check constraints on a single column but ideally, for performance optimization, these should be combined using the logical operators. For example
1617

1718
~~~ sql
1819
warranty_period INT CHECK (warranty_period >= 0) CHECK (warranty_period <= 24)
1920
~~~
2021

21-
should be specified as
22+
should be specified as:
2223

2324
~~~ sql
2425
warranty_period INT CHECK (warranty_period BETWEEN 0 AND 24)
2526
~~~
2627

2728
## Syntax
2829

29-
The syntax of the Check constraint depends on whether you want the constraint to use a [single column](#single-column-column-level) or [multiple columns](#multiple-column-table-level).
30+
Check constraints can be defined at the [table level](#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).
3031

31-
### Single Column (Column Level)
32+
{{site.data.alerts.callout_info}}You can also add the Check constraint to existing tables through <a href="/add-constraint.html#add-the-check-constraint"><code>ADD CONSTRAINT</code></a>.{{site.data.alerts.end}}
33+
34+
### Column Level
3235

3336
{% include sql/diagrams/check_column_level.html %}
3437

@@ -38,7 +41,7 @@ The syntax of the Check constraint depends on whether you want the constraint to
3841
| `column_name` | The name of the constrained column. |
3942
| `column_type` | The constrained column's [data type](data-types.html). |
4043
| `check_expr` | An expression that returns a Boolean value; if the expression evaluates to `FALSE`, the value cannot be inserted.|
41-
| `column_constraints` | Any other column-level [constraints](constraints.html) you want to apply. |
44+
| `column_constraints` | Any other column-level [constraints](constraints.html) you want to apply to this column. |
4245
| `column_def` | Definitions for any other columns in the table. |
4346
| `table_constraints` | Any table-level [constraints](constraints.html) you want to apply. |
4447

@@ -53,7 +56,7 @@ The syntax of the Check constraint depends on whether you want the constraint to
5356
);
5457
~~~
5558

56-
### Multiple Column (Table Level)
59+
### Table Level
5760

5861
{% include sql/diagrams/check_table_level.html %}
5962

@@ -63,12 +66,12 @@ The syntax of the Check constraint depends on whether you want the constraint to
6366
| `column_def` | Definitions for any other columns in the table. |
6467
| `name` | The name you want to use for the constraint, which must be unique to its table and follow these [identifier rules](keywords-and-identifiers.html#identifiers). |
6568
| `check_expr` | An expression that returns a Boolean value; if the expression evaluates to `FALSE`, the value cannot be inserted.|
66-
| `table_constraints` | Any table-level [constraints](constraints.html) you want to apply. |
69+
| `table_constraints` | Any other table-level [constraints](constraints.html) you want to apply. |
6770

6871
**Example**
6972

7073
~~~ sql
71-
> CREATE TABLE inventories(
74+
> CREATE TABLE inventories (
7275
product_id INT NOT NULL,
7376
warehouse_id INT NOT NULL,
7477
quantity_on_hand INT NOT NULL,
@@ -79,7 +82,7 @@ The syntax of the Check constraint depends on whether you want the constraint to
7982

8083
## Usage Example
8184

82-
Check constraints may be specified at the column or table level and can reference other columns within the table. Internally, all column level Check constrints are converted to table level constraints so they can be handled in a consistent fashion.
85+
Check constraints may be specified at the column or table level and can reference other columns within the table. Internally, all column-level Check constraints are converted to table-level constraints so they can be handled in a consistent fashion.
8386

8487
~~~ sql
8588
> CREATE TABLE inventories (
@@ -89,7 +92,7 @@ Check constraints may be specified at the column or table level and can referenc
8992
PRIMARY KEY (product_id, warehouse_id)
9093
);
9194

92-
> INSERT INTO inventories (product_id, warehouse_id, quantity_on_hand) VALUES (1, 2, -20);
95+
> INSERT INTO inventories (product_id, warehouse_id, quantity_on_hand) VALUES (1, 2, 0);
9396
~~~
9497
~~~
9598
pq: failed to satisfy CHECK constraint (quantity_on_hand > 0)
@@ -98,6 +101,7 @@ pq: failed to satisfy CHECK constraint (quantity_on_hand > 0)
98101
## See Also
99102

100103
- [Constraints](constraints.html)
104+
- [`DROP CONSTRAINT`](drop-constraint.html)
101105
- [Default Value constraint](default-value.html)
102106
- [Foreign Key constraint](foreign-key.html)
103107
- [Not Null constraint](not-null.html)

constraints.md

Lines changed: 104 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,116 @@
11
---
22
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.
44
toc: false
55
---
66

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.
88

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.
1010

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>
1212

13-
The different types of constraints are:
13+
## Supported Constraints
1414

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).
23109

24110
## See Also
25-
- [`ALTER TABLE`](alter-table.html)
26-
- [`SHOW CONSTRAINTS`](show-constraints.html)
111+
27112
- [`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

Comments
 (0)