Skip to content

Commit

Permalink
Document auto-validation of foreign keys
Browse files Browse the repository at this point in the history
Fixes #4891.
  • Loading branch information
jseldess committed Oct 28, 2019
1 parent ebbf829 commit 14e68c9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion v19.2/add-constraint.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The user must have the `CREATE` [privilege](authorization.html#assign-privileges
-----------|-------------
`table_name` | The name of the table containing the column you want to constrain.
`constraint_name` | The name of the constraint, which must be unique to its table and follow these [identifier rules](keywords-and-identifiers.html#identifiers).
`constraint_elem` | The [`CHECK`](check.html), [foreign key](foreign-key.html), [`UNIQUE`](unique.html) constraint you want to add. <br/><br/>Adding/changing a `DEFAULT` constraint is done through [`ALTER COLUMN`](alter-column.html). <br/><br/>Adding/changing the table's `PRIMARY KEY` is not supported through `ALTER TABLE`; it can only be specified during [table creation](create-table.html).
`constraint_elem` | The [`CHECK`](check.html), [foreign key](foreign-key.html), [`UNIQUE`](unique.html) constraint you want to add.<br/><br/>Adding/changing a `DEFAULT` constraint is done through [`ALTER COLUMN`](alter-column.html).<br/><br/>Adding/changing the table's `PRIMARY KEY` is not supported through `ALTER TABLE`; it can only be specified during [table creation](create-table.html).<br><br><span class="version-tag">New in v19.2:</span> Applying a foreign key constraint now validates existing rows in addition to enforcing conformance for new rows. As such, it is no longer necessary to use [`VALIDATE CONSTRAINT`](validate-constraint.html) for foreign keys.

## Viewing schema changes

Expand Down
12 changes: 7 additions & 5 deletions v19.2/validate-constraint.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ toc: true

The `VALIDATE CONSTRAINT` [statement](sql-statements.html) is part of `ALTER TABLE` and checks whether values in a column match a [constraint](constraints.html) on the column. This statement is especially useful after applying a constraint to an existing column via [`ADD CONSTRAINT`](add-constraint.html). In this case, `VALIDATE CONSTRAINT` can be used to find values already in the column that do not match the constraint.

<span class="version-tag">New in v19.2:</span> Applying a foreign key constraint via `ADD CONSTRAINT` now validates existing rows in addition to enforcing conformance for new rows. As such, it is no longer necessary to use [`VALIDATE CONSTRAINT`](validate-constraint.html) for foreign keys.

{% include {{ page.version.version }}/sql/combine-alter-table-commands.md %}

## Required privileges
Expand All @@ -29,20 +31,20 @@ The user must have the `CREATE` [privilege](authorization.html#assign-privileges

{% include {{ page.version.version }}/misc/schema-change-view-job.md %}

## Examples
## Example

In [`ADD CONSTRAINT`](add-constraint.html), we [added a foreign key constraint](add-constraint.html#add-the-foreign-key-constraint-with-cascade) like so:
In [`ADD CONSTRAINT`](add-constraint.html), we [added a `CHECK` constraint](add-constraint.html#add-the-check-constraint) like so:

{% include copy-clipboard.html %}
~~~ sql
> ALTER TABLE orders ADD CONSTRAINT customer_fk FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE;
> ALTER TABLE orders ADD CONSTRAINT check_id_non_zero CHECK (id > 0);
~~~

In order to ensure that the data added to the `orders` table prior to the creation of the `customer_fk` constraint conforms to that constraint, run the following:
In order to ensure that the data added to the `orders` table prior to the creation of the `check_id_non_zer` constraint conforms to that constraint, run the following:

{% include copy-clipboard.html %}
~~~ sql
> ALTER TABLE orders VALIDATE CONSTRAINT customer_fk;
> ALTER TABLE orders VALIDATE CONSTRAINT check_id_non_zero;
~~~

{{site.data.alerts.callout_info}}
Expand Down

0 comments on commit 14e68c9

Please sign in to comment.