ORA-02256:引用列的数量必须与引用的列匹配

时间:2016-04-17 12:58:06

标签: sql oracle

CREATE TABLE STATEMENTS.

OK. 0 rows

OK. 0 rows

FOREIGN KEY (ProdId) REFERENCES ALLOCATION
                                *
Error at line 7:
ORA-02256: number of referencing columns must match referenced columns

我得到的错误。

分配是弱实体,不确定如何解决任何帮助会很好:D

CREATE TABLE MANAGER (
MgrId number (3),
MFirstName varchar2(30),
MSurName varchar2(30),
PRIMARY KEY (MgrId)
);

CREATE TABLE ALLOCATION (
MgrId number (3),
ProdId number(4),
PRIMARY KEY (MgrId, ProdId),
FOREIGN KEY (MgrId) REFERENCES MANAGER,
FOREIGN KEY (ProdId) REFERENCES PRODUCT
);

CREATE TABLE QUALITYCHECK (
WeekNo number (1),
Score number (1),
MgrId number (3) NOT NULL,
ProdId number(4) NOT NULL,
PRIMARY KEY (WeekNo, MgrId, ProdId),
FOREIGN KEY (MgrId) REFERENCES ALLOCATION,
FOREIGN KEY (ProdId) REFERENCES ALLOCATION
);

2 个答案:

答案 0 :(得分:0)

ProdID是一列,但引用表中的主键是TWO COLUMNS,MgrID和ProdID。您不能让FK引用PK的子集。

如何修复取决于您想要做什么。例如,为什么你需要ProdID来引用ALLOCATION而不是PRODUCT(顺便提一下你没有展示的表格)?

答案 1 :(得分:0)

如果您没有引用主键本身,您还需要提及引用表的列。

CREATE TABLE ALLOCATION (
  MgrId number (3),
  ProdId number(4),
  PRIMARY KEY (MgrId, ProdId),
  FOREIGN KEY (ProdId) REFERENCES PRODUCT(ProdId),
  FOREIGN KEY (MgrId) REFERENCES MANAGER(MgrId)
);

始终引用父表。虽然不是强制性的。这只是一个值得遵循的标准。

CREATE TABLE QUALITYCHECK (
  WeekNo number (1),
  Score number (1),
  MgrId number (3) NOT NULL,
  ProdId number(4) NOT NULL,
  PRIMARY KEY (WeekNo, MgrId, ProdId),
  FOREIGN KEY (MgrId) REFERENCES MANAGER(MgrId),
  FOREIGN KEY (ProdId) REFERENCES PRODUCT(ProdId)
);