I know I can setup a FK constraint to make sure Table1.ColA exists in
Table2.Key, however what if I want to do the reverse?
I want to ensure Table1.ColA does NOT exist in Table2.Key.. Can I do
this with any sort of CHECK constraint, trigger, custom function, etc?
Thanks!
EDIT:
Let’s say I have a table called “Names” :
1 Michael
2 David
3 William
Now I have a table called “Nicknames”:
Mike -> 1
Mikey -> 1
Dave -> 2
Bill -> 3
Will -> 3
I want to make sure no one adds the row “Michael” to “Nicknames” since it already exists in “Names”.
In Standard SQL you can use
CREATE ASSERTIONbut PostgreSQL doesn’t support it. You can fake it in triggers on both tables (e.g.UNIONthe two tables,GROUP BYnames and testCOUNT(*) > 1or perhaps just test that the logicalinsertedtable values do not appear in the other table) or otherwise procedural code.You can ‘design away’ the problem by using one table and an explicit subtype and use a conventional
UNIQUEconstraint, as suggested by @gbn.