如何使用OR条件定义检查约束

时间:2014-09-11 01:29:48

标签: sql oracle

我只想在表格中插入数据时限制player_position中的值。

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 个答案:

答案 0 :(得分: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)
);

您应该考虑是否应该将这些值放在查找表中并改为定义外键。在这种特定情况下可能没有必要,但通常在检查约束中有很多值是设计气味。