SQL constraint minvalue / maxvalue?

Shimmy Weitzhandler picture Shimmy Weitzhandler · Nov 15, 2009 · Viewed 52.2k times · Source

Is there a way to set a SQL constraint for a numeric field that min value should be 1234 and max value should be 4523?

Answer

yfeldblum picture yfeldblum · Nov 15, 2009

SQL Server syntax for the check constraint:

create table numbers (
    number int not null
        check(number >= 1234 and number <= 4523),
    ...
)

create table numbers (
    number int not null,
    check(number >= 1234 and number <= 4523),
    ...
)

create table numbers (
    number int not null,
    constraint number_range_check
        check(number >= 1234 and number <= 4523),
    ...
)