Question

I am working on SQL Server 2012

I would like to know what is the maximum number of Check Constraints a Column can have?

I tried searching online, but couldn't find

Was it helpful?

Solution

I would say 1, since you only obtain a column check constraint by declaring it inline with the rest of the column definition, and you cannot declare multiple such constraints at that point.

create table T (ID int not null CHECK (ID > 10) CHECK (ID < 99))

Produces the result:

Msg 8148, Level 16, State 0, Line 1

More than one column CHECK constraint specified for column 'ID', table 'T'.

However, you can declare as many table constraints as you like, up to the theoretical limit that Patrick's Answer mentions. But as with many things, if you have to ask about a limit, you're probably doing something wrong. I.e. around about the time that you're writing your 50th constraint about one column, shouldn't you be asking yourself why you have so many?


Okay, this answer obviously is causing some confusion. At the end of the day, there's not much to distinguish between column check constraints and table check constraints that just happen to only mention a single column. All I'd like to point out is that the system does seem to draw distinctions between them. Whether that actually achieves any efficiencies (e.g. column check constraints need not be considered if the column they apply to isn't mentioned in a query) I couldn't say for sure.

But SQL Server insists on there only being a maximum of a single column check constraint per column in the table, and exposes this information in it's catalogs. For example, in sys.check_constraints, it exposes the parent_column_id which is described as

0 indicates a table-level CHECK constraint.

Non-zero value indicates that this is a column-level CHECK constraint defined on the column with the specified ID value.

OTHER TIPS

I think you are pretty good since the maximum number of objects, including constraints, database wide is 2,147,483,647. There doesn't seem to be a maximum number of check constraints on a column / table.

Technet:

Database objects include objects such as tables, views, stored procedures, user-defined functions, triggers, rules, defaults, and constraints. The sum of the number of all objects in a database cannot exceed 2,147,483,647.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top