SQL Server数据类型概述

In this article, we will give an overview of various SQL Server data types.

在本文中,我们将概述各种SQL Server数据类型。

介绍 (Introduction)

In SQL Server, a data type defines the type of data in a table column or variable. It is a mandatory and essential step in designing a table. A table with inappropriate data types can lead to various issues such as improper query optimization, performance issues, data truncation.

在SQL Server中,数据类型定义表列或变量中的数据类型。 这是设计表时必须执行的必不可少的步骤。 数据类型不合适的表可能导致各种问题,例如查询优化不正确,性能问题,数据截断。

To understand SQL Server data types, let’s look at the following page to create a new Google account (for reference purpose only):

为了了解SQL Server数据类型,让我们看下面的页面来创建一个新的Google帐户(仅供参考):

Google sign up page

It requires the following inputs:

它需要以下输入:

  • First name and Last name: Suppose we require only alphabets in these fields

    名和姓:假设我们在这些字段中只需要字母
  • Username: We can use letter, numbers and periods in the user name

    用户名:我们可以在用户名中使用字母,数字和句点
  • Password: We can use alphabets, numbers, and special characters

    密码:我们可以使用字母,数字和特殊字符

We can consider a few more examples:

我们可以考虑更多示例:

  • Contact Number: It should be a numeric field

    联系电话:应为数字字段
  • Pin Code: We might expect both alphabets and numeric fields in this column

    PIN码:我们可能希望此列中同时包含字母和数字字段

We store all these columns data in a table of SQL Server. We can use various data types in SQL Server and use them as per the requirement. Let’s explore the SQL Server data types.

我们将所有这些列数据存储在SQL Server表中。 我们可以在SQL Server中使用各种数据类型,并根据需要使用它们。 让我们探讨一下SQL Server数据类型。

SQL Server数据类型的类别 (Categories of SQL Server data types)

SQL Server supports the following data type’s categories:

SQL Server支持以下数据类型的类别:

  • Exact numeric: bit, tinyint, smallint, int, bigint, decimal, numeric, money and smallmoney 精确数字 :位,tinyint,smallint,int,bigint,十进制,数字,货币和smallmoney
  • Approximate numeric: Read and float 近似数值:读取和浮动
  • Date and time: date, DateTime, datetime2, datetimeoffset, smalldatetime, time 日期和时间:日期,DateTime,datetime2,datetimeoffset,smalldatetime,时间
  • Character strings:char, varchar, text 字符串 :char,varchar,text
  • Unicode character strings: Nchar, Nvarchar, Ntext Unicode字符串 :Nchar,Nvarchar,Ntext
  • Binary strings: Binary, image and varbinary 二进制字符串:二进制,图像和varbinary
  • Other data types: Cursor, hierarchyid, sql_variant, table, rowversion, uniqueidentifier, XML, Spatial and geography 其他数据类型:游标,hierarchyid,sql_variant,表,行版本,唯一标识符,XML,空间和地理

Categories of SQL Server Data Types

Note: SQL Server will deprecate ntext, text and image data type in future releases. We should avoid the use of these data types. You can use Nvarchar(max), varchar(max), and varbinary(max).

注意:在将来的版本中,SQL Server将弃用ntext,text和image数据类型。 我们应该避免使用这些数据类型。 您可以使用Nvarchar(max), varchar(max)varbinary(max)。

确切的数字SQL Server数据类型 (Exact numeric SQL Server data type)

We use exact numeric data types for integer, decimal, and money. Each data type has its own lower, upper limit and memory requirements. We should use the smallest data type to save memory requirements as well. For example, we can use the bit data type for storing true (1) or false (0) values.

我们对整数,小数和货币使用精确的数值数据类型。 每种数据类型都有自己的下限,上限和内存要求。 我们也应该使用最小的数据类型来节省内存需求。 例如,我们可以使用位数据类型来存储true(1)或false(0)值。

Data Type

Lower Range

Upper Range

Storage

Remarks

Bit

0

1

1 byte

We can also store NULL values in this.

tinyint

0

255

1 byte

We can store whole numbers up to 255 in this data type.

Smallint

-2^15 (-32,768)

2^15-1 (32,767)

2 bytes

We can store whole numbers between a lower and higher range.

Int

−2^31 (−2,147, 483,648)

2^31−1 (−2,147, 483,647

4 bytes

It also stores the whole number similar to a smallint but its lower and upper limits changes as defined.

Bigint

−2^63 (−9,223,372, 036,854,775,808)

2^63−1 (−9,223,372, 036,854,775,807)

8 bytes

We should use bigint data type if we cannot accommodate data in the integer data type.

Decimal

−10^38+1

10^381−1

It depends upon precision.

1 – 9 -> 5 bytes

10-19->9 bytes

20-28->13 bytes

29-28->17 bytes

We use decimal data type for

scale and fixed precision numbers.

Numeric

−10^38+1

10^381−1

It depends upon precision.

1 – 9 -> 5 bytes

10-19->9 bytes

20-28->13 bytes

29-28->17 bytes

Decimal and numeric are synonyms. We can use them interchangeably.

Smallmoney

-214,478.3648

+214,478.3647

4 bytes

We can use this data type for monetary or currency values.

Money

−922,337, 203, 685,477.5808

+922,337, 203, 685,477.5807

8 bytes

数据类型

下限范围

上限范围

存储

备注

0

1个

1个字节

我们还可以在其中存储NULL值。

tinyint

0

255

1个字节

在此数据类型中,我们最多可以存储255个整数。

Smallint

-2 ^ 15(-32,768)

2 ^ 15-1(32,767)

2字节

我们可以在较低和较高范围之间存储整数。

整数

−2 ^ 31(−2,147,483,648)

2 ^ 31−1(−2,147,483,647

4字节

它还存储类似于smallint的整数,但是其下限和上限会按定义变化。

比金特

−2 ^ 63(−9,223,372,036,854,775,808)

2 ^ 63−1(−9,223,372,036,854,775,807)

8字节

如果无法容纳整数数据类型的数据,则应使用bigint数据类型。

小数

−10 ^ 38 + 1

10 ^ 381−1

这取决于精度。

1 – 9-> 5个字节

10-19-> 9字节

20-28-> 13个字节

29-28-> 17个字节

我们使用十进制数据类型

比例尺和固定精度数字。

数字

−10 ^ 38 + 1

10 ^ 381−1

这取决于精度。

1 – 9-> 5个字节

10-19-> 9字节

20-28-> 13个字节

29-28-> 17个字节

小数和数字是同义词。 我们可以互换使用它们。

小钱

-214,478.3648

+214,478.3647

4字节

我们可以将此数据类型用于货币或货币值。

−922,337,203,685,477.5808

+922,337,203,685,477.5807

8字节

近似数值SQL Server数据类型 (Approximate numeric SQL Server data type)

Data Type

Lower Range

Upper Range

Storage

Remarks

Real

−3.40E+38

3.40E+38

4 bytes

We can use float924) as ISO synonym for real.

Float(n)

−1.79E+308

1.79E+308

Its storage depends upon value (n)

N(1-24) ->4 bytes

N(25-53)->8 bytes

It is an Approximate-number data types.

The default value of N is 53.

数据类型

下限范围

上限范围

存储

备注

真实

−3.40E + 38

3.40E + 38

4字节

我们可以使用float924)作为real的 ISO同义词。

浮点数

−1.79E + 308

1.79E + 308

它的存储量取决于值(n)

N(1-24)-> 4个字节

N(25-53)-> 8个字节

它是一个近似数数据类型。

N的默认值为53。

日期和时间SQL Server数据类型 (Date and Time SQL Server Data types)

We stored date and time data along with time offset using these data types.

我们使用这些数据类型存储了日期和时间数据以及时间偏移。

Data Type

Lower Range

Upper Range

Storage

Remarks

Date

0001-01-01

9999-12-31

3 bytes

1. It stores only dates in SQL Server.

2. Its default value is 1900-01-01.

3. It provides default format

YYYY-MM-DD.

Datetime

1753-01-01

9999-12-31

8 bytes

1. We can define a date along with time with fractional seconds.

2. The default value for this data type is 1900-01-01 00:00:00.

