我有一张这样的桌子:
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 |
+--------------------+-------------------+-----------------+------------------+------------+-----------------+
答案 0 :(得分:2)
CREATE TABLE :: Constraint Expressions
...
MariaDB 10.2.1引入了两种定义约束的方法:
- CHECK(expression)作为列定义的一部分给出。
- CONSTRAINT [constraint_name]检查(表达式)
...
如果您使用第一种形式定义约束(列约束),则可以使用MODIFY COLUMN
将其删除:
ALTER TABLE `test`
MODIFY COLUMN `height` INT(10);
如果使用第二种形式(表约束),则可以使用DROP CONSTRAINT
将其删除:
ALTER TABLE `test`
DROP CONSTRAINT `height`;
请参见dbfiddle。