Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict the length of INTEGER when creating a table in SQL Server?

When creating a table in SQL SERVER, I want to restrict that the length of an INTEGER column can only be equal 10.

eg: the PhoneNumber is an INTEGER, and it must be a 10 digit number.

How can I do this when I creating a table?

like image 894
Midhun Mathew Avatar asked Aug 21 '14 09:08

Midhun Mathew


People also ask

How do I restrict the length of an integer in SQL?

If you want to limit the range of an integer column you can use a check constraint: create table some_table ( phone_number integer not null check (phone_number between 0 and 9999999999) ); But as R.T. and huMpty duMpty have pointed out: a phone number is usually better stored in a varchar column.

How do you define integer length in SQL?

The INTEGER datatype is just a subtype of NUMBER. You can define the column as NUMBER(8,0) to get you an integer column that is <= 8 digits.

What is length precision and scale in SQL Server?

Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2. In SQL Server, the default maximum precision of numeric and decimal data types is 38.


1 Answers

If you want to limit the range of an integer column you can use a check constraint:

create table some_table
(
   phone_number integer not null check (phone_number between 0 and 9999999999)
);

But as R.T. and huMpty duMpty have pointed out: a phone number is usually better stored in a varchar column.

like image 83
a_horse_with_no_name Avatar answered Nov 15 '22 05:11

a_horse_with_no_name