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

时间:2015-04-17 14:21:05

标签: sql database oracle isql

这些是我的表格:

CREATE TABLE EMPLOYEE(
    Emp_id number(4),
    Emp_name varchar2(30),
    Emp_gender varchar2(1),
    Status varchar2(30),
    Years_service number(4),
    Primary Key (emp_id)
);

CREATE TABLE ACTIVITY(
    Act_id number(4),
    Description varchar2(30),
    Category_code varchar2(1),
    Primary Key(Act_id)
);

CREATE TABLE ALLOCATION(
    Emp_id number(4) NOT NULL,
    Act_id number(4) NOT NULL,
    Hourly_rate number(5,2) NOT NULL,
    Primary Key (Act_id, Emp_id),
    Foreign Key (Act_id) REFERENCES ACTIVITY,
    Foreign Key (Emp_id) REFERENCES EMPLOYEE,
    CONSTRAINT CK_ALLOCATION_RATE CHECK(Hourly_rate > 0 and Hourly_rate<300) 
);

CREATE TABLE ACTION(
    Week_no number(2) NOT NULL,
    Hrs_worked number(4,1) NOT NULL,
    Act_id number(4) NOT NULL,
    emp_id number(4) NOT NULL,
    Primary Key (Week_no, Act_id, emp_id),
    Foreign Key (Act_id) References Allocation,
    Foreign Key (emp_id) References Allocation
);

完整地创建了表格employeeactivityallocation。但是当我尝试创建表action并将外键引用到表allocation时,它会说: ORA-02256:引用列的数量必须与引用的列匹配。

1 个答案:

答案 0 :(得分:1)

使用references时,我认为在两个表中包含列是个好主意,即使语法不必要(默认是主键)。 / p>

您有一个复合主键,因此您需要一个外部引用的复合键:

CREATE TABLE ACTION (
    Week_no number(2) NOT NULL,
    Hrs_worked number(4,1) NOT NULL,
    Act_id number(4) NOT NULL,
    emp_id number(4) NOT NULL,
    Primary Key (Week_no, Act_id, emp_id),
    Foreign Key (Act_id, emp_id) References Allocation(Act_id, emp_id)
);