Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set range for int in sql server

hey I'm new in SQL server i was wondering if we can set range to int (when creating table) like we set range to char

testing char(5) not null

when i tried to use the same way to set range in int

testing int(5) not null 

it will get error, so how to set range for int. if can't can you tell me why?

like image 851
user3371867 Avatar asked Dec 26 '22 09:12

user3371867


1 Answers

There is no INT(5) data type in sql server , but you can make use of CHECK constraints to put a limit/Range on values that can be inserted in that column.

Google to learn more about Check Constraint and you would do something like this.....

CREATE TABLE MyTable
(
    Testing  INT CHECK (testing > 100 AND testing <500)
  , OtherCol INT 
);
like image 107
M.Ali Avatar answered Jan 10 '23 16:01

M.Ali