SQL Server Data Type

Source: Internet
Author: User
Data Type Type Description
Bit Integer The BIT data type is an integer, and its value can only be 0, 1, or null. This data type is used to store data with only two possible values, such as yes, no, true, or fa lse, on, or off.
Int Integer Int data type can store integers from-231 (-2147483648) to 231 (2147483 647. This type can be used for almost all numeric data stored in the database. This data type occupies 4 bytes in the database.
Smallint Integer The smallint data type can store integers from-215 (-32768) to 215 (32767. This type of data is useful for storing numeric data that is often limited to a specific range. This data type occupies 2 bytes of space in the database.
Tinyint Integer The tinyint data type can store integers ranging from 0 to 255. It is useful when you only plan to store a limited number of values. This data type occupies 1 byte in the database
Numeric Exact numeric type Numeric has the same data type as decimal.
Decimal Exact numeric type The decimal data type can be used to store numeric data with fixed precision and range from-1038-1 to 1038-1. When using this data type, you must specify the range and precision. The range is the total number of digits that can be stored around the decimal point. Precision is the number of digits stored on the right of the decimal point.
Money Currency type The money data type is used to indicate money and currency values. This data type can store data from-922 billion to 922 billion, accurate to one thousandth of the currency unit
Smallmoney Currency type The smallmoney data type is used to indicate money and currency values. This data type can store data from-214748.3648 to 214748.3647, accurate to one thousandth of the currency unit
Float Approximate numeric type Float data is an approximate value type for floating point numbers. Floating Point Numbers are similar because not all numbers in the range can be accurately expressed. Floating Point number can be any number from-1.79e + 308 to 1.79e + 308
Real Approximate numeric type The real data type is similar to a floating point number and is an approximate value type. It indicates the floating point number between-3.40e + 38 and 3.40e + 38.
Datetime Datetime type The datetime data type is used to represent the date and time. This data type stores all the date and time data from January 1, 1/300 to 3.33 seconds or milliseconds.
Smalldatetime Datetime type The smalldatetime data type is used to indicate the date and time from January 1, January 1-20, 1900 to January 1, June 6, accurate to one minute.
Cursor Special Data Type The cursor data type is a special data type that contains a reference to the cursor. This type of data is used in the stored procedure and cannot be used when creating a table.
Timestamp Special Data Type The timestamp data type is a special data type used to create a unique digital within the database range. A table can have only one timestamp column. The value of the timestamp column changes each time a row is inserted or modified. Although its name contains "time", the timestamp column is not a date that people can recognize. In a database, the timestamp value is unique.
Uniqueidentifier Special Data Type The uniqueidentifier data type is used to store a globally unique identifier (guid. GUID is indeed globally unique. This number has almost no chance of being rebuilt in another system. You can use the newid function or convert a string to a unique identifier to initialize a column with a unique identifier.
Char Character Type The Char data type is used to store non-uniform encoded data with a fixed length. When defining a column of this type, you must specify the column length. This data type is useful when you always know the length of the data to be stored. For example, when you store data in zip code and 4 character format, you know that it always takes 10 characters. The column width of this data type is up to 8000 characters
Varchar Character Type The varchar data type, same as the char type, is used to store non-uniform encoding character data. Unlike char, the data type is variable. When defining a column as the data type, you must specify the maximum length of the column. The biggest difference between it and char data types is that the storage length is not the column length, but the data length.
Text Character Type The text data type is used to store a large amount of non-uniform encoding character data. This data type can contain a maximum of 2 billion to characters
Nchar Unified encoding type The nchar data type is used to store fixed-length, unified encoding data. Unified encoding uses a dual-byte structure to store each character, rather than a single byte (in common text ). It allows a large number of extended characters. This data type can store 4000 characters, doubling the byte space used
Nvarchar Unified encoding type The nvarchar data type is used as a variable-length uniform encoding type data. This data type can store 4000 characters, and the byte space is doubled.
Ntext Unified encoding type The ntext data type is used to store a large amount of uniform encoding data. This data type can store 230-1 or nearly 1 billion characters, and the byte space is doubled.
Binary Binary data type The binary data type is used to store binary data that can be up to 8000 bytes in length. When the content of the input table is close to the same length, you should use this data type.
Varbinary Binary data type The varbinary data type is used to store up to 8000 bytes of long binary data. This type of data should be used when the content size of the input table is variable.
Image Binary data type The image data type is used to store variable-length binary data up to 2 billion-1-1 or approximately bytes.


1. when to use a fixed-length data type char instead of a variable-length data type varchar

varchar saves space than Char. When char does not reach the specified length, adding spaces to reach the specified length will result in a waste of storage space, which is slightly less efficient than Char. That is, to achieve efficiency, a certain amount of space must be sacrificed, this is what we often say in database design, "Change space for efficiency '.

although varchar saves space than char, if a varchar column is often modified and the length of the data to be modified is different each time, this will cause row migration, and this will cause redundant I/O, which should be avoided during database design and adjustment, in this case, it is better to replace varchar with Char.

2. varchar and nvarchar

varchar uses non-uniform encoding for storage. For example, an English character occupies one byte, A Chinese Character occupies two bytes

nvarchar is stored in Unicode. For example, an English character occupies two bytes, A Chinese Character occupies two bytes

normally, you can use varchar to store Chinese characters, however, if the operating system is an English operating system and the Chinese font is not fully supported, if the Chinese character stored in SQL Server is varchar, garbled characters (displayed as?) will occur ??). In general, you do not know that this is because you use the wrong data type to store the data. You will try to install Chinese fonts, try to set the language environment of the operating system... these cannot solve the problem. The only solution is to set the database field type to nvarchar (or nchar ).

it can be seen that if you want to store multiple encoding characters in a field, nvarchar is recommended. In this case, nvarchar is safe to avoid unexpected Encoding Problems.

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.