如何添加多个外键“ORA-02270:此列列表没有匹配的唯一键或主键”

时间:2016-09-25 12:49:58

标签: sql database oracle oracle10g

create table coc (
WS_NO number(10,0) PRIMARY KEY,
last_name varchar2(30),
first_name varchar2(30),
middle_initial varchar2(1),
Email varchar2(2),
contact_no varchar2(11),
date_to_be_submitted date,
applicant_id number(10,0),
company_name VARCHAR2(50),
plant_location VARCHAR2(50),
sample_id number(10,0),
sample_name VARCHAR2(50),
parameters VARCHAR2(50),
CONSTRAINT APPLICANT_FK FOREIGN KEY (applicant_id, company_name, plant_location) REFERENCES APPLICANT (applicant_id, company_name, plant_location),
CONSTRAINT SAMPLE_FK FOREIGN KEY (sample_id, sample_name, parameters) REFERENCES SAMPLE (sample_id, sample_name, parameters));

2 个答案:

答案 0 :(得分:0)

我认为错误信息非常清楚。您在外键约束中提到了列,但未定义它们。如果您希望它们引用其他内容,您需要定义它们:

create table coc (
    WS_NO number(10,0) PRIMARY KEY,
    last_name varchar2(30),
    first_name varchar2(30),
    middle_initial varchar2(1),
    Email varchar2(2),
    contact_no varchar2(11),
    date_to_be_submitted date,
    applicant_id ??,
    company_name ??,
    plant_location ??,
    sample_id ??,
    sample_name ??,
    CONSTRAINT APPLICANT_FK FOREIGN KEY (applicant_id, company_name, plant_location) REFERENCES APPLICANT (applicant_id, company_name, plant_location),
    CONSTRAINT SAMPLE_FK FOREIGN KEY (sample_id, sample_name, parameters) REFERENCES SAMPLE (sample_id, sample_name, plant_location)
);

请记住:类型必须与原始表中的类型相同。

答案 1 :(得分:0)

必须在引用的表APPLICANT和SAMPLE中将列集(applicant_id,company_name,plant_location)和(sample_id,sample_name,parameters)设置为唯一键或主键。通过选择ALL_CONS_COLUMNS按table_name过滤来检查这一点。