Link Details

Link 124148 thumbnail
User 298346 avatar

By gauravjassal
via gauravjassal.com
Published: Oct 10 2008 / 09:32

Constraints enables business rules to be enforced by the database instead of via application code. Through the judicious use of constraints, application and SQL coding can be minimized and data integrity can be maximized. Constraints may be applied to columns in the form of uniqueness requirements, relational integrity constraints to other tables/rows, allowable values and data types.For example, a column containing a product price should probably only accept positive values. But there is no standard data type that accepts only positive numbers. Another issue is that you might want to constrain column data with respect to other columns or rows. For example, in a table containing product information, there should be only one row for each product number.
  • 9
  • 2
  • 2126
  • 454

Comments

Add your comment
User 210175 avatar

jtheory replied ago:

0 votes Vote down Vote up Reply

How do database constraints reduce application and/or SQL code?

They help find bugs quicker, sure, and they protect your data (particularly important if you allow ad hoc SQL at all...) but in my experience if a production app is regularly attempting to violate database constraints (because it doesn't protect the data in code...) you've got a problem.

Does anyone use database constraints *instead* of app logic to enforce business rules?

User 285418 avatar

Motion Control replied ago:

-1 votes Vote down Vote up Reply

Don't buy a safe. Because, if the burglars can enter your house you've got a problem.

User 200970 avatar

icet replied ago:

0 votes Vote down Vote up Reply

hibernate validator + annotations will give you the best cost/beneffit of both worlds (app and db validation)

User 285418 avatar

Motion Control replied ago:

-1 votes Vote down Vote up Reply

Get some real world experience ...

User 285418 avatar

Motion Control replied ago:

0 votes Vote down Vote up Reply

The blog post merely scratches the surface of the subject.

User 246736 avatar

jlawmi replied ago:

0 votes Vote down Vote up Reply

Good starter if you don't know what a constraint is.

thoughts : constraints have more value if >1 code base will be hitting the tables. This is common in an enterprise, such that having validation in hibernate won't help the pl/sql batch code.
Also, for each constraint you do not have, thats one more unit test you should have :)


Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.