Monday, February 02, 2009

Valid Data Vs Correct Data

A constraint makes sure the data is valid, but it can not guaranty it is correct.

For example, if you use a foreign key to enforce data in child table should have a reference in parent table. you can guaranty the data in child table is valid, but you can not tell if the data is correct. if the primary key is composed of data "male" and "female", as long as data in child table is one of these valid values, it passes the checking of constraint. But you might mistakenly assigned a male a gender of female.

Using foreign key is better than using trigger to validating the data, because foreign key checks the validity before data is entered. In contrast, DML triggers works on data after data has been entered. Trigger can roll the change back, but that requires double work since the data is entered, then undone.

No comments: