This article lists the data type list (list) of mysql, which mainly includes five categories: integer type, floating point type, string type, date type and other data types. The following will provide a detailed explanation of these five data types. There are also lengths and ranges of mysql data types with usage recommendations and basic principles for selecting data types.
Mainly include the following five categories:
Integer types: BIT, BOOL, TINY INT, SMALL INT, MEDIUM INT, INT, BIG INT
Floating point type: FLOAT, DOUBLE, DECIMAL
String type: CHAR, VARCHAR, TINY TEXT, TEXT, MEDIUM TEXT, LONGTEXT, TINY BLOB, BLOB, MEDIUM BLOB, LONG BLOB
Date type: Date, DateTime, TimeStamp, Time, Year
Other data types: BINARY, VARBINARY, ENUM, SET, Geometry, Point, MultiPoint, LineString, MultiLineString, Polygon, GeometryCollection, etc.
1. Integer type
##MySQL data typeMeaning (signed) tinyint(m)1 byte range (-128~127)smallint(m)2 byte range (- 32768~32767)mediumint(m)3 bytes Range (-8388608~8388607)int (m)4 bytes range (-2147483648~2147483647)##bigint (m)If unsigned is added to the value range, the maximum value will be doubled. For example, the value range of tinyint unsigned is (0~256).8 bytes range (-9.22 *10 to the 18th power) |
The m in int(m) represents the display width in the SELECT query result set. It does not affect the actual value range or the display width. I don’t know what the use of this m is.
2. Floating point type (float and double)
MySQL data typefloat(m,d)##double( m,d)Double precision floating point type 16-bit precision (8 bytes) m total number, d decimal placesSet a field definition It is float(6,3). If you insert a number 123.45678, the actual number stored in the database is 123.457, but the total number is subject to the actual number, that is, 6 digits. The integer part has a maximum length of 3 digits. If the number 12.123456 is inserted, 12.1234 is stored. If 12.12 is inserted, 12.1200 is stored.Meaning | |
Single precision floating point type 8-bit precision (4 bytes) m total number, d decimal places | |
varchar and text:
1.varchar can specify n, text cannot. The internal storage of varchar is the actual number of characters stored, 1 byte (n<=255) or 2 bytes ( n>255), text is the actual number of characters and is 2 bytes
If you define a field as timestamp, the time data in this field will be automatically refreshed when other fields are modified, so this data type field can store the last modified time of this record.
Data type attributes
MySQL keyword | Meaning |
NULL | Data columns can contain NULL values |
NOT NULL | Data columns are not allowed to contain NULL values |
1. When specifying the data type, the principle of small size is generally adopted. For example, if you can use TINY INT, it is best not to use INT, and if you can use FLOAT type, it is best not to use DOUBLE type, so It will greatly improve the operating efficiency of MYSQL, especially under large data volume testing conditions.
2. There is no need to design the data table too complicated. The distinction between functional modules may be more convenient for later maintenance. Be careful when presenting a hodgepodge of data tables.
3. The classification of data tables and fields Naming is also a skill
4. Before designing the data table structure, please imagine it is your room. Maybe the result will be more reasonable and efficient
5. The final design result of the database must be It is a trade-off between efficiency and scalability. It is inappropriate to favor either side.
Prerequisite: Use a suitable storage engine.
Selection principle: Determine how to choose the appropriate data type based on the selected storage engine.
The following selection methods are classified by storage engine:
MyISAM data storage engine and data columns: MyISAM data table, it is best to use fixed-length (CHAR) data columns instead of variable Data column of length (VARCHAR).
MEMORY storage engine and data columns: MEMORY data tables currently use fixed-length data row storage, so it does not matter whether you use CHAR or VARCHAR columns. Both are handled as CHAR types.
InnoDB storage engine and data columns: It is recommended to use VARCHAR type.
For InnoDB data tables, the internal row storage format does not distinguish between fixed-length and variable-length columns (all data rows use head pointers pointing to data column values), so in essence , using fixed-length CHAR columns is not necessarily simpler than using variable-length VARCHAR columns. Therefore, the main performance factor is the total storage used by the data rows. Since CHAR takes up more space on average than VARCHAR, it is better to use VARCHAR to minimize the total storage and disk I/O of data rows that need to be processed.
Let’s talk about fixed-length data columns and variable-length data columns.
The CHAR and VARCHAR types are similar, but they are saved and retrieved differently. They also differ in terms of their maximum length and whether trailing spaces are preserved. No case conversion is performed during storage or retrieval.
The following table shows the results of saving various string values into CHAR(4) and VARCHAR(4) columns, illustrating the difference between CHAR and VARCHAR:
1 | is displayed in the format of YYYY. For example: 2009 | |
M | ||
M | Variable length string, requires M<=255 | |
M | Binary storage similar to Char, characterized by inserting fixed length and filling 0 | |
M | Variable length binary storage similar to VarChar, characterized by fixed length without padding 0 | |
Max :255 | Case insensitive | |
Max:64K | Case insensitive | |
Max:16M | Case insensitive | |
Max:4G | Case insensitive | |
##LineString | ||
##MultiPoint | ||
MultiLineString | ||
Value | CHAR(4) | Storage requirements | VARCHAR(4) | Storage requirements |
'' | ' ' | 4 bytes | '' | 1 byte |
'ab' | 'ab ' | 4 bytes | 'ab ' | 3 bytes |
'abcd' | 'abcd' | 4 bytes | 'abcd' | 5 bytes |
'abcdefgh' | 'abcd' | 4 bytes | 'abcd' | 5 characters Section |
Please note that the value in the last row in the above table only applies when strict mode is not used; if MySQL is running in strict mode, the column length will not be exceeded. The value is not saved , and an error occurs.
The values retrieved from CHAR(4) and VARCHAR(4) columns are not always the same because trailing spaces are removed from the CHAR column when retrieving. The following example illustrates the difference:
mysql> CREATE TABLE vc (v VARCHAR(4), c CHAR(4));
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO vc VALUES ('ab ', 'ab ');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT CONCAT(v, ' '), CONCAT(c, ' ') FROM vc;
--------------------------------
| CONCAT(v, ' ') | CONCAT(c, ' ') |
--------------------------------
| ab ---------------- ----------------
1 row in set (0.00 sec)
Related Article:
Detailed explanation of MYSQL data types
Detailed explanation of the correct use of numeric types of MySQL data types
Related videos:The above is the detailed content of What are the data types of mysql? Detailed explanation of mysql data types. For more information, please follow other related articles on the PHP Chinese website!