oracle ORA-02270: 此列列表没有匹配的唯一键或主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16295961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:37:34  来源:igfitidea点击:

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

oracle

提问by user2332706

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

出现 ORA-02270: 此列列表没有匹配的唯一键或主键

What is the problem?

问题是什么?

回答by sbaker

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

您正在通过创建主来创建复合(或复合)键

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

and then, you try to make ACCREDITATION_BODY_IDa 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 notthe correct way to do it.

然后,您尝试创建ACCREDITATION_BODY_ID一个外键,引用同一个表上的同一列。我不知道你想达到什么目的,但无论如何,这不是正确的方法。

In my opinion, Reason is:ACCREDITATION_BODY_IDmust 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 allthe columns in the foreign key statement. (fix me if I'm wrong)

在我看来,Reason 是:ACCREDITATION_BODY_ID必须是主键才能被引用为外键,但这里的情况并非如此。你的表的约束是一个复合键,你需要引用外键语句中的所有列。(如果我错了请纠正我)

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.

顺便说一句,在同一个表中将主键作为外键引用对我来说没有任何意义(也许我错过了一些东西,但仍然......)。我会考虑改变设计。