|
I wonder how many people are already aware of this, and practice it? I wonder if MySQL is capable of this?
Constraints are a way of enforcing data integrity. If your data is of value, they prevent it from going corrupt, becoming untrustworthy. There are different types, from default values to checks to referential integrity. All of these are declarative - you tell the database what the rules of proper data are, and it makes sure they're obeyed.
A check constraint is simple. If you have an employee table with an age column, you might only allow values from 18 to 80 years old. If somebody tries to update an employee and make her 175 years old, the database would fail that update - if you have a check constraint in place. Here's the kicker, if you query the employee table for anyone > 80 years old, it will return an empty set without even looking at the table!
DRI is similar. If there's a primary and foreign key relationship between two tables, you can't have data in one unless there's matching data in the other. With this setup, if you query for line items for an order that doesn't exist, the database will return an empty set, often by looking at the order table instead of order detail. Why? The table is much smaller, so you get your answer much faster.
You can verify this by looking at query plans. And then, so long as you don't use nocheck, you can optimize read performance in your database by setting up proper constraints.
|