尝试使用3个外键引用相同的主键时,ORA-00957重复列名称错误

时间:2017-03-21 11:03:58

标签: sql oracledb

我在创建表时遇到问题:

CREATE TABLE EMPLOYEE
(
employee_id NUMBER(5) NOT NULL UNIQUE,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL

CONSTRAINT employee_pk PRIMARY KEY (employee_id)
);

CREATE TABLE PROJECT
(
project_id NUMBER(5) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,

CONSTRAINT project_pk PRIMARY KEY (PROJECT_ID),

CONSTRAINT fk_leader
    FOREIGN KEY (consultant_leader, developer_leader, project_leader)
    REFERENCES EMPLOYEE (employee_id, employee_id, employee_id)
);

在上一节中,当我尝试引用员工的表employee_id时,我得到了ORA-00957。我认为这是因为3个不同的领导类型外键引用了相同的employee_id,但据我所知,它应该不是问题。语法错了吗?

2 个答案:

答案 0 :(得分:1)

您当前的问题是您需要三个外键关系,而不是一个有三列的关键。

但是,没有必要将primary key声明为unique。所以,我建议:

CREATE TABLE EMPLOYEE (
    employee_id NUMBER(5) NOT NULL PRIMARY KEY,
    position VARCHAR2(100) NOT NULL,
    name VARCHAR2(255) NOT NULL,
    salary NUMBER(6) NOT NULL
);

CREATE TABLE PROJECT (
    project_id NUMBER(5) NOT NULL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    budget NUMBER(6) NOT NULL,
    consultant_leader NUMBER(5) NOT NULL,
    developer_leader NUMBER(5) NOT NULL,
    project_leader NUMBER(5) NOT NULL,
    CONSTRAINT fk_leader FOREIGN KEY (consultant_leader)
         REFERENCES EMPLOYEE (employee_id),
    CONSTRAINT fk_leader FOREIGN KEY (developer_leader)
         REFERENCES EMPLOYEE (employee_id),
    CONSTRAINT fk_leader FOREIGN KEY (project_leader)
         REFERENCES EMPLOYEE (employee_id)
);

当然,您不需要将PRIMARY KEY约束置于行内。单独声明它的好处是你可以根据自己的喜好给出约束名称。

答案 1 :(得分:0)

我认为你应该创建三个不同的FK:FK_Consultant,FK_developer,FK_projleader