I’m using Postgresql and JDBC and I’m trying to figure out how to create an assertion.
I currently have two relations table A(url varchar(30)) and table B(url varchar(30)) and I would like to write an assertion so that it is impossible to insert into table B if that element already exists in table A. How do I do this in JDBC? If it’s not possible to use assertions, how would I make something similar happen?
EDIT: Ok, so I’ve read that it’s not possible to create an assertion using postgresql; thus, my question is now, how would I do something similar using the constraints of Postgresql and JDBC?
What you want is effectively a negative foreign key constraint, which isn’t directly supported. You can implement this constraint using a PL/PgSQL
BEFORE INSERT OR UPDATEtrigger function that tests the condition.See PL/PgSQL trigger procedures in the Pg docs.
You’ll probably need to use
SERIALIZABLEisolation mode to handle races where one transaction inserts intoBwhile the other inserts intoA. You’ll need the improvedserializableisolation support in 9.1/9.2.