--
-- Avoid error messages due to existing tables.
-- Tables can only be DROPped when they are "empty"
--
DROP TABLE arc_ CASCADE CONSTRAINTS;
DROP TABLE arc_reftbl1 CASCADE CONSTRAINTS;
DROP TABLE arc_reftbl2 CASCADE CONSTRAINTS;
DROP TABLE arc_reftbl3 CASCADE CONSTRAINTS;
CREATE TABLE arc_ (
arcid NUMBER
, fkcol1 NUMBER
, fkcol2 NUMBER
, fkcol3 NUMBER
);
ALTER TABLE arc_ ADD CONSTRAINT arc_pk PRIMARY KEY (arcid);
CREATE TABLE arc_reftbl1 ( rpk1 NUMBER );
CREATE TABLE arc_reftbl2 ( rpk2 NUMBER );
CREATE TABLE arc_reftbl3 ( rpk3 NUMBER );
ALTER TABLE arc_reftbl1 ADD CONSTRAINT arc_reftbl1_pk PRIMARY KEY ( rpk1 );
ALTER TABLE arc_reftbl2 ADD CONSTRAINT arc_reftbl2_pk PRIMARY KEY ( rpk2 );
ALTER TABLE arc_reftbl3 ADD CONSTRAINT arc_reftbl3_pk PRIMARY KEY ( rpk3 );
-- ------------------------------------------------------------------------
-- foreign keys: table arc_ only
-- ------------------------------------------------------------------------
ALTER TABLE arc_
ADD CONSTRAINT arc_fk1
FOREIGN KEY (fkcol1) REFERENCES arc_reftbl1( rpk1 );
ALTER TABLE arc_
ADD CONSTRAINT arc_fk2
FOREIGN KEY (fkcol2) REFERENCES arc_reftbl2( rpk2 );
ALTER TABLE arc_
ADD CONSTRAINT arc_fk3
FOREIGN KEY (fkcol3) REFERENCES arc_reftbl3( rpk3 );
-- ------------------------------------------------------------------------
-- CHECK constraints: table arc_ only
-- ------------------------------------------------------------------------
ALTER TABLE arc_
ADD CONSTRAINT check_xor
CHECK (
( fkcol1 IS NOT NULL AND fkcol2 IS NULL AND fkcol3 IS NULL )
OR ( fkcol1 IS NULL AND fkcol2 IS NOT NULL AND fkcol3 IS NULL )
OR ( fkcol1 IS NULL AND fkcol2 IS NULL AND fkcol3 IS NOT NULL )
);