Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add switch for check constraint #13998

Merged
merged 6 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Query OK, 1 row affected (0.03 sec)

## CHECK

> **Note:**
>
> The 'CHECK' constraint feature is disabled by default. To enable it, you need to set the [`tidb_enable_check_constraint`](/system-variables.md#tidb_enable_check_constraint-new-in-v720) variable to `ON`.
qiancai marked this conversation as resolved.
Show resolved Hide resolved

qiancai marked this conversation as resolved.
Show resolved Hide resolved
A `CHECK` constraint restricts the values of a column in a table to meet your specified conditions. When the `CHECK` constraint is added to a table, TiDB checks whether the constraint is satisfied during the insertion or updates of data into the table. If the constraint is not met, an error is returned.

The syntax for the `CHECK` constraint in TiDB is the same as that in MySQL:
Expand Down Expand Up @@ -129,6 +133,11 @@ In addition to specifying `[NOT] ENFORCED` when adding the constraint, you can a
ALTER TABLE t ALTER CONSTRAINT c1 NOT ENFORCED;
```

### MySQL compatibility

- It is not supported to add a `CHECK` constraint while adding a column (for example, `ALTER TABLE t ADD COLUMN a CHECK(a > 0)`). In this case, only the column will be added successfully, and TiDB will ignore the `CHECK` constraint without reporting any error.
- It is not supported to use `ALTER TABLE t CHANGE a b int CHECK(b > 0)` to add a `CHECK` constraint. When this statement is executed, TiDB will report an error.
qiancai marked this conversation as resolved.
Show resolved Hide resolved

## UNIQUE KEY

Unique constraints mean that all non-null values in a unique index and a primary key column are unique.
Expand Down
8 changes: 8 additions & 0 deletions system-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,14 @@ mysql> SELECT job_info FROM mysql.analyze_jobs ORDER BY end_time DESC LIMIT 1;
- Default value: `OFF`
- This variable is used to control whether to enable the cascades planner.

### tidb_enable_check_constraint <span class="version-mark">New in v7.2.0</span>

- Scope: GLOBAL
fzzf678 marked this conversation as resolved.
Show resolved Hide resolved
- Persists to cluster: Yes
- Type: Boolean
- Default value: `OFF`
- This variable is used to control whether to enable the [`CHECK` constraint](/constraints.md#check) feature.

### tidb_enable_chunk_rpc <span class="version-mark">New in v4.0</span>

- Scope: SESSION
Expand Down