2015-04-07 50 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上的我:\ –