Checking record with a foreign key exists

Should i check the existence of foreign key with an sql request or should I just try to insert and catch if it fails?

I can check if the key i'm referencing exists or not.

My question is more of a theoretical: Catching an exception vs. querying db one more time for every insert.

For example say you have two tables, Cats and Humans. Cats has a foreign key referencing Human. Before I try to insert a Cat should I check if a human with given cat's owner column exists or should I try to insert blindly and catch if it fails.



Read more here: https://stackoverflow.com/questions/23176580/checking-record-with-a-foreign-key-exists

Content Attribution

This content was originally published by interlude at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: