I have a table with a composite foreign key like so:
CREATE TABLE a
(
a1 INTEGER,
a2 INTEGER,
a3 INTEGER,
a4 INTEGER,
-- other columns
FOREIGN KEY (a1, a2, a3, a4) REFERENCES b
);
The foreign key may or may not exists, therefore a1 to a4 may be NULL, however if so, they should be NULL all at the same time. Is there a better way to specify such an "optional composite foreign key" than to add a (rather verbose) constraint like so?
CHECK ((a1 IS NULL) = (a2 IS NULL) AND
(a3 IS NULL) = (a4 IS NULL) AND
(a1 IS NULL) = (a3 IS NULL))