check约束:如果约束表中一个字段的值可以来自另外两张表中的字段?

来源:百度知道 编辑:UC知道 时间:2024/07/13 09:43:51
我想约束表Table表中的ID字段值可以来自与Table1中的no字段的值,也可以来自Table2中的no字段的值,怎么写?Table1中no字段为char(5),Table2中no字段为char(7),长度不一样,Table中ID字段为char(7)
现在好用了,但是怎么给出用户提示呢?

写个触发器可以实现你的要求

create trigger id_check on Table
for insert update
as
begin
if(select no from inserted) not in (select no from Table1 union select no from Table2)
rollback
end

--用这个 刚才那个单条数据插入时应该可以多条就不对了
这回应该可以了
create trigger id_check on Table
for insert,update
as
begin
if(select count(ins.no) from inserted ins left join Table1 t1 on ins.no=t1.no left join Table2 t2 on ins.no=t2.no where t1.no is null and t2.no is null)>0
rollback
print '提示的话'
end
不知道是否满足你的要求

触发器 存储过程都能实现 CHECK不太好办