Skip to content

Commit

Permalink
sql: add switch for check constraint (#13998) (#14016)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jun 28, 2023
1 parent 60fdb96 commit e185bee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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`.
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 @@ -128,6 +132,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 is added successfully, and TiDB ignores 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 reports an error.

## 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 @@ -1615,6 +1615,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
- 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

0 comments on commit e185bee

Please sign in to comment.