views:

127

answers:

3

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?

+1  A: 

If you are using SQL Server, you want to use a CHECK constraint like this:

CREATE TABLE foo ( someint INT NOT NULL CHECK (someint >= 1234 AND someint <= 4523) )

steveschoon
+4  A: 

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),
    ...
)
Justice
The link bellow really helped me: http://technet.microsoft.com/en-us/library/ms179491.aspx.
Shimmy
+1  A: 
CREATE TABLE WhatEver
(
    ...
    NumericField INTEGER NOT NULL CHECK(NumericField BETWEEN 1234 AND 4523),
    ...
);

Note that 'BETWEEN AND' provides a range inclusive of the quoted limit values.

Jonathan Leffler