[MDEV-30899] Fail to drop CHECK constraint Created: 2023-03-22  Updated: 2024-02-08

Status: In Review
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.4, 10.11.2, 10.5, 10.6, 10.7, 10.8, 10.9, 10.10, 10.11
Fix Version/s: 10.4, 10.5, 10.6, 10.11

Type: Bug Priority: Major
Reporter: John Jove Assignee: Sergei Golubchik
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Blocks
is blocked by MDEV-24598 duplicate CHECK constraint name In Review

 Description   

Run the following statements, you will see the last statement fails to drop the CHECK constraint, and reports an error ` Can't DROP CONSTRAINT `c1`; check that it exists`

DROP TABLE IF EXISTS t0;
CREATE TABLE t0 (c1 INT CHECK ( c1 > 0 ));
SELECT CONSTRAINT_NAME, TABLE_NAME, CONSTRAINT_TYPE FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_NAME = 't0' AND TABLE_SCHEMA = 'test';
ALTER TABLE t0 DROP CONSTRAINT c1;



 Comments   
Comment by Alice Sherepa [ 2023-03-23 ]

Thanks for the report, it should be fixed.
Currently it works like this - there are 2 ways to define a constraint:

CHECK(expression) given as part of a column definition – to drop it one should use ALTER TABLE ..MODIFY COLUMN.
CONSTRAINT constraint_name CHECK (expression) – here ALTER TABLE .. DROP CONSTRAINT ;

MariaDB [test]> CREATE TABLE t0 (c1 INT CHECK ( c1 > 0 ));
Query OK, 0 rows affected (0,054 sec)
 
MariaDB [test]> show create table t0;
+-------+-----------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                      |
+-------+-----------------------------------------------------------------------------------------------------------------------------------+
| t0    | CREATE TABLE `t0` (
  `c1` int(11) DEFAULT NULL CHECK (`c1` > 0)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs |
+-------+-----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,001 sec)
 
MariaDB [test]> ALTER TABLE t0 MODIFY COLUMN c1 INT ;
Query OK, 0 rows affected (0,037 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [test]> show create table t0;
+-------+------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                     |
+-------+------------------------------------------------------------------------------------------------------------------+
| t0    | CREATE TABLE `t0` (
  `c1` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs |
+-------+------------------------------------------------------------------------------------------------------------------+
1 row in set (0,003 sec)
 
MariaDB [test]> CREATE or replace TABLE t0 (c1 INT, CHECK ( c1 > 0 ));
Query OK, 0 rows affected (0,059 sec)
 
MariaDB [test]> show create table t0;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                   |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t0    | CREATE TABLE `t0` (
  `c1` int(11) DEFAULT NULL,
  CONSTRAINT `CONSTRAINT_1` CHECK (`c1` > 0)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,001 sec)
 
MariaDB [test]> ALTER TABLE t0 DROP CONSTRAINT CONSTRAINT_1;
Query OK, 0 rows affected (0,031 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [test]> show create table t0;
+-------+------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                     |
+-------+------------------------------------------------------------------------------------------------------------------+
| t0    | CREATE TABLE `t0` (
  `c1` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs |
+-------+------------------------------------------------------------------------------------------------------------------+
1 row in set (0,001 sec)

Comment by John Jove [ 2023-03-23 ]

Thanks for your generous reply. I'll follow your advice to improve my tool.

Generated at Thu Feb 08 10:19:44 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.