文章详情页
MySQL的check约束中可以包含子查询吗
浏览:21日期:2022-06-21 09:44:00
问题描述
我写了一句SQL如下:
create table if not exists Teachings( teacher varchar(255), classid varchar(32) references Class(id) check(teacher in (select username from Users where role = ’teacher’)));
但是在执行的时候报错(MySQL版本5.6.30):
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ’check(teacher in (select username from Users where role = ’teacher’)))’ at line 4
是不是MySQL中的check约束不能包含子查询呢?如果是,我的这句SQL应该怎么实现呢?
PS:完整的SQL见这里
问题解答
回答1:DDL漏了个逗号
MySQL并不支持CHECK约束,参见MySQL文档
可以用触发器来实现
相关文章:
排行榜
![基于Nginx的Wordpress安装失败?](http://www.haobala.com/attached/image/news/202208/112305819e.png)