Check for integrity constraint violation in SQL before deletion
My question is sort of similar to this one
I want to check if a DELETE query will fail because of a constraint violation. I'd like to do this on database level because I think letting it fail and catching the error is ugly.
Another option is "manually" checking for it using SELECT queries to see if there are constraints, but this is rather tedious, imho.
Is 开发者_如何学编程there a "native" way to do this?
The simplest and most direct way is to execute the DELETE, and trap the error.
If you run a SELECT statement against other tables, you might learn whether rows exist (implying a foreign key constraint). But foreign key constraints are just one of many kinds of integrity constraints.
You also have to consider that some integrity constraints might be implemented with triggers (for good reasons, bad reasons, or no reasons at all).
精彩评论