开门见山
MySQL作为一种通用的数据库服务程序,其主要功能是为上层应用提供通用的数据存储服务。那MySQL提供的数据存储服务支持哪些类型呢?这正是本文所叙。SQL语句help data types;可以查看MySQL支持的所有数据类型,如下:
mysql> help data types;
You asked for help about help category: "Data Types"
For more information, type 'help <item>', where <item> is one of the following
topics:
AUTO_INCREMENT
BIGINT
BINARY
BIT
BLOB
BLOB DATA TYPE
BOOLEAN
CHAR
CHAR BYTE
DATE
DATETIME
DEC
DECIMAL
DOUBLE
DOUBLE PRECISION
ENUM
FLOAT
INT
INTEGER
LONGBLOB
LONGTEXT
MEDIUMBLOB
MEDIUMINT
MEDIUMTEXT
SET DATA TYPE
SMALLINT
TEXT
TIME
TIMESTAMP
TINYBLOB
TINYINT
TINYTEXT
VARBINARY
VARCHAR
YEAR DATA TYPE
MySQL的数据类型大体可分为四类:
- numeric:数值类型
- Character(string):字符/字符串类型;
- Binary:二进制数据类型;
- Temporal(Date and Time Types):与时间和日期相关的类型;
下文将重点描述此四类数据类型。
数据类型
Numeric--数值类型
此大类包括TINYINT、SMALLINT、MEDIUMINT、INT等,其区别如下:
Class—子类 |
Type—具体类型 |
Description—描述 |
Integer |
TINYINT |
Very small integer data type |
Integer |
SMALLINT |
Small integer data type |
Integer |
MEDIUMINT |
Medium-sized integer data type |
Integer |
INT |
Normal- (average-) sized integer data type |
Integer |
BIGINT |
Large integer data type |
Floating-Point |
FLOAT |
single-precision (four-byte) floating-point number |
Floating-Point |
DOUBLE |
Normal, double-precision (eight-byte) floatingpoint number |
Fixed-Point |
DECIMAL |
Exact-value numbers that have an integer part, a fractional part, or both |
BIT |
BIT |
Bit-field values |
Character—字符类型
字符类型又可分为如下两子类:
Text: True, unstructured character string data types。--非结构化的字符串类型;
Integer: Structured string types for storing data from a fixed selection of allowed string values, represented internally as integers.---结构化字符串类型由于存储固定被允许的值,如枚举类型。其在MySQL内部以整型呈现(类似于C语言中的宏定义)。
字符类型的各个具体类型如下:
Class—子类 |
Type—具体类型 |
Description—描述 |
Text |
CHAR |
Fixed-length character string, up to a maximum of 255 characters |
Text |
VARCHAR |
Variable-length character string, up to a maximum of 65,535 characters |
Text |
TINYTEXT |
Variable-length character string, up to a maximum of 255 characters |
Text |
TEXT |
Variable-length character string, up to a maximum of 65,535 characters |
Text |
MEDIUMTEXT |
Variable-length character string, up to a maximum of 16,777,215 characters |
Text |
LONGTEXT |
Variable-length character string, up to a maximum of 4,294,967,295 characters |
Integer |
ENUM(枚举) |
Enumeration consisting of a fixed set of legal values |
Integer |
SET(集合) |
Set consisting of a fixed set of legal values |
在character—字符类型中,还涉及到字符集和字符校对的概念,具体可查看本人的博客:
Binary
Binary类型存储二进制比特串,又可分为两类:
Binary: Binary strings of both fixed and variable length;固定长度或可变长度的二进制字符串;
BLOB: Variable-length unstructured collection of binary data。可变长度的非结构化二进制数据集,如XML文档;
Class—子类 |
Type—具体类型 |
Description—描述 |
Binary |
BINARY |
Similar to the CHAR (fixed-length) type, but stores binary byte strings instead of nonbinary character strings |
Binary |
VARBINARY |
Similar to the VARCHAR (variable-length) type, but stores binary byte strings instead of nonbinary character strings |
BLOB |
TINYBLOB |
BLOB column with a maximum length of 255 bytes |
BLOB |
BLOB |
BLOB column with a maximum length of 65,535 bytes |
BLOB |
MEDIUMBLOB |
BLOB column with a maximum length of 16,777,215 bytes |
BLOB |
LONGBLOB |
BLOB column with a maximum length of 4,294,967,295 bytes |
Temporal
关于时间的类型,具体包括如下:
Type—类型 |
Format—格式 |
Example—举例 |
DATE |
YYYY-MM-DD |
2006-08-04 |
TIME |
hh:mm:ss[.uuuuuu] |
12:59:02.123456 |
DATETIME |
YYYY-MM-DD hh:mm:ss[.uuuuuu] |
2006-08-04 12:59:02.123 |
TIMESTAMP |
YYYY-MM-DD hh:mm:ss[.uuuuuu] |
2006-08-04 12:59:02.12 |
YEAR |
YYYY |
2006 |
其他
BOOLEAN:These types are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true。
Boolean类型与TINTINT(1)相同,即一个字节的小整型。其存储0值代表为false,非0值代表ture;
Setting Data Types to NULL
MySQL支持某些列为NULL,即为空值。关于什么时候适合用NULL,什么时候不适合用NULL可参阅如下说明。
- When to Use NULL
In the beginning stages of database design, when you are making a list of the data to be included, it becomes clear that some data may not be available for all columns. Examine these cases and determine whether null values should be allowed. Also, you can change this for an existing table if a problem is detected due to occurrences of null values in the column.
在数据库设计的开始阶段,当您列出要包含的数据时,很明显有些数据可能不适用于所有列。
- When Not to Use NULL
There are cases when you should not allow null values in a column. The most common case is when it is a primary key. Another example is any column that must have a value for the database design to make sense.
总结
MySQL的数据类型关系到数据占用的存储空间、SQL优化、数据精度等相关方面,如存储金融数据就不适合使用float或double数据类型(可以使用DECIMAL—固定精度),因为可能会发生数据圆整错误。掌握了MySQL的数据类型,即可在后续的应用开发中灵活的选择适合自己应用的类型,同时可以最大的使用/优化MySQL的性能。