Article Directory
2.3 Data Sheet
2.3.1 Data Type
Data types in SQL Server 2005:
2.3.1.1 Exact numbers
Types of:
- Integer:
type of data | range | storage |
---|---|---|
int | -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647) | 4 bytes |
bigint | -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807) | 8 bytes |
smallint | -2^15 (-32,768) to 2^15-1(32,767) | 2 bytes |
tinyint | 0 to 255 | 1 byte |
bit | 1,0(true,flase) | If there are 8 or less bit columns in the table, these columns share 1 byte for storage; if there are 9 to 16 bit columns, these columns are stored by 2 bytes, and so on. |
- Numerical values with fixed precision and decimal places:
decimal and numeric (the two are equivalent)- Representation method: decimal[(p[,s])] and numeric[(p[,s])]
- p (precision, precision): The total number of decimal digits that can be stored at most, including the digits to the left and right of the decimal point. The default precision is 18
- s(scale, number of decimal places): The maximum number of decimal digits that can be stored to the right of the decimal point. The number of decimal places must be a value from 0 to p, that is, 0<=s<=p. The default number of decimal places is 0
Currency or monetary value:
2.3.1.2 Approximate numbers
Types of:
float | real |
---|
Role: used to indicate approximate values (approximate values). Use when the size and number of digits are not clear, such as the result of dividing two trees, the result of square rooting, etc.
type of data | range | storage |
---|---|---|
float(n) | -1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308 | Depends on the value of n |
real | -3.40+38 to -1.18E-38, 0 and 1.18E-38 to 3.40E+38 | 4 bytes |
2.3.1.3 Date and time
Types of:
detetime | smalldatetime |
---|
Function: A data type that represents the date and time of a certain day.
type of data | range | Accuracy | storage |
---|---|---|---|
datetime | January 1, 1753 to December 31, 9999 | 3.33 ms | 2*4 bytes |
smalldatetime | January 1, 1900 to June 6, 2079 | 1 minute | 2*2 bytes |
2.3.1.4 String
Types of:
char | text |
---|---|
varchar |
type of data | range | storage |
---|---|---|
char(n) | Fixed length, the length is n bytes. The value of n ranges from 1 to 8,000 | n bytes |
varchar(n/max) | Variable length, the value of n ranges from 1 to 8,000. max indicates that the maximum storage size is 2^31-1 bytes. | The actual length of the input data plus 2 bytes |
text (will be cancelled in the future) | Variable-length character data, the maximum length is 2^31-1 characters. | Maximum 2^31-1 bytes |
The choice of char and varchar:
- If the size of the column data items is consistent, char is used.
- If the size of the column data items differs considerably, use varchar.
- If the size of the column data items differs greatly, and the size may exceed 8,000 bytes, use varchar(max).
Type selection of long numeric data (such as student ID): generally select string
- Number type has length limitation;
- When using the numeric type, the leading zero of the data will be discarded;
- Data splicing is inconvenient;
- It is inconvenient to extract, replace, and modify certain digits in the data.
Choice of char(1), tinyint and bit:
2.3.1.5 Unicode strings
Unicode: Universal Chatacter Set (Universal Character Set)
Type:
nchar | ntext |
---|---|
nvarchar |
type of data | range | storage |
---|---|---|
nchar(n) | Fixed-length Unicode character data of n characters. The value of n must be between 1 and 4,000. | 2*n bytes |
nvarchar(n/max) | Variable-length Unicode character data. The value of n is between 1 and 4,000. max indicates that the maximum storage size is 2^31-1 characters | Double the number of input characters + 2 bytes |
ntext (will be cancelled in the future) | Variable-length Unicode data, the maximum length is 2^30-1 characters. | Enter twice the number of characters |
The difference between char and nchar:
- Scope of application
- storage
- 1 length of nchar can represent 1 letter, number, symbol, Chinese character, 1 length of char can only represent 1 letter, number, symbol, 1 length of char can only represent 1 letter, number, symbol, Chinese character To use 2 lengths;
- When querying and comparing data, the space at the end of nchar is meaningful.
2.3.1.6 Binary string
Types of:
binary | image |
---|---|
varbinary |
type of data | range | storage |
---|---|---|
binary(n) | Fixed-length binary data with a length of n bytes, where n is a value from 1 to 8,000. | n bytes |
varbinary(n/max) | Variable-length binary data. n can take a value from 1 to 8,000. max indicates that the maximum storage size is 2^31-1 bytes. | The actual length of the input data plus 2 bytes |
image (will be cancelled in the future) | Variable length binary data, from 0 to 2^31-1 bytes | Maximum 2^31-1 bytes |
Storage method of multimedia data such as pictures:
- Store content in the table: use varbinary(max) to store.
- Write data: convert the original data into binary data and store it in the table;
- Read data: read out binary data and convert it into original data.
- Store the path in the table:
- relative path
- Absolute path
2.3.1.7 Other data types
Types of:
cursor | timestamp |
---|---|
sql_variant | uniqueidentifier |
table | xml |
type of data | effect |
---|---|
cuesor cursor |
A data type of variable or stored procedure OUTPUT parameter |
sql_variant variable type |
Used to store the values of various data types supported by SQL05 (excluding text, ntext, image, timestamp and sql_variant). |
table 临时表 |
用于临时存储一组作为表值函数的结果集返回的行。 |
数据类型 | 作用 | 存储 |
---|---|---|
timestamp 时间戳 |
给表加时间戳(每个数据库都有一个)计数器,当对数据库中 包含timestamp列的表执行插入或更新操作时,该计数器值就会增加。 |
8字节 |
uniqueidentifier | Globally Unique Identifier(GUID,全球唯一标识符) | 16字节 |
xml | 存储XML数据 | 最大2GB |
2.3.1.8 用户自定义数据类型
⭐使用SQL Server Management Studio
- 方法:“对象资源管理器” → “数据库” → 选择数据库名称 → “可编程性” → “类型” → “用户定义数据类型” → 右键 → “新建用户定义的数据类型(N)…”。
2.3.1.9 常用数据类型(牢记)
数据类型 | 数据类新 | ||
---|---|---|---|
精确数字 | int decimal monkey |
字符串 | char varchar |
近似数字 | float real |
Unicode 字符串 |
nchar nvarchar |
日期时间 | datetime | 二进制字符串 | varbinary |
Sql Server 字段类型与C#数据类型对应表
Sql Server类型 | C#类型 | Sql Server类型 | C#类型 |
---|---|---|---|
image、binary、varbinary | byte[] | money、decimal、numeric | decimal |
text、ntext、 char、varchar、 cchar、nvarchar |
string | real | single |
int | int | float | double |
tnyint | byte | bit | bool |
smallint | int16 | bigint | int64 |
datetime、timestamp | DateTime |