Inverse FK constraint in Postgresql 9.0?

I know that I can set the FK restriction to make sure Table1.ColA exists in Table2.Key, however what if I want to do the opposite?

I want Table1.ColA does not exist in Table2.Key .. Can I do this with any CHECK constraint, trigger, user function, etc.? Thank!

EDIT:

Let's say I have a table called "Names":

1 Michael
2 David
3 William

Now I have a table called "Aliases":

Mike -> 1
Mikey -> 1
Dave -> 2
Bill -> 3
Will -> 3

I want to make sure that no one adds the string “Michael” to “Aliases”, as it already exists in “Names”.

+3
source share
2 answers

SQL CREATE ASSERTION, PostgreSQL . (, UNION , GROUP BY COUNT(*) > 1) , , , inserted ) .

"" , UNIQUE, @gbn.

+2

  • "name type" (primar, nick name)
  • "nickname of", self FK

, , .

This eliminates the need to use any code to support "not FK"

+1
source

All Articles