MYSQL 约束 - 多重检查
好吧,我在这里为我正在处理的数据库编写一些约束,我想知道我是否可以执行以下操作:
ALTER TABLE Course
ADD CONSTRAINT cs_level
CHECK (clevel in ('P','I','II','III'))
...而不是这样:
ALTER TABLE Course
ADD CONSTRAINT cs_level
CHECK (clevel = 'P' OR clevel = 'I' OR clevel = 'II' OR clevel = 'III')
Well i am here writing some constraints for a db I am working on and i was wondering if I could do the following:
ALTER TABLE Course
ADD CONSTRAINT cs_level
CHECK (clevel in ('P','I','II','III'))
...instead of this:
ALTER TABLE Course
ADD CONSTRAINT cs_level
CHECK (clevel = 'P' OR clevel = 'I' OR clevel = 'II' OR clevel = 'III')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即使 MySQL 强制执行
CHECK
约束,我仍然会创建一个COURSE_LEVEL
表:COURSE_LEVEL_CODE
、VARCHAR(3)、主键COURSE_LEVEL_DESCRIPTION
、VARCHAR(120)...并在
COURSE
表的clevel
列上创建 FOREIGN KEY 约束:Even if MySQL enforced
CHECK
constraints, I'd still be creating aCOURSE_LEVEL
table:COURSE_LEVEL_CODE
, VARCHAR(3), primary keyCOURSE_LEVEL_DESCRIPTION
, VARCHAR(120)...and create a FOREIGN KEY constraint on the
COURSE
table,clevel
column:不幸的是,MySQL 不支持 CHECK 约束。它们被解析但被忽略。
来自参考手册:
您可以尝试在插入行之前将此检查约束放入业务逻辑中。
正如Joe Celko在他的书中Joe Celko 的 Smarties SQL :
确实有点苛刻,但在某种程度上他是正确的,因为 MySQL 不支持一堆标准SQL 功能。
Unfortunately, MySQL does not support the CHECK-constraints. They are parsed but ignored.
From the reference manual:
You can try to put this check constraint in your business logic before inserting the row.
As Joe Celko says in his book Joe Celko's SQL for Smarties:
A little harsh indeed, but in a way he's correct, since MySQL doesn't support a bunch of Standard SQL features.
MySQL只支持外键约束,不支持检查约束。无论您从哪里获得该语法,都不是 MySQL 手册。
MySQL only supports foreign key constraints, not check constraints. Wherever you got that syntax from, it wasn't the MySQL manual.