Skip to Main Content

Oracle Database Discussions

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Foreign Key Constraint

goodluck247Apr 18 2017 — edited Apr 25 2017

Hello all,

I have the following 2 tables:

create table custinfo(

cust_custno  number(5),

cust_plantno number(3),

cust_name    varchar2(50));

alter table custinfo add constraint pk_custinfo_custno_plantno primary key (cust_custno, cust_plantno);

create table contracts(

cont_custno number(5),

cont_name   varchar2(30),

cont_start  date,

cont_end    date,

cont_notes  varchar2(50));

alter table contracts add constraint fk_contracts_custno foreign key (cont_custno) references custinfo (cust_custno);

When I execute the above statements, it gives me an error:

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

02270. 00000 -  "no matching unique or primary key for this column-list"

*Cause:    A REFERENCES clause in a CREATE/ALTER TABLE statement

           gives a column-list for which there is no matching unique or primary

           key constraint in the referenced table.

*Action:   Find the correct column names using the ALL_CONS_COLUMNS

           catalog view

It is my understanding, that this is happening because the PK in the first table is a composite PK, and the FK in the second table is trying to use only one of the two columns making the PK.

But I don't need the second column in my FK, in fact, it is imperative that I not use the second column (cust_plantno), but use the first one (cust_custno).

Is there a solution to this situation?

Thank you.

This post has been answered by Frank Kulash on Apr 18 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 16 2017
Added on Apr 18 2017
13 comments
607 views