The difference and length of smallint, int, bigint, tinyint in SQL Server database

Source: Internet
Author: User

SmallInt is an accurate numeric data type whose accuracy is unchanged after arithmetic operations, with 2-byte encoding

The range of signed smallint values is -2^15-------2^15-1, which is -32768----32767

The range of unsigned smallint values is 0-------2^16-1, 0----65535,

The int data type is stored as a 4-byte integer, with a range of values -2^31---2^31, so the smallint type is preferred when considering the maximum data range.

Numeric constants that support ranges in the INT data type are still interpreted as numeric, the number of decimal digits is 0, and the precision is sufficient to hold the value specified.

BigInt can accurately represent integers from the -2^63------2^63-1, the data type is stored as 8 bytes of storage, and when the bigint value is within the range supported by the INT data type, SQL Server will

BigInt is implicitly converted to int, and if the bigint expression contains a value outside the range supported by the INT data type, a conversion error occurs at run time.

In the data type precedence table, the bigint data type is above int, smallint

tinyint if the field is set to unsigned, only integers from 0 to 255 can be stored and cannot be used to store negative numbers

If the field is not set to unsigned, a number between 128 and 127 will be stored

tinyint storage with 1 bytes

In general, in order to save space, the smallest integer data should be used as much as possible, these types of integers look small, but in larger tables, the number of bytes increases quickly.

On the other hand, once you have created a field, it is difficult to modify it. So, for security reasons, you should predict how much a field needs to be stored, and then choose the appropriate data type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.