Overview of SQL Server database data types

In SQL Server, when designing a table, you need to define the data type of the column (column) of the table. Inappropriate data types may cause various problems, such as query performance problems, data truncation (truncation). This article briefly introduces the data types supported by SQL Server.

SQL SERVER Microsoft official document https://docs.microsoft.com/zh-cn/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver15  , select the commonly used one here Brief summary introduction.

Character string:

type of data

description

Storage length (number of characters)

char(n)

Fixed-length character string. Up to 8,000 characters.

n <= 8000 characters

varchar(n)

Variable-length character string. Up to 8,000 characters.

0-n characters

varchar(max)

Variable-length character string. 1,073,741,824 characters at most.

0-max character

text

Variable-length character string. Up to 2GB character data. [Not recommended, will be deprecated]

0-2GB characters

 

Unicode string:

type of data

description

Storage length (number of characters)

nchar(n)

Fixed-length Unicode data. Up to 4,000 characters.

 n <=4000 characters

nvarchar(n)

Variable-length Unicode data. Up to 4,000 characters.

 0-n <=4000 

nvarchar(max)

Variable-length Unicode data. Maximum of 536,870,912 characters.

 0-536,870,912

ntext

Variable-length Unicode data. Up to 2GB character data. [Not recommended, will be deprecated]

 0-2GB characters

In particular, the difference between varchar and nvarchar:

Varchar uses single byte to store data in SQL Server, and nvarchar uses Unicode double byte to store data.

English characters occupy one byte. When storing, if the field type is varchar, it will only occupy one byte, and if the field type is nvarchar, it will occupy two bytes.

Chinese characters occupies two bytes. When storing, no matter whether the field type is varchar or nvarchar, it occupies two bytes (usually Unicode encoding).

