Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Creating Constraints question
On Mon, 06 Mar 2000 23:23:34 GMT, argosy22_at_my-deja.com
wrote:
>and the primary keys are different:
That right there is why you are getting the error message when you create the foreign key constraint.
>If I make a constraint where one table references the other,
>must it reference the entire primary key, or not?
They must reference either a primary key, or a unique key. For example, the following should work:
CREATE TABLE parent (
parent_id number,
constraint parent_id_unique
unique (parent_id)
);
create table child (
child_id number,
parent_id number,
constraint child_has_parent
foreign key (parent_id) references parent (parent_id)
);
If you don't have either a unique or a primary key in your "parent" table that matches the foreign key in your child table, then you can do one of two things (that I'm aware of): You can create a trigger, or you can create a third table.
Jonathan