Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: Is this supposed to work like this?
On Wed, 29 Oct 1997 11:51:57 -0500, "Joseph D. Sumalbag" <joseph_sumalbag_at_bose.com> wrote:
>Alan Caldera wrote:
Meant to say REFERENCES tablea.
>> INSERT INTO tableb VALUES(1,1,null);
>> COMMIT;
>>
>> Why do I not get a FK violation on the insert into table B? If I put
>> in an obviously invalid combination in aa2 and aa3 then I get the
>> appropriate parent key not found message.
>>
>> Any ideas?
>>
Well, after thinking more about it and consulting with various
sources, I have answered my own question. BTW, it is legal to say
REFERENCES tablea without naming the columns explicitly.
It turns out that Oracle permits partially null foreign keys in accordance with ISO-ANSI SQL 92. To truly enforce the condition you desire (ie having both parts of the key), one must either employ a NOT NULL or CHECK constraint. I came to this after further realizing that if I neglected to properly enforce it and specified ON DELETE CASCADE in the foreign key definition, that I would end up with orphan records. NOT GOOD.
In short, to ensure that referential integrity in enforced across multiple columns and still allowing the fields to be 'optional':
ALTER TABLE tableb ADD CONSTRAINT ck_fk_intact CHECK ((aa2 is not null and aa3 is not null) or (aa2 is null and aa3 is null));
Adding this constraint produces what I would consider to be proper behaviour. I think I will add this to my list of interview questions to harass candidates with. ;-).
Thanks to all that responded.
Alan Caldera Received on Thu Oct 30 1997 - 00:00:00 CST
![]() |
![]() |