referential integrity - Should I enforce business logic through database errors? -


there's interesting design decision i've been thinking lately. let's i'm adding usernames table, , want make sure there no duplicates. username column not null unique. either:

  1. query database before inserting make sure there no duplicate names, or
  2. just insert, , catch exceptions come database engine.

assuming db i'm using capable of enforcing constraints, wondering situations each of these choices appropriate in.

it seems idea option 2. wouldn't recommend option 1 because you've doubled amount of time required inserts (they require reads first). besides, new developer going commit sometime , not check, , broken.

another thing consider how downtime appropriate? mission critical app? happens if business logic corrupt? factories shut down if is? or annoying bugs.

you can't afford have factories shut down because exception didn't think of crashed server. so, perhaps nightly or weekly check on data correctness can in case. however, feel db capabilities enforce uniqueness (and potentially other enforcements) appropriate way go.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -