Prev | Current Page 163 | Next

Brad Ediger

"Advanced Rails"

If
the validations in the domain are the only way to ensure that the data is consistent,
this may lead to problems.
Constraints
Database-level constraints provide a way to explicitly specify an application??™s
implicit assumptions about its data. There are two types of constraints, which should
not be confused:
Business logic
???A manager may not manage more than five employees.??? The key characteristic
of business logic constraints is that they could conceivably change throughout
the lifetime of the database. Business logic constraints should never be in the
database, barring a very good reason to the contrary.
Integrity
???U.S. Social Security numbers, when provided, must contain exactly nine digits.???
Integrity constraints define the nature of the data being represented. Admittedly,
???the nature of the data??? is a somewhat nebulous concept; the meaning will
differ between databases. Integrity constraints must reside in the database, if for
no other reason than to provide a last-level sanity check on the data.
Advanced Database Features | 111
As with any other area of data modeling, there are gray areas. An example would be
???an employee??™s salary must be positive,??? which could conceivably go either way.*
The advantage of constraints is that they narrow the domain of possible results the
database can generate. When you know the DBMS for an online store can never output
a negative price for a product, you can sum the prices for the line items belonging
to an order without worrying about invalid prices.


Pages:
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175