3.It provides accuracy in increments of .000, .003, or .007 seconds.

4. We should avoid using this data type. We can use Datetime2 instead.

Datetime2

0001-01-01 00:00:00

9999-12-31 23:59:59.9999999

6-8 bytes

1. Precision<3 -> 6 bytes

2.Precision 3 or 4-> 7 bytes

1. the default format for this is YYYY-MM-DD hh:mm: ss[.fractional seconds].

2. It provides precision from 0 to 7 digits, with an accuracy of 100ns.

2. The default precision for datetime2 is 7 digits.

Datetimeoffset

0001-01-01 00:00:00

9999-12-31 23:59:59.9999999

10 bytes

1. It is similar to a datetime2 data type but includes time zone offset as well.

2. Timezone offset is -14:00 through +14:00.

smalldatetime

1900-01-01 00:00:00

2079-06-06 23:59:59

4 bytes

1. It defines a date with the time of the day.

2. Its default value is 1900-01-01 00:00:00.

3. It provides an accuracy of one minute.

Time

00:00:00.0000000

23:59:59.9999999

5 bytes

1. We can use it for storing only time data.

2. Its default format is hh:mm:ss[.nnnnnnn].

3. It provides an accuracy of 100 nanoseconds.

数据类型

下限范围

上限范围

存储

备注

日期

0001-01-01

9999-12-31

3个字节

1.它仅将日期存储在SQL Server中。

2.其默认值为1900-01-01。

3.提供默认格式

YYYY-MM-DD。

约会时间

1753-01-01

9999-12-31

8字节

1.我们可以定义日期和时间以及小数秒。

2.此数据类型的默认值为1900-01-01 00:00:00。

3.它提供的精度为.000,.003或.007秒。

4.我们应避免使用此数据类型。 我们可以改用Datetime2。

日期时间2

0001-01-01 00:00:00

9999-12-31 23:59:59.9999999

6-8个字节

1.精度<3-> 6字节

2.精度3或4-> 7个字节

1.此文件的默认格式为YYYY-MM-DD hh:mm:ss [.fractional seconds]。

2.它提供0到7位数字的精度,精度为100ns。

2. datetime2的默认精度为7位数字。

日期时间偏移

0001-01-01 00:00:00

9999-12-31 23:59:59.9999999

10字节

1.它类似于datetime2数据类型,但还包括时区偏移量。

2.时区偏移量是-14:00到+14:00。

小日期时间

1900-01-01 00:00:00

2079-06-06 23:59:59

4字节

1.它用一天中的时间定义一个日期。

2.其默认值为1900-01-01 00:00:00。

3.它提供一分钟的准确性。

时间

00:00:00.0000000

23:59:59.9999999

5字节

1.我们可以将其仅用于存储时间数据。

2.其默认格式为hh:mm:ss [.nnnnnnn]。

3.它提供100纳秒的精度。

字符串SQL Server数据类型 (Character Strings SQL Server Data types)

Data Type

Lower Range

Upper Range

Storage

Remarks

Char(n)

0 characters

8000 characters

N bytes

1. It provides a fixed-width character data type.

Varchar(n)

0 characters

8000 characters

n bytes + 2 bytes

1.It is a variable length character data type.

2.N defines the string size.

Varchar (max)

0 characters

2^31 chars

n bytes + 2 bytes ~ 2 GB

We should avoid using this data type unless required due to its huge storage requirement.

Text

0 chars

2,147,483,647 chars

n bytes + 4 bytes

1. It is a variable-length character data type.

2. We should avoid using this data type as it might get deprecated in future versions of SQL Server.

数据类型

下限范围

上限范围

存储

备注

字符(n)

0个字符

8000个字符

N字节

1.提供固定宽度的字符数据类型。

Varchar(n)

0个字符

8000个字符

n字节+ 2字节

1.这是一个可变长度的字符数据类型。

2.N定义字符串大小。

Varchar(最大值)

0个字符

2 ^ 31个字符

n字节+ 2字节〜2 GB

由于其巨大的存储需求,除非有必要,否则应避免使用此数据类型。

文本

0个字符

