📅  最后修改于: 2020-11-17 01:53:43             🧑  作者: Mango
数据类型指定特定的数据类型,例如整数,浮点数,布尔值等。它还标识该类型的可能值,可以对该类型执行的操作以及该类型的值的存储方式。在MySQL中,每个数据库表都有许多列,并且每个列都包含特定的数据类型。
我们可以确定MySQL中具有以下特征的数据类型:
MySQL支持各种类别的许多SQL标准数据类型。它使用许多不同的数据类型,这些数据类型可以分为以下几类:数字,日期和时间,字符串类型,空间类型和JSON数据类型。
MySQL具有所有必需的SQL数字数据类型。这些数据类型可以包括精确的数字数据类型(例如,整数,十进制,数字等),以及近似的数字数据类型(例如,浮点,实数和双精度)。它还支持BIT数据类型来存储位值。在MySQL中,数字数据类型分为两类,有符号的或无符号的(位数据类型除外)。
下表包含MySQL支持的所有数值数据类型:
Data Type Syntax | Description |
---|---|
TINYINT | It is a very small integer that can be signed or unsigned. If signed, the allowable range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. We can specify a width of up to 4 digits. It takes 1 byte for storage. |
SMALLINT | It is a small integer that can be signed or unsigned. If signed, the allowable range is from -32768 to 32767. If unsigned, the allowable range is from 0 to 65535. We can specify a width of up to 5 digits. It requires 2 bytes for storage. |
MEDIUMINT | It is a medium-sized integer that can be signed or unsigned. If signed, the allowable range is from -8388608 to 8388607. If unsigned, the allowable range is from 0 to 16777215. We can specify a width of up to 9 digits. It requires 3 bytes for storage. |
INT | It is a normal-sized integer that can be signed or unsigned. If signed, the allowable range is from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to 4294967295. We can specify a width of up to 11 digits. It requires 4 bytes for storage. |
BIGINT | It is a large integer that can be signed or unsigned. If signed, the allowable range is from -9223372036854775808 to 9223372036854775807. If unsigned, the allowable range is from 0 to 18446744073709551615. We can specify a width of up to 20 digits. It requires 8 bytes for storage. |
FLOAT(m,d) | It is a floating-point number that cannot be unsigned. You can define the display length (m) and the number of decimals (d). This is not required and will default to 10,2, where 2 is the number of decimals, and 10 is the total number of digits (including decimals). Decimal precision can go to 24 places for a float type. It requires 2 bytes for storage. |
DOUBLE(m,d) | It is a double-precision floating-point number that cannot be unsigned. You can define the display length (m) and the number of decimals (d). This is not required and will default to 16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for a double. Real is a synonym for double. It requires 8 bytes for storage. |
DECIMAL(m,d) | An unpacked floating-point number that cannot be unsigned. In unpacked decimals, each decimal corresponds to one byte. Defining the display length (m) and the number of decimals (d) is required. Numeric is a synonym for decimal. |
BIT(m) | It is used for storing bit values into the table column. Here, M determines the number of bit per value that has a range of 1 to 64. |
BOOL | It is used only for the true and false condition. It considered numeric value 1 as true and 0 as false. |
BOOLEAN | It is Similar to the BOOL. |
此数据类型用于表示时间值,例如日期,时间,日期时间,时间戳和年份。每个时间类型都包含值,包括零。当我们插入无效值时,MySQL无法表示它,然后使用零值。
下表说明了MySQL支持的所有日期和时间数据类型:
Data Type Syntax | Maximum Size | Explanation |
---|---|---|
YEAR[(2|4)] | Year value as 2 digits or 4 digits. | The default is 4 digits. It takes 1 byte for storage. |
DATE | Values range from ‘1000-01-01’ to ‘9999-12-31’. | Displayed as ‘yyyy-mm-dd’. It takes 3 bytes for storage. |
TIME | Values range from ‘-838:59:59’ to ‘838:59:59’. | Displayed as ‘HH:MM:SS’. It takes 3 bytes plus fractional seconds for storage. |
DATETIME | Values range from ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. | Displayed as ‘yyyy-mm-dd hh:mm:ss’. It takes 5 bytes plus fractional seconds for storage. |
TIMESTAMP(m) | Values range from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ TC. | Displayed as ‘YYYY-MM-DD HH:MM:SS’. It takes 4 bytes plus fractional seconds for storage. |
字符串数据类型用于保存纯文本和二进制数据,例如文件,图像等。MySQL可以基于模式匹配(例如LIKE运算符,正则表达式等)执行字符串值的搜索和比较。
下表说明了MySQL支持的所有字符串数据类型:
Data Type Syntax | Maximum Size | Explanation |
---|---|---|
CHAR(size) | It can have a maximum size of 255 characters. | Here size is the number of characters to store. Fixed-length strings. Space padded on the right to equal size characters. |
VARCHAR(size) | It can have a maximum size of 255 characters. | Here size is the number of characters to store. Variable-length string. |
TINYTEXT(size) | It can have a maximum size of 255 characters. | Here size is the number of characters to store. |
TEXT(size) | Maximum size of 65,535 characters. | Here size is the number of characters to store. |
MEDIUMTEXT(size) | It can have a maximum size of 16,777,215 characters. | Here size is the number of characters to store. |
LONGTEXT(size) | It can have a maximum size of 4GB or 4,294,967,295 characters. | Here size is the number of characters to store. |
BINARY(size) | It can have a maximum size of 255 characters. | Here size is the number of binary characters to store. Fixed-length strings. Space padded on the right to equal size characters. (introduced in MySQL 4.1.2) |
VARBINARY(size) | It can have a maximum size of 255 characters. | Here size is the number of characters to store. Variable-length string. (introduced in MySQL 4.1.2) |
ENUM | It takes 1 or 2 bytes that depend on the number of enumeration values. An ENUM can have a maximum of 65,535 values. | It is short for enumeration, which means that each column may have one of the specified possible values. It uses numeric indexes (1, 2, 3…) to represent string values. |
SET | It takes 1, 2, 3, 4, or 8 bytes that depends on the number of set members. It can store a maximum of 64 members. | It can hold zero or more, or any number of string values. They must be chosen from a predefined list of values specified during table creation. |
MySQL中的BLOB是一种数据类型,可以保存可变数量的数据。根据可以容纳的最大长度,将它们分为四种不同的类型。
下表显示了MySQL支持的所有Binary Large Object数据类型:
Data Type Syntax | Maximum Size |
---|---|
TINYBLOB | It can hold a maximum size of 255 bytes. |
BLOB(size) | It can hold the maximum size of 65,535 bytes. |
MEDIUMBLOB | It can hold the maximum size of 16,777,215 bytes. |
LONGBLOB | It can hold the maximum size of 4gb or 4,294,967,295 bytes. |
它是一种特殊的数据类型,用于保存各种几何和地理值。它对应于OpenGIS类。下表显示了MySQL支持的所有空间类型:
Data Types | Descriptions |
---|---|
GEOMETRY | It is a point or aggregate of points that can hold spatial values of any type that has a location. |
POINT | A point in geometry represents a single location. It stores the values of X, Y coordinates. |
POLYGON | It is a planar surface that represents multisided geometry. It can be defined by zero or more interior boundary and only one exterior boundary. |
LINESTRING | It is a curve that has one or more point values. If it contains only two points, it always represents Line. |
GEOMETRYCOLLECTION | It is a kind of geometry that has a collection of zero or more geometry values. |
MULTILINESTRING | It is a multi-curve geometry that has a collection of linestring values. |
MULTIPOINT | It is a collection of multiple point elements. Here, the point cannot be connected or ordered in any way. |
MULTIPLYGON | It is a multisurface object that represents a collection of multiple polygon elements. It is a type of two-dimensional geometry. |
MySQL从v5.7.8版本开始支持本机JSON数据类型。这种数据类型使我们能够快速有效地存储和访问JSON文档。
与将JSON格式的字符串存储在字符串列中相比,JSON数据类型具有以下优点: