MariaDB 10.4.6 drop字段级CHECK约束问题

MariaDB 10.4.6 drop field level CHECK constraint problem

我有一个 table 这样的:

CREATE TABLE test (
    height int(10) CHECK(height>5)
);

当我尝试通过以下方式删除 check constraint 时:

ALTER TABLE test DROP CONSTRAINT height;

我收到此错误消息:

ERROR 1091 (42000): Can't DROP CONSTRAINT `height`; check that it exists

这是 SHOW CREATE TABLE test; 命令的输出:

+-------+-------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                      
|
+-------+-------------------------------------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
`height` int(10) DEFAULT NULL CHECK (`height` > 5)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------+

这里是 SELECT * from information_schema.table_constraints where TABLE_NAME = 'test'; 输出:

+--------------------+-------------------+-----------------+------------------+------------+-----------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA     | TABLE_NAME | CONSTRAINT_TYPE |
+--------------------+-------------------+-----------------+------------------+------------+-----------------+
| def                | test_db           | height          | test_db          | test       | CHECK           |
+--------------------+-------------------+-----------------+------------------+------------+-----------------+

CREATE TABLE :: Constraint Expressions

...

MariaDB 10.2.1 introduced two ways to define a constraint:

  • CHECK(expression) given as part of a column definition.
  • CONSTRAINT [constraint_name] CHECK (expression)

...

如果您使用第一种形式(列约束)定义约束,您可以使用 MODIFY COLUMN:

删除它
ALTER TABLE `test`
  MODIFY COLUMN `height` INT(10);

如果您使用第二种形式(table 约束),您可以使用 DROP CONSTRAINT:

删除它
ALTER TABLE `test`
  DROP CONSTRAINT `height`;

dbfiddle