I have a requirement as mentioned below:
table1
------------------------
A B C D E
------------------------
1 2 * P Q
1 2 A Q P
1 3 B W U
-----------------------
Column A B C are the primary key on table1
table2
------------------------
A B C
------------------------
1 2 1
1 2 2
1 2 A
------------------------
column A B C needs to be implemented as composite foreign key on table2
for column A B C on table 1, the only exception is ,if column C in parent table (table 1) is *
then any value can appear in column C of child table (table2) provided column A and column B
are same in both tables.
We are looking for an implementation with minimal trigger .Currently we are not able to create foreign key because of this weird requirement.
Please suggest any alternate approach and make my day 🙂
This issue can be addressed without need of any new structure.
just by introducing a new_column (NEW_C) in child table (table2)
and using this column in foreign key contraint instead of ‘column C’.
Steps are below:
1>
alter table2 add (new_c varchar2(1));2>
update table2 set new_c= c;3>Use newly introduced column NEW_C instead of
colum Cin FK4>Create a simple insert/update trigger on child table to maintain new column
NEW_C.Alternative is to introduce a new virtual column (oracle 11g) on child table,
Use deterministic pl/sql function to
derive this virtual coulmn and then create foreign key constraint
using Virtual column.
But I will prefer trigger based approach as any udate/delete operation on parent table
will suffer because need to derive and check Virtual column each time.