【发布时间】:2021-12-15 08:27:21
【问题描述】:
我有两张桌子,一张是学生桌子,一张是员工桌子:
create table student (
id int not null primary key
)
create table staff (
id int not null primary key
)
我希望每个中的 id 都是唯一的。我知道这在生产中不应该是这样,但我只是想看看为什么我的检查约束不起作用,我正在使用一个更简单的例子来解释。
然后我更改表格以包含以下检查:
alter table student add constraint not_staff check (id not in (select id from staff))
alter table staff add constraint not_student check (id not in (select id from student))
这些检查似乎无效。
我的问题是我们是否可以在检查约束中包含这些类型的 SQL 语句。如果是这样,为什么上述约束无效,我将如何解决它。
谢谢!
【问题讨论】:
-
创建一个执行检查的用户定义函数,并从表检查约束中调用它。或者,您可以使用数据库触发器。
标签: sql db2 constraints