Reputation: 8610
Before I execute SQL query, I'd like to check if foreign key constraint will be violated. Is this possible?
Upvotes: 0
Views: 69
Reputation: 95642
You can check for the foreign key's existence with a SELECT statement. To do that in a completely transparent way, you'd need to write code that examines either system tables or information_schema views, builds the right SELECT statement on the fly, then executes it and examines the result. You can hardcode most of that stuff, but only at the risk of executing code that's based on assumptions that no longer exist. (FKs might be dropped, tables renamed, etc.)
But there are some problems with both those approaches.
Since you have to trap errors anyway, it usually makes more sense to just issue the INSERT or UPDATE statement, and trap the error that says you violated a foreign key constraint.
Upvotes: 2