2,147,483,647字符

n字节+ 4字节

1.它是可变长度字符数据类型。

2.我们应该避免使用此数据类型,因为在将来SQL Server版本中可能会不建议使用该数据类型。

Unicode字符串SQL Server数据类型 (Unicode character string SQL Server data types)

Data Type

Lower Range

Upper Range

Storage

Remarks

Nchar

0 characters

4000 characters

2 times n bytes

It is a Unicode string of fixed width.

Nvarchar

0 chars

4000 Chars

2 times n bytes

Nvarchar is a Unicode string of variable width.

Ntext

0 chars

1,073,741,823 char

2 times the string length

1. It is a Variable-length Unicode data

2. We should avoid using this data type as it will be deprecated in future SQL releases.

数据类型

下限范围

上限范围

存储

备注

恩查尔

0个字符

4000个字符

2乘以n字节

它是固定宽度的Unicode字符串。

Nvarchar

0个字符

4000个字符

2乘以n字节

Nvarchar是可变宽度的Unicode字符串。

文字

0个字符

1,073,741,823字符

字符串长度的2倍

1.它是可变长度的Unicode数据

2.我们应避免使用此数据类型,因为在将来SQL版本中将不建议使用该数据类型。

二进制SQL Server数据类型 (Binary SQL Server data types)

Data Type

Lower Range

Upper Range

Storage

Remarks

Binary

0 bytes

8000 bytes

N bytes

This data type is a fixed-width binary string.

varbinary

0 bytes

8000 bytes

Its storage is the actual length of string + 2 bytes.

Image

0 bytes

2,147,483,647 bytes

Avoid using this data type, as it will be deprecated in future SQL Server releases.

数据类型

下限范围

上限范围

存储

备注

二元

0字节

8000字节

N字节

此数据类型是固定宽度的二进制字符串。

varbinary

0字节

8000字节

它的存储空间是字符串的实际长度+ 2个字节。

图片

0字节

2,147,483,647字节

避免使用此数据类型,因为它将在将来SQL Server版本中弃用。

其他数据类型 (Other data types)

There are few data types as well that can be used as per the requirement:

根据要求,也很少有数据类型可以使用:

  • Cursor: It is useful for variables or stored procedure OUTPUT parameter referencing to a cursor 游标 :对于引用游标的变量或存储过程OUTPUT参数很有用
  • Rowversion: It returns automatically generated, unique binary numbers within a database Rowversion :它返回数据库中自动生成的唯一二进制数
  • Hierarchyid: it is a system data type with variable length. We use it to represent a position in a hierarchy
  • Hierarchyid :它是长度可变的系统数据类型。 我们用它来表示层次结构中位置
  • Uniqueidentifier: It provides 16 bytes GUID Uniqueidentifier :它提供16字节的GUID
  • XML: It is a special data type for storing the XML data in SQL Server tables XML :这是一种特殊的数据类型,用于将XML数据存储在SQL Server表中
  • Spatial Geometry type: We can use this for representing data in a flat (Euclidean) coordinate system 空间几何类型 :我们可以使用它来表示平面(欧几里得)坐标系中的数据
  • Spatial Geography Types: We can use Spatial Geography type for storing ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates. It represents data in a round-earth coordinate system 空间地理类型:我们可以使用空间地理类型来存储椭球(地球)数据,例如GPS纬度和经度坐标。 它表示地球坐标系中的数据
  • Table: It is a special data type useful for storing result set temporarily in a table-valued function. We can use data from this for processing later. It can be used in functions, stored procedures, and batches Table :这是一种特殊的数据类型,可用于将结果集临时存储在表值函数中。 我们可以将其中的数据用于以后的处理。 可用于函数,存储过程和批处理中

结论 (Conclusion)

In this article, we explored SQL Server data types in brief. You should be aware of all these data types and use the appropriate data type for better storage, query processing, and data retrieval.

在本文中,我们简要探讨了SQL Server数据类型。 您应该了解所有这些数据类型,并使用适当的数据类型来更好地存储,查询处理和数据检索。

翻译自: https://www.sqlshack.com/an-overview-of-sql-server-data-types/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值