约束检查Oracle SQL

时间:2015-10-17 22:42:19

标签: oracle-sqldeveloper

我想知道如何确保SecondInstrument的值不等于PrimInstrument中的值,因为音乐家不能两次使用相同的乐器。到目前为止,这是我已经尝试过的,我得到了这个错误信息:

02438. 00000 -  "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.

有人可以帮我解决这个问题吗? ;)提前致谢。

Create table SessionMusician (

Musician_ID number constraint pkSessionMus_Mus_ID Primary Key,

StageName varchar2(30)constraint nnSessionMus_StageName Not NULL,

RealName varchar2(30)constraint nnSessionMus_RealName Not NULL,

PrimInstrument varchar2(30) constraint nnSessionMus_PrimInstrumant Not Null,

SecondInstrument varchar2(30)  Default 'N/A' Constraint
chk_SessionMus_Secinstrument check (SecondInstrument <> PrimInstrument),

CONSTRAINT SessionMusician_unique UNIQUE (StageName,RealName));

1 个答案:

答案 0 :(得分:0)

您需要将其设为out-of-line constraint

CREATE TABLE SessionMusician (
    Musician_ID       NUMBER        CONSTRAINT pkSessionMus_Mus_ID         PRIMARY KEY,
    StageName         VARCHAR2(30)  CONSTRAINT nnSessionMus_StageName      NOT NULL,
    RealName          VARCHAR2(30)  CONSTRAINT nnSessionMus_RealName       NOT NULL,
    PrimInstrument    VARCHAR2(30)  CONSTRAINT nnSessionMus_PrimInstrumant NOT NULL,
    SecondInstrument  VARCHAR2(30)  DEFAULT 'N/A',
    CONSTRAINT SessionMusician_unique       UNIQUE ( StageName, RealName ),
    CONSTRAINT chk_SessionMus_Secinstrument CHECK (SecondInstrument <> PrimInstrument)
);