Bug #121498 DROP COLUMN silently drops CHECK constraints that use no column
Submitted: 13 Oct 17:00
Reporter: CHING HSIN CHANG Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: DDL Severity:S3 (Non-critical)
Version:8.4.11, 9.7.2, 26.7.0 OS:Linux (official Docker images mysql:8.4.11, mysql:9.7.2, mysql:26.7.0)
Assigned to: CPU Architecture:x86
Tags: ALTER TABLE, check constraint, drop column

[13 Oct 17:00] CHING HSIN CHANG
Description:
A table has a CHECK constraint whose expression uses no column, such as CHECK (1 = 0) or CHECK (true). ALTER TABLE ... DROP COLUMN drops this constraint without an error or warning, even when the dropped column has nothing to do with it. Afterwards the table accepts rows that the constraint rejected before. ADD COLUMN, RENAME COLUMN and MODIFY keep the constraint. ALGORITHM=INPLACE and ALGORITHM=COPY behave the same. A column-level constant CHECK (b INT CHECK (1 = 0)) is also dropped when column a is dropped.

The manual (ALTER TABLE, "CHECK constraints") says: "If a CHECK constraint refers only to a single column, dropping the column automatically drops the constraint." A constraint that uses no column does not refer to the dropped column.

Expected: the constraint is kept, and the last INSERT below fails with ERROR 3819.

How to repeat:
CREATE TABLE t (a INT, b INT, CONSTRAINT ck CHECK (1 = 0));
INSERT INTO t VALUES (1, 1);    -- ERROR 3819 (HY000): Check constraint 'ck' is violated.
ALTER TABLE t ADD COLUMN c INT;
SELECT CONSTRAINT_NAME FROM information_schema.CHECK_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE();  -- ck
ALTER TABLE t DROP COLUMN b;    -- OK, no warning
SELECT CONSTRAINT_NAME FROM information_schema.CHECK_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE();  -- empty
INSERT INTO t VALUES (1, 1);    -- OK, 1 row inserted