oracle 错误:ORA-02270:此列列表没有匹配的唯一键或主键

q8l4jmvw  于 2023-05-06  发布在  Oracle
关注(0)|答案(1)|浏览(219)

当我运行我的脚本时,我一直在名册表上得到上面的错误,请问我做错了什么?有什么建议吗?

CREATE TABLE SHIFT (
    Shift_ID NUMBER(5),
    Shift_Type VARCHAR2(20), 
    Start_Time VARCHAR2(10), 
    End_Time VARCHAR2(10),
    CONSTRAINT PK_SHIFT PRIMARY KEY (Shift_ID, Start_Time, End_Time)
    -- CONSTRAINT CHECK_SHIFT_TYPE CHECK (SHIFT_Type IN ('Day', 'Night'))
    -- CONSTRAINT CHECK_TOTAL_HOURS CHECK (Total_Hours IN (6, 8, 12))
);
CREATE TABLE ROSTER(
    Roster_ID NUMBER(5),
    Shift_Date DATE,
    Shift_End_Date DATE,
    Total_Hours NUMBER(2),
    Shift_ID NUMBER(5) REFERENCES SHIFT(Shift_ID),
    Start_Time VARCHAR2(10) REFERENCES SHIFT(Start_Time),
    End_Time VARCHAR2(10) REFERENCES SHIFT(End_Time),
    Ward_ID NUMBER(5) REFERENCES WARD (Ward_ID),
    Ward_Name VARCHAR2(25) REFERENCES WARD (Ward_Name),
    CONSTRAINT PK_ROSTER PRIMARY KEY (Roster_ID)
);
ngynwnxp

ngynwnxp1#

如果你需要创建一个包含多个列的外键,或者希望命名它,你不能使用列级语法...你必须在最后添加一个CONSTRAINT,像这样:

CREATE TABLE ROSTER(
    Roster_ID NUMBER(5),
    Shift_Date DATE,
    Shift_End_Date DATE,
    Total_Hours NUMBER(2),
    Shift_ID NUMBER(5),
    Start_Time VARCHAR2(10),
    End_Time VARCHAR2(10),
    Ward_ID NUMBER(5),
    Ward_Name VARCHAR2(25),
    CONSTRAINT PK_ROSTER PRIMARY KEY (Roster_ID),
    CONSTRAINT FK_ROSTER_SHIFT FOREIGN KEY (Shift_ID, Start_Time, End_Time) REFERENCES SHIFT(Shift_ID, Start_Time, End_Time)
);

相关问题