Straight to the point
As a general database service program, MySQL's main function is to provide general data storage services for upper-level applications . What types of data storage services does MySQL provide? This is exactly what this article describes. The SQL statement help data types; you can view all the data types supported by MySQL , as follows:
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 data types can be roughly divided into four categories:
- numeric: numeric type
- Character (string): character/string type;
- Binary: binary data type;
- Temporal (Date and Time Types): Types related to time and date;
The following will focus on these four types of data types.
type of data
Numeric--numeric type
This category includes TINYINT, SMALLINT, MEDIUMINT, INT, etc. The differences are as follows:
Class —Subclass |
Type- specific type |
Description— 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—Character type
Character types can be divided into the following two sub-categories:
Text: True, unstructured character string data types. --Unstructured string type;
Integer: Structured string types for storing data from a fixed selection of allowed string values, represented internally as integers.---Structured string types for storing data from a fixed selection of allowed string values, represented internally as integers. It is presented as an integer inside MySQL (similar to the macro definition in the C language).
The specific types of character types are as follows:
Class —Subclass |
Type- specific type |
Description— 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) |
Enumeration consisting of a fixed set of legal values |
Integer |
SET (set) |
Set consisting of a fixed set of legal values |
In character—character type, the concept of character set and character proofreading is also involved. For details, please check my blog:
MySQL series: 3 character sets, collation rules, Chinese storage
Binary
The Binary type stores binary bit strings, which can be divided into two categories:
Binary: Binary strings of both fixed and variable length; fixed-length or variable-length binary strings;
BLOB: Variable-length unstructured collection of binary data. Variable-length unstructured binary data sets, such as XML documents;
Class —Subclass |
Type- specific type |
Description— 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的性能。