James Gates
Posts: 1
Nickname: jamesgates
Registered: Sep, 2008
|
|
Re: Database Constraints: Use Them or Lose Them?
|
Posted: Oct 2, 2009 11:59 AM
|
|
Use database constraints to preserve the essential integrity of the data model. Unique or not-null columns and foreign-key references, which are inherent to and invariant in the model, belong in the database--even if you have to duplicate some validation logic in the application tiers.
My rationale for this is two-fold.
First, in every significant system I've worked on, the data has outlived the original application code. For any number of reasons new requirements for accessing the data are identified. So, that original client-server may be replaced by or co-exist with web applications, mobile clients, Flex or other RIA clients, web services, and so on, yet the underlying data persists. In such cases the data is clearly where the lasting business value lies. Preserving its basic integrity has to be a given.
Second, this ability to ensure the basic integrity of a data model is one of the main benefits of a DBMS. By designing fundamental integrity constraints (which are very different from mutable business rules) into the declarative data model, the constraints are exercised and enforced on every possible access path. They are truly invariant and simply can't be violated. The same can't be said for constraints imposed at any other layer.
Note that I'm not arguing for building mutable business rules into the database constraints. But basic model integrity? Absolutely.
I'm also not arguing that input validation should be deferred to the database. I've made good use of Hibernate Validator, and expect to use Beans Validations or whatever is best in the future to consolidate my input validation logic. It's a good thing to be able to have input validated in the web client (for a more responsive user experience) and again in the web application (to catch anyone trying to hack the site) and again in the database (for any input that affects model integrity). These aren't just wasted processor cycles or unnecessary repetition, they're needed to fulfill the joint requirements of usability and system integrity. Overlapping your security zones is a good thing.
|
|