2015-04-07 51 views
0

我创建了一个表“测试”,我试图输入一些数据,但我得到一个错误。错误是错误:关系“test”的新行违反检​​查约束“test_status_check”细节:失败的行包含(5,2015-07-21,15:00:00,I7,9,NULL,NULL)。 我认为这是因为状态为空。因此,我试图把一个空的测试表,但仍然没有工作postgreSQL检查约束和空

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... 
    ) 

ERROR:新行的关系“试验”违反了检查约束“test_status_check”详细信息:失败行包含(5,2015年7月21日,15:00:00,I7,9,NULL,NULL)。

+1

是这个mysql还是postegresql? – BK435

+0

Postgres 9.3接受你的语法,但是,真的是你想要的。 Postgres,MySQL和哪个版本? –

+0

我删除了MySQL标签,因为标题清楚地表示PostgreSQL(并且上次我检查了,'CHECK'约束在MySQL中无法使用)。 –

回答

1

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

the documentation

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.

所以,别的东西弄乱了。请参阅here以获取您的原始代码工作示例。

+0

@Bredan Long当我在表中输入一些数据时发生错误*查询失败:错误:新行关系“test”违反检查约束“test_status_check”详细信息:失败行包含(2,2015-04-29,10:00 :00,I2,1,NULL,NULL)。* –

+0

@nicholaslui你确定删除了旧约束吗? –

+0

嗯,奇怪..你原来的代码适用于PostgreSQL 9.3.6上的我:\ –