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

Add more tests for CHECK constraints #351

Open
morgo opened this issue Oct 17, 2024 · 1 comment
Open

Add more tests for CHECK constraints #351

morgo opened this issue Oct 17, 2024 · 1 comment

Comments

@morgo
Copy link
Collaborator

morgo commented Oct 17, 2024

CHECK constraints have a schema-level namespace, so (using an example roughly from the MySQL manual) this will fail:

DROP TABLE IF EXISTS t1, _t1_new;
CREATE TABLE t1 (
  id INT NOT NULL PRIMARY KEY auto_increment,
  CHECK (c1 <> c2),
  c1 INT CHECK (c1 > 10),
  c2 INT CONSTRAINT c2_positive CHECK (c2 > 0),
  c3 INT CHECK (c3 < 100),
  CONSTRAINT c1_nonzero CHECK (c1 <> 0),
  CHECK (c1 > c3)
);
CREATE TABLE _t1_new (
  id INT NOT NULL PRIMARY KEY auto_increment,
  CHECK (c1 <> c2),
  c1 INT CHECK (c1 > 10),
  c2 INT CONSTRAINT c2_positive CHECK (c2 > 0),
  c3 INT CHECK (c3 < 100),
  CONSTRAINT c1_nonzero CHECK (c1 <> 0),
  CHECK (c1 > c3)
);

..
ERROR 3822 (HY000): Duplicate check constraint name 'c2_positive'.

However, this doesn't fail in spirit. This is because to create the new table we use the following syntax:

DROP TABLE IF EXISTS t1, _t1_new;
CREATE TABLE t1 (
  id INT NOT NULL PRIMARY KEY auto_increment,
  CHECK (c1 <> c2),
  c1 INT CHECK (c1 > 10),
  c2 INT CONSTRAINT c2_positive CHECK (c2 > 0),
  c3 INT CHECK (c3 < 100),
  CONSTRAINT c1_nonzero CHECK (c1 <> 0),
  CHECK (c1 > c3)
);
CREATE TABLE _t1_new LIKE t1;
..
Query OK, 0 rows affected (0.01 sec)

This is because MySQL renames the constraints in CREATE TABLE.. LIKE:

mysql> SHOW CREATE TABLE _t1_new\G
*************************** 1. row ***************************
       Table: _t1_new
Create Table: CREATE TABLE `_t1_new` (
  `id` int NOT NULL AUTO_INCREMENT,
  `c1` int DEFAULT NULL,
  `c2` int DEFAULT NULL,
  `c3` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `_t1_new_chk_1` CHECK ((`c1` <> 0)),
  CONSTRAINT `_t1_new_chk_2` CHECK ((`c2` > 0)),
  CONSTRAINT `_t1_new_chk_3` CHECK ((`c1` <> `c2`)),
  CONSTRAINT `_t1_new_chk_4` CHECK ((`c1` > 10)),
  CONSTRAINT `_t1_new_chk_5` CHECK ((`c3` < 100)),
  CONSTRAINT `_t1_new_chk_6` CHECK ((`c1` > `c3`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

Since it's not guaranteed we will always use CREATE TABLE.. LIKE (although I see no reason to switch), we should have tests to ensure this case is handled correctly.

@chikitai

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants