首页 > MySQL的check约束中可以包含子查询吗

MySQL的check约束中可以包含子查询吗

我写了一句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漏了个逗号

  2. MySQL并不支持CHECK约束,参见MySQL文档

  3. 可以用触发器来实现

【热门文章】
【热门文章】