[Note: Unicode (Unicode, Universal Code, Single Code) is a character encoding used on computers. It sets a unified and unique binary code for each character in each language to meet the requirements of cross-language and cross-platform text conversion and processing. 】 

Under normal circumstances, varchar can also store Chinese characters, but if the operating system is an English operating system and the support for Chinese fonts is not comprehensive, garbled characters (displayed as??) will appear when the Chinese characters are stored as varchar in SQL Server;

The solution to the problem is to change the type of the database field to nvarchar (or nchar).

Using the nvarchar type, you can solve the character set compatibility problem (don't worry about the Chinese garbled problem), and you don't need to consider the difference between Chinese and English characters when judging a string. The disadvantage is that it will double the storage space.

So generally speaking, if it may contain Chinese characters, use nchar/nvarchar to store, if pure English and numbers (guaranteed that Chinese is not included), use char/varchar to store.

In addition, we need to add that the use of varchar to store fields is likely to cause implicit conversion;

     The example is as follows: create an AAA table, insert 1000 pieces of test data, and build an index on name1, and then execute the following two lines of SQL statements, check the execution plan, and find that the second one has more overhead for constant Scan.

DECLARE @name VARCHAR(100) SET @name ='张三500' SELECT * FROM AAA WHERE name1 = @name --varchar storage, the parameter type is varchar, there is no implicit conversion

DECLARE @name NVARCHAR(100) SET @name ='张三500' SELECT * FROM AAA WHERE name1 = @name --varchar storage, the parameter type is nvarchar, there is an implicit conversion

Note: Sometimes it appears that the nvarchar data type is used, but the system still has the problem of Chinese garbled or special symbol garbled; at this time, add N in front of the updated or inserted field value; (if it is of varchar type, add N is not easy).

So what is this N used for? There is an uppercase N in front of the string parameter value, and its meaning is to indicate that the string in the quotation marks is unicode.

To sum up: 1. For the Chinese garbled problem, first ensure that the column type is nvarchar; 2. Then use "N" to ensure that the data is unicode when inserting data. In this way, inexplicable question marks, Chinese garbled characters or special symbols garbled will not be displayed.

 

Binary type:

type of data

description

Storage length (number of characters)

bit

Allow 0, 1, or NULL

 1 byte

binary(n)

Fixed-length binary data. Up to 8,000 bytes.

 n <= 8000 bytes

varbinary(n)

Variable-length binary data. Up to 8,000 bytes.

 0-n bytes

varbinary(max)

Variable-length binary data. Up to 2GB bytes.

 0-2GB bytes

image

Variable-length binary data. Up to 2GB. [Not recommended, will be deprecated]

 0-2GB bytes

The bit type is special. In SQL Server 2008, when using the SSMS visual tool to assign a value to a bit type column, you need to enter True or False, and when querying data, it is displayed as 1 or 0. When using SQL statements to add data, you need to use 1 and 0 to indicate.

The larger the byte, the larger the integer range. If it exceeds the byte range, it will overflow.

 

Number type:

type of data

description

Storage length (number of characters)

tinyint

All numbers from 0 to 255 are allowed.

1 byte

smallint

All numbers from -32,768 to 32,767 are allowed.

2 bytes

int

All numbers from -2,147,483,648 to 2147483647 are allowed.

4 bytes

bigint

All numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 are allowed.

8 bytes

decimal(p,s)

Numbers with fixed precision and scale. Allow numbers from -10^38 +1 to 10^38 -1.

The p parameter indicates the maximum number of digits that can be stored (to the left and right of the decimal point). p must be a value between 1 and 38. The default is 18.

The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value between 0 and p. The default is 0.

5-17 bytes

numeric(p,s)

Numbers with fixed precision and scale. Allow numbers from -10^38 +1 to 10^38 -1.

The p parameter indicates the maximum number of digits that can be stored (to the left and right of the decimal point). p must be a value between 1 and 38. The default is 18.

The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value between 0 and p. The default is 0.

5-17 bytes

smallmoney

Currency data between -214,748.3648 and 214,748.3647.

4 bytes

money

Currency data between -922,337,203,685,477.5808 and 922,337,203,685,477.5807.

8 bytes

float(n)

Floating precision digital data from -1.79E + 308 to 1.79E + 308. The parameter n indicates whether the field holds 4 bytes or 8 bytes. float(24) holds 4 bytes, while float(53) holds 8 bytes. The default value of n is 53.

4 or 8 bytes

real

Numerical data with floating precision from -3.40E + 38 to 3.40E + 38.

4 bytes

Float and real are the floating-point types of SQL Server, where float represents a double-precision floating-point data type, and real represents a single-precision floating-point data type. The stored value is approximate and cannot be used for equivalent comparison.

The exact data type of SQL Server is represented by Decimal or Numeric. The two are exactly the same. It is recommended to use the Decimal type. Decimal(5,3) means that the integer part and the decimal part are up to 5 digits in total. The decimal point is up to 3 digits, and the integer digits are up to 2 digits. If you enter an integer part of more than 2 digits, an error will occur. If the number exceeds the specified number of digits, it will be automatically rounded.

SQL Server currency data types are represented by Smallmoney and money. The data value consists of an integer part and a decimal part, and the decimal part is accurate to 4 digits. If there are more than 4 decimal places, it will be automatically rounded.

 

Date type:

type of data

description

存储长度(字符数)

datetime

从 1753 年 1 月 1 日 到 9999 年 12 月 31 日,精度为 3.33 毫秒。

8 bytes

datetime2

从 1753 年 1 月 1 日 到 9999 年 12 月 31 日,精度为 100 纳秒。

6-8 bytes

smalldatetime

从 1900 年 1 月 1 日 到 2079 年 6 月 6 日,精度为 1 分钟。

4 bytes

date

仅存储日期。从 0001 年 1 月 1 日 到 9999 年 12 月 31 日。

3 bytes

time

仅存储时间。精度为 100 纳秒。

3-5 bytes

datetimeoffset

与 datetime2 相同,外加时区偏移。

8-10 bytes

timestamp

存储唯一的数字,每当创建或修改某行时,该数字会更新。timestamp 基于内部时钟,不对应真实时间。每个表只能有一个 timestamp 变量。

 8 bytes

 

其他数据类型:

数据类型

描述

sql_variant

存储最多 8,000 字节不同数据类型的数据,除了 text、ntext 以及 timestamp。

uniqueidentifier

存储全局标识符 (GUID)。GUID(Globally Unique Identifier)是一个通过特定算法产生的二进制长度为128位的数字标识符,用于指示产品的唯一性。

xml

存储 XML(eXtensible Markup Language)格式化数据,用来传输和存储数据。最多 2GB。

cursor

Cursor(游标)提供了在结果集中一次以行或者多行前进或向后浏览数据的能力。我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许用户对指定位置的数据进行处理。

table

存储结果集,供稍后处理。

 

附录、

SQL在线练习网站 https://www.liaoxuefeng.com/wiki/1177760294764384/1179611432985088

提示:没办法保存,退出去内容就没了。

 

☆创建数据表的语法:
CREATE TABLE table_name (column_name column_type);

建立Persons表
CREATE TABLE Persons
(
PersonID int,
Name varchar(40),
Address varchar(80),
phone varchar(20)
);

☆向表中插入新记录。格式
INSERT INTO table_name
VALUES (value1,value2,value3,...);

INSERT INTO table_name(column1,column2,column3,...)
VALUES (value1,value2,value3,...);
如:
INSERT INTO Persons(PersonID,Name,Address,phone) VALUES(1,"张三","X1市a1区b1街c1号","11111111");
INSERT INTO Persons(PersonID,Name,Address,phone) VALUES(2,"李四","X1市a2区b2街c1号","22222222");
INSERT INTO Persons(PersonID,Name,Address,phone) VALUES(3,"王五","X2市m1区n1街M1号","33333333");

【注意,其中的逗号、引号是英文的】

☆查询——从表中读取数据
SELECT column_name,column_name
FROM table_name;

SELECT * FROM table_name;
如:
SELECT * FROM Persons;

☆条件查询,在查询语句后附上
WHERE 条件;

SELECT * FROM Persons
WHERE PersonID=2;

☆删除表
语法:
drop table 表名称;

drop table Persons;

 

Guess you like

Origin blog.csdn.net/cnds123/article/details/114830086