Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CREATE TABLE A (
id SERIAL NOT NULL PRIMARY KEY
);
CREATE TABLE B (
id SERIAL NOT NULL PRIMARY KEY
);
CREATE TABLE Parent (
id SERIAL NOT NULL PRIMARY KEY,
aId INTEGER NOT NULL REFERENCES A (id),
bId INTEGER NOT NULL REFERENCES B (id),
UNIQUE(aId, bId)
);
CREATE TABLE Child (
parentId INTEGER NOT NULL REFERENCES Parent (id),
createdOn TIMESTAMP NOT NULL
);
-- Is it possible to create a unique constraint on Child
-- such that for all rows in Child at most one references
-- a Parent having some value of aId? Stated another way
-- can I created a unique constraint so that the join of the
-- above tables will have no duplicate aId?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment