check constraint opinion?

  • What's your opinion on check constraints in SS?

    Check constraints seem good for enforcing constraints at the table level. Putting validation at other levels does not prevent a user from directly entering invalid data in the database.

    However, can check constraints potentially slow the database down or create other issues?

  • sqlguy-736318 (9/25/2012)


    What's your opinion on check constraints in SS?

    Check constraints seem good for enforcing constraints at the table level. Putting validation at other levels does not prevent a user from directly entering invalid data in the database.

    However, can check constraints potentially slow the database down or create other issues?

    I am not 100% sure what you are looking for as an answer but...yes check constraints can potentially slow down the database. In fact, they will slow it down. The more important question is how much. Check constraints are usually really really fast to the point that the performance difference is unnoticeable and even difficult to measure. As a DBA it is your job to ensure the data. If that means that only values in a certain range or something like that then you should use check constraints. This is one of the ways as a DBA you have of ensuring that garbage data does not get in the system. I would never trust the validation outside of the database to be consistent. You just need to be sure that whatever constraint you establish is in line with the data requirements.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • An alternative to CHECK constraints are Foreign Keys referencing lookup tables. These are useful when you need to validate that a value in a column is within a set and that set may need to be changed periodically. For example if your company ships to customers in the USA and Canada you could have a CHECK constraint on the Country column in your CustomerAddress table that made sure that was the case, however if you think you may someday ship to Mexico you may instead want to create a table named Country, add rows for USA and Canada and reference it using a Foreign Key. The, when you start shipping to Mexico you simply add a row to the Country table and from that opint forward you can add rows to CustomerAddress using the new country code.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply