假设我有两张桌子:
info(id PRIMARY KEY, opendate NOT NULL, closedate)
fileinfo(fileno PRIMARY KEY, id FOREIGN KEY REFERENCES info.id)
仅当info.closedate的对应值为null时,我才允许将新条目放入fileinfo。有没有办法做到这一点?
答案 0 :(得分:0)
您可以将一个触发器附加到fileinfo表,在INSERT上运行,它会查询关闭的信息,如果它不为空,则会抛出错误。
编辑:添加了一个SQLServer触发器的例子,它可以做同样的事情。编辑为MySQL语法要求。
CREATE TRIGGER InsertFileInfo
ON fileinfo
AFTER INSERT
AS
BEGIN
declare @closedate DateTime ;
select @closedate = closedate from info where id in (select id from new);
if @closedate is not null
begin
raiserror ('CloseDate is not null', 16, 1);
end
END