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.
Comments
jtheory replied ago:
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?
Motion Control replied ago:
Don't buy a safe. Because, if the burglars can enter your house you've got a problem.
icet replied ago:
hibernate validator + annotations will give you the best cost/beneffit of both worlds (app and db validation)
Motion Control replied ago:
Get some real world experience ...
Motion Control replied ago:
The blog post merely scratches the surface of the subject.
jlawmi replied ago:
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 :)
Voters For This Link (9)
Voters Against This Link (2)