Skip to content

Instantly share code, notes, and snippets.

@blt
Created June 8, 2011 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blt/1014519 to your computer and use it in GitHub Desktop.
Save blt/1014519 to your computer and use it in GitHub Desktop.
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