2014-09-11 69 views
-2

我只是想在表中插入數據時限制player_position中的值。如何使用OR條件來定義檢查約束

Create Table PLAYERS 
(Player_Id NUMBER(4),Player_First_Name VARCHAR(15),Player_Last_Name VARCHAR(15),Player_Team_Code CHAR(3), 
Player_Position CHAR(2), 
CONSTRAINT Player_Position_check CHECK (Player_position = 'QB' or 'RB' or 'WR' or 'DB' or 'LB' or 'OT' or 'DT' or 'PT' or 'KI') 
CONSTRAINT Player_Id_PK PRIMARY KEY (Player_Id)); 

這段代碼有什麼問題?

回答

1

您需要使用正確的列表語法,這是一個IN cluase:

Create Table PLAYERS (
    Player_Id NUMBER(4), 
    Player_First_Name VARCHAR(15), 
    Player_Last_Name VARCHAR(15), 
    Player_Team_Code CHAR(3), 
    Player_Position CHAR(2), 
    CONSTRAINT Player_Position_check 
     CHECK (Player_position in ('QB', 'RB', 'WR', 'DB', 'LB', 'OT', 'DT', 'PT', 'KI')), 
    CONSTRAINT Player_Id_PK PRIMARY KEY (Player_Id) 
); 

你應該考慮是否你應該把這些值在查找表,並且定義了一個外鍵代替。在這個特定情況下可能沒有必要,但通常在檢查約束中有很多值是設計氣味。