我有一个由以下SQL定义的表:
CREATE TABLE test (
id integer PRIMARY KEY NOT NULL UNIQUE,
status text NOT NULL,
enddate date,
/* Checks */
CHECK (status IN ("Current", "Complete"))
);
我想使用CHECK添加一个约束,它要求enddate
为非空**,**如果status
为“Complete”。
这可能吗?我使用的是SQLite v3.6.16。
3条答案
按热度按时间mzaanser1#
怎么样:
xmd2e60i2#
没有什么可以阻止您在一个表上有多个
CHECK
约束。IMO是最简单和最容易扩展的解决方案:这利用了 if A then B 在逻辑上等价于 either not A or B 的事实。
sxpgvts33#
这将在SQLite中工作,其中
CHECK
约束是内联编写的。我将双引号改为撇号,以便它可以在PHP中使用。