SQL错误:ORA-02270:创建表时出错

时间:2016-09-22 00:04:20

标签: sql oracle foreign-keys primary-key

这些是我到目前为止的表,错误是表SESSIONS,我似乎无法找到错误是什么?我输错了主键和外键吗?

CREATE TABLE BRANCH(
BRANCHID CHAR(2) NOT NULL,
BRANCHNAME VARCHAR2(20),
BRANCHSUBURB VARCHAR2(15),
BRANCHPOSTCODE CHAR(4),
CONSTRAINT BRANCH_PK PRIMARY KEY(BRANCHID));

CREATE TABLE SCREEN(
CHAR(2) NOT NULL,
SCREENID CHAR(2) NOT NULL,
SCREENCAPACITY NUMBER(3),
CONSTRAINT SCREEN_PK PRIMARY KEY(BRANCHID, SCREENID),
CONSTRAINT SCREEN_FK FOREIGN KEY(BRANCHID) REFERENCES BRANCH(BRANCHID));

CREATE TABLE MOVIE(
MOVIEID CHAR(2) NOT NULL,
MOVIENAME VARCHAR2(20),
MOVIEGENRE VARCHAR2(20),
MOVIECLASSIFICATION VARCHAR2(5),
CONSTRAINT MOVIE_PK PRIMARY KEY(MOVIEID));

CREATE TABLE SESSIONS(
SESSIONID CHAR(4) NOT NULL,
BRANCHID CHAR(2) NOT NULL,
SCREENID CHAR(2) NOT NULL,
MOVIEID CHAR(2) NOT NULL,
SESSIONDATE DATE,
SESSIONPRICE NUMBER(4,2),
CONSTRAINT SESSIONS_PK PRIMARY KEY(SESSIONID, BRANCHID, SCREENID, MOVIEID),
CONSTRAINT SESSIONS_FK1 FOREIGN KEY(BRANCHID) REFERENCES BRANCH(BRANCHID),
CONSTRAINT SESSIONS_FK2 FOREIGN KEY(SCREENID) REFERENCES SCREEN(SCREENID),
CONSTRAINT SESSIONS_FK3 FOREIGN KEY(MOVIEID) REFERENCES MOVIE(MOVIEID));

1 个答案:

答案 0 :(得分:0)

您有SCREEN的复合主键。因此问题是:

CONSTRAINT SESSIONS_FK2 FOREIGN KEY(SCREENID) REFERENCES SCREEN(SCREENID),

您可以轻松解决此问题,因为您还有BRANCHID

CONSTRAINT SESSIONS_FK2 FOREIGN KEY(SCREENID, BRANCHID) REFERENCES SCREEN(SCREENID, BRANCHID),