postgreSQL 检查约束和 null

postgreSQL check constraint and null

我创建了一个表 "TEST" 并尝试输入一些数据,但出现错误。错误是错误:关系 "test" 的新行违反检??查约束 "test_status_check" 详细信息:失败行包含 (5 , 2015-07-21, 15:00:00, I7 , 9 , NULL, NULL) .
我认为这是因为状态为空。因此,我尝试在测试表中放置一个空值,但仍然无法正常工作

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE test(
    clientID CHAR (20),
    startDate DATE,
    startTime TIME,
    instructorNO CHAR(20),
    centreID  CHAR(20),
    STATUS CHAR(4) CHECK (STATUS IN ('Fail','Pass')) NULL,
    reason VARCHAR(400),

    omitted...
    )

错误:关系 "test" 的新行违反检??查约束 "test_status_check" 详细信息:失败行包含 (5 , 2015-07-21, 15:00:00, I7 , 9 , NULL, NULL) .


编辑:我完全改变了主意。您现有的代码是有效的(并且 NULL 部分是不必要的)。

根据文档,

It should be noted that a check constraint is satisfied if the check expression evaluates to true or the null value. Since most expressions will evaluate to the null value if any operand is null, they will not prevent null values in the constrained columns. To ensure that a column does not contain null values, the not-null constraint described in the next section can be used.

所以,有些事情搞砸了。有关您的原始代码工作的示例,请参见此处。