ORA-02438:列检查约束不能引用其他列

时间:2018-04-10 09:14:38

标签: oracle

CREATE TABLE event (
event_id       NUMBER(6) NOT NULL check(event_id > 0),
event_title    VARCHAR2(100 CHAR) NOT NULL,
event_team     VARCHAR2(1 CHAR) default 'N' NOT NULL check(event_team 
IN('Y',  'N')),
no_per_team    NUMBER(2) default 1 NOT NULL check((event_team = 'N' AND 
no_per_team = 1) OR (event_team = 'Y' AND no_per_team > 1)),
event_gender   VARCHAR2(1) NOT NULL check(event_gender IN ('M', 'F')),
og_id          NUMBER(3) NOT NULL,
sport_id       NUMBER(3) NOT NULL
);

错误报告:

ORA-02438: Column check constraint cannot reference other columns
*Cause:    attempted to define a column check constraint that references
           another column.
*Action:   define it as a table check constraint.

2 个答案:

答案 0 :(得分:1)

在声明CHECK约束列时,您无法引用其他列。而是在表级定义它。

CREATE TABLE event (
event_id       NUMBER(6) NOT NULL check(event_id > 0),
event_title    VARCHAR2(100 CHAR) NOT NULL,
event_team     VARCHAR2(1 CHAR) default 'N' NOT NULL check(event_team 
IN('Y',  'N')),
no_per_team    NUMBER(2) default 1 NOT NULL ,
event_gender   VARCHAR2(1) NOT NULL check(event_gender IN ('M', 'F')),
og_id          NUMBER(3) NOT NULL,
sport_id       NUMBER(3) NOT NULL,
CONSTRAINT CHECK_NO_P check((event_team = 'N' AND 
no_per_team = 1) OR (event_team = 'Y' AND no_per_team > 1))
);

答案 1 :(得分:1)

将列检查约束移动到表检查约束(如Oracle告诉您的那样):

CREATE TABLE event
(
   event_id       NUMBER (6) NOT NULL CHECK (event_id > 0),
   event_title    VARCHAR2 (100 CHAR) NOT NULL,
   event_team     VARCHAR2 (1 CHAR)
                     DEFAULT 'N'
                     NOT NULL
                     CHECK (event_team IN ('Y', 'N')),
   no_per_team    NUMBER (2) DEFAULT 1 NOT NULL,
   event_gender   VARCHAR2 (1) NOT NULL CHECK (event_gender IN ('M', 'F')),
   og_id          NUMBER (3) NOT NULL,
   sport_id       NUMBER (3) NOT NULL,
   --
   CONSTRAINT ch_npt CHECK
      (   (    event_team = 'N'
           AND no_per_team = 1)
       OR (    event_team = 'Y'
           AND no_per_team > 1))
);