Free Copilot Chat learning sessions – become a Copilot Chat expert in an hour!
March 22, 2025I previously described how column data types help define what you can put in a column. But sometimes I need to be more specific.
For example, if I have an ExternalCourseRating column and it’s an integer but it’s not allowed to be any value outside the range 1 to 4, I need to do more than just say it’s an integer because that could have a wide range of potential values.
If it was a value that I can make a foreign key reference to a table of External Course Ratings, that would be a good solution.
But if I don’t have that, I can enforce the range with a check constraint.
Naming Check Constraints
Some people will look at the example and say “what’s with the odd constraint name?“.
I used to have pretty boring names for check constraints but over the years, I’ve come to appreciate constraint names that tell you what the issue is. When someone tries to insert or update an incorrect value, they are told they violated a check constraint, and they’re told the name of the constraint.
In so many systems, when generic (often meaningless) names are used, I have no idea a) which column had the issue or b) the issue that occurred. Meaningful constraint names help with that.
Learning T-SQL
It’s worth your while becoming proficient in SQL. If you’d like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.
The post T-SQL 101: #133 What is a Check Constraint in SQL Server? appeared first on The Bit Bucket.