Friday, May 20, 2005

Multiple Foreign Keys on the Same ID

You can't set the same foreign key for two different columns with the same constraint:

create table atable (
id varchar2(65) primary key);

create table btable (
a_id1 varchar2(65),
a_id2 varchar2(65));

ALTER TABLE btable
ADD CONSTRAINT btable_fkey_both
FOREIGN KEY (a_id1, a_id2)
REFERENCES atable(id, id)
ON DELETE SET NULL;

ERROR at line 1:
ORA-00957: duplicate column name

ALTER TABLE btable
ADD CONSTRAINT btable_fkey_both
FOREIGN KEY (a_id1, a_id2)
REFERENCES atable(id)
ON DELETE SET NULL;

ERROR at line 1:
ORA-02256: number of referencing columns must match referenced columns

But you can do it if you break it up into separate constraints:

ALTER TABLE btable
ADD CONSTRAINT btable_fkey1
FOREIGN KEY (a_id1)
REFERENCES atable (id)
ON DELETE SET NULL;

Table altered.

ALTER TABLE btable
ADD CONSTRAINT btable_fkey2
FOREIGN KEY (a_id2)
REFERENCES atable (id)
ON DELETE SET NULL;

Table altered.

Note: Monday is a holiday in Canada, no update until Tuesday.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?