Skip to content

CHECK CONSTRAINT loss when ENFORCED again #47567

@shawn0915

Description

@shawn0915

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

mysql> show create table t\G
*************************** 1. row ***************************
       Table: t
Create Table: CREATE TABLE `t` (
  `a` int(11) DEFAULT NULL,
CONSTRAINT `t_chk_1` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t_chk_2` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t_chk_3` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
CONSTRAINT `check_1` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t_chk_4` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.00 sec)

mysql> insert t select 1;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t;
+------+
| a    |
+------+
|    2 |
|    2 |
|    1 |
+------+
3 rows in set (0.01 sec)

mysql> ALTER TABLE t ALTER CONSTRAINT t_chk_1 ENFORCED;
ERROR 3819 (HY000): Check constraint 't_chk_1' is violated.
mysql> select * from t;
+------+
| a    |
+------+
|    2 |
|    2 |
|    1 |
+------+
3 rows in set (0.01 sec)

mysql> show create table t\G
*************************** 1. row ***************************
       Table: t
Create Table: CREATE TABLE `t` (
  `a` int(11) DEFAULT NULL,
CONSTRAINT `t_chk_2` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t_chk_3` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
CONSTRAINT `check_1` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t_chk_4` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.00 sec)

mysql> select * from information_schema.CHECK_CONSTRAINTS;
+--------------------+-------------------+-----------------+--------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | CHECK_CLAUSE |
+--------------------+-------------------+-----------------+--------------+
| def                | sbtest            | check_1         | (1 < `a`)    |
| def                | sbtest            | t_chk_2         | (1 < `a`)    |
| def                | sbtest            | t_chk_3         | (1 < `a`)    |
| def                | sbtest            | check_1         | (1 < `a`)    |
| def                | sbtest            | t_chk_4         | (1 < `a`)    |
+--------------------+-------------------+-----------------+--------------+
5 rows in set (0.00 sec)

mysql>

2. What did you expect to see? (Required)

t_chk_1 should be keep NOT ENFORCED, but not loss.

and, in mysql, the logic is right.

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.1.0     |
+-----------+
1 row in set (0.00 sec)

mysql> ALTER TABLE t ALTER CONSTRAINT t_chk_1 ENFORCED;
ERROR 3819 (HY000): Check constraint 't_chk_1' is violated.
mysql> select * from t;
+------+
| a    |
+------+
|    1 |
|    2 |
|    2 |
+------+
3 rows in set (0.00 sec)

mysql> show create table t\G
*************************** 1. row ***************************
       Table: t
Create Table: CREATE TABLE `t` (
  `a` int DEFAULT NULL,
  CONSTRAINT `check_1` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
  CONSTRAINT `t_chk_1` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
  CONSTRAINT `t_chk_2` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
  CONSTRAINT `t_chk_3` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */,
  CONSTRAINT `t_chk_4` CHECK ((1 < `a`)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin
1 row in set (0.00 sec)

mysql> select * from information_schema.CHECK_CONSTRAINTS;
+--------------------+-------------------+-----------------+--------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | CHECK_CLAUSE |
+--------------------+-------------------+-----------------+--------------+
| def                | sbtest            | t_chk_1         | (1 < `a`)    |
| def                | sbtest            | t_chk_2         | (1 < `a`)    |
| def                | sbtest            | t_chk_3         | (1 < `a`)    |
| def                | sbtest            | check_1         | (1 < `a`)    |
| def                | sbtest            | t_chk_4         | (1 < `a`)    |
+--------------------+-------------------+-----------------+--------------+
5 rows in set (0.00 sec)

3. What did you see instead (Required)

4. What is your TiDB version? (Required)

Release Version: v7.5.0-alpha

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions