3

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?

Community
  • 1
  • 1
Niels Bom
  • 8,728
  • 11
  • 46
  • 62

1 Answers1

3

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).

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185