ORA-02270: no matching unique or primary key for this column-list

 ̄綄美尐妖づ 提交于 2021-02-05 08:56:11

问题


I have a table, and the structure is

CREATE TABLE  "COURSE_ACCREDITED" 
("COURSE_ID" VARCHAR2(50) NOT NULL ENABLE, 
"ACCREDITATION_BODY_ID" VARCHAR2(50) NOT NULL ENABLE, 
"DATE_OBTAINED" VARCHAR2(50), 
 PRIMARY KEY ("COURSE_ID", "ACCREDITATION_BODY_ID", "DATE_OBTAINED") ENABLE, 
 CONSTRAINT "COURSE_ACCREDITED_CON" FOREIGN KEY ("COURSE_ID")
  REFERENCES  "COURSE" ("COURSE_ID") ENABLE)

When I add a constrain

alter table "COURSE_ACCREDITED" add constraint
"COURSE_ACCREDITED_CON2" foreign key ("ACCREDITATION_BODY_ID") references "COURSE_ACCREDITED"    ("ACCREDITATION_BODY_ID")

It appears ORA-02270: no matching unique or primary key for this column-list

What is the problem?


回答1:


You are creating a composite (or compound) key by making this primary:

("COURSE_ID", "ACCREDITATION_BODY_ID", "DATE_OBTAINED")

and then, you try to make ACCREDITATION_BODY_ID a foreign key, referencing to the same column on the same table. I don't know what are you trying to achieve, but anyway, It's not the correct way to do it.

In my opinion, Reason is: ACCREDITATION_BODY_ID must be a primary key in order to be referenced as a foreign key, but that's not the case here. Your table's constraint is a compound key, and you need to reference all the columns in the foreign key statement. (fix me if I'm wrong)

Try reference all the compound key columns in the foreign key statement. That may solve your problem.

By the way, referencing a primary key as a foreign key in the same table didn't make any sense to me (maybe i'm missing something but still..). I'd consider changing the design.



来源:https://stackoverflow.com/questions/16295961/ora-02270-no-matching-unique-or-primary-key-for-this-column-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!