Home:ALL Converter>Oracle foreign key constraints - check constraint syntax?

Oracle foreign key constraints - check constraint syntax?

Ask Time:2018-08-10T09:17:20         Author:Mr Magoo

Json Formatter

I have a child table in oracle that has two foreign key columns, relating to two different parent tables. I want to create a constraint that says the child must have at least one of those parents - e.g.

ALTER TABLE table_name
ADD CONSTRAINT constraint_name
   FOREIGN KEY (column1)
   REFERENCES parent_table (column1)
   OR
   FOREIGN KEY (column2)
   REFERENCES parent_table_2 (column1)

This won't work with a foreign key constraint because that can only relate to one parent table - is it possible to do this with a check constraint instead?

Author:Mr Magoo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51777666/oracle-foreign-key-constraints-check-constraint-syntax
Dmitry Grekov :

Foreign key constraints ensure the referential integrity, not mandatory values.\nI think you have to have to separate FK contraints and additional check constraint like this:\n\nalter table table_name \n add constraint c_check_cols \n check(column1 is not null or column2 is not null);\n",
2018-08-10T06:08:07
yy