bitsCN.com
MYSQL入门学习之四:MYSQL的数据类型
相关链接:
MYSQL入门学习之一:基本操作
http:///database/201212/173868.html
MYSQL入门学习之二:使用正则表达式搜索
http:///database/201212/173869.html
MYSQL入门学习之三:全文本搜索
http:///database/201212/173873.html
一、整型
整数类型是数据库中最基本的数据类型。标准SQL中支持INTEGER和SMALLINT这两种整数类型。MySQL数据库除了支持这两种类型外,还扩展支持了TINYINT、MEDIUMINT和BIGINT。
各种整数类型的取值范围、存储的字节数如下:
整型 字节数 无符号数的取值范围 有符号数的取值范围
TINYINT 1 0~255 -128~127
SMALLINT 2 0~65535 -32768~12767
MEDIUMINT 3 0~16777215 -8388608~8388607
INT 4 0~4294967295 -2147483648~2147483647
INTEGER 4 0~4294967295 -2147483648~2147483647
BIGINT 8 0~18446744073709551615 -9223372036954775808~9223372036854775807
二、浮点数类型和定点数类型
MySQL使用浮点数类型和定点数类型来表示小数。浮点数类型包括单精度浮点数(FLOAT类型)和双精度浮点数(DOUBLE类型)。定点数类型就是DECIMAL型。
FLOAT型、DOUBLE型、DECIMAL型的取值范围、存储的字节数如下:
小数类型 字节数 负数取值范围 无符号取值范围
FLOAT 4 -3.402823466E+38~ 0和1.175494351E-38~
-1.175494351E-38 3.402823466E+38
DOUBLE 8 1.7976931348623157E+308~ 0和2.2250738585072014E~
-2.2250738585072014E-308 1.7976931348623157E+308
DECIMAL(M,D) M+2 DOUBLE型 同DOUBLE型
或DEC(M,D)
M:最大长度(包括小数部分,但不包括小数点)
D:小数点后保留长度
对于浮点数和定点数,当插入值的精度高于实际定义的精度,系统会自动进行四舍五入处理。其目的是为了使该值的精度达到要求。浮点数进行四舍五入不会报警,定点数出现警告。
在未指定精度的情况下,浮点数和定点数有其默认的精度。FLOAT类型和DOUBLE类型默认会保存实际精度。这个精度与操作系统和硬件的精度有关。DECIMAL类型默认整数位为10, 小数位为0,即默认为整数。
在MySQL中,定点数精度比浮点数要高。而且,浮点数会出现误差。如果要对数据的精度要求比较高,应该选择定点数。
三、日期和时间类型
1、DATETIME类型
DATETIME类型表示同时包含日期和时间信息的值。MySQL以'YYYY-MM-DD HH:MM:SS'格式检索和显示DATETIME值。支持的范围为'1000-01-01 00:00:00'~'9999-12-31 23:59:59'。
2、DATE类型
DATE类型表示只有日期值而没有时间值的值。MySQL用'YYYY-MM-DD'格式检索和显示DATE值。支持的范围为'1000-01-01'~'9999-12-31'。
3、TIME类型
TIME值可以使用多种表示格式。
'D HH:MM:SS.fraction'格式的字符串。还可以使用下面任何一种“非严格”语法'HH:MM:SS.fraction'、'HH:MM:SS'、'HH:MM'、'D HH:MM:SS'、'D HH:MM'、'D HH'或'SS'。这里D表示日,可以取0~34的值。请注意MySQL不保存分数。
'HHMMSS'格式的没有间隔符的字符串,被假定为是有意义的时间。例如,'101112'被理解为'10:11:12',但'109712'是不合法的(它有一个没有意义的分钟部分),其将变为'00:00:00'。
HHMMSS格式的数值,被假定为是有意义的时间。例如,101112被理解为'10:11:12'。下面的格式也可以理解:SS、MMSS、HHMMSS、HHMMSS.fraction。请注意MySQL不保存分数。
4、YEAR类型
YEAR类型是一个单字节类型,用于表示年。MySQL以YYYY格式检索和显示YEAR值。范围为1901~2155。
可以指定各种格式的YEAR值。
四位字符串,范围为'1901'~'2155'。
四位数字,范围为1901~2155。
两位字符串,范围为'00'~'99'。'00'~'69'和'70'~'99'范围的值分别被转换为2000~2069和1970~1999范围的YEAR值。
两位整数,范围为1~99。1~69和70~99范围的值分别被转换为2001~2069和1970~1999范围的YEAR值。请注意两位整数范围与两位字符串范围稍有不同,因为不能直接将零指定为数字并将它解释为2000。必须将它指定为一个字符串'0'或'00'或它被解释为0000。
5、TIMESTAMP类型
TIMESTAMP类型使用4个字节来表示日期和时间。TIMESTAMP类型的范围从1970-01-001 08:00:01到2038-01-19 11:14:07。MySQL中也是以'YYYY-MM-DD HH:MM:SS'的形式显示TIMESTAMP类型的值。从其形式可以看出,TIMESTAMP类型与DATETIME类型显示的格式一样的。给TIMESTAMP类型的字段赋值的表示方法基本与DATETIME类型相同。值的注意的是,TIMESTAMP类型范围比较小,没有DATETIME类型的范围大。因此,输入值时要保证在TIMESTAMP类型时有效范围内。
四、字符串类型
字符串类型是在数据库中存储字符串的数据类型。
1、CHAR类型和VARCHAR类型
CHAR类型和VARCHAR类型都是在创建表时指定最大长度,其基本形式如下:
字符串类型(M)
例如,CHAR(4)就是指数据类型为CHAR类型,其最大长度为4。
CHAR类型的长度是固定的,在创建表时就指定了。其长度可以是0~255的任意值。
例如,CHAR(100)就是指定CHAR类型的长度为100。
VARCHAR类型的长度是可变的,在创建时指定了最大长度。定义时,其最大值可以取0~65535之间的任意值。指定VARCHAR类型的最大值以后,其长度可以在0到最大长度之间。例如,VARCHAR(100)的最大长度是100。但是,不是每条记录都要占100个位置。而是在这个最大值范围内,使用多少分配多少。VARCHAR类型实际占用的空间为字符串的实际长度加1。这样,可以有效的节约系统的空间。
2、TEXT类型
TEXT类型是一种特殊的字符串类型。TEXT只能保存字符数据,如文章等。TEXT类型包含TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT。
类型 允许的长度 存储空间
TINYTEXT 0~255字节 值的长度+2个字节
TEXT 0~65535字节 值的长度+2个字节
MEDIUMTEXT 0~167772150字节 值的长度+3个字节
LONGTEXT 0~4294967295字节 值的长度+4个字节
从表可以看出,各种TEXT类型的区别在于允许的长度和存储空间不同。因此在这几种TEXT中,根据需求选取既能满足需要以最节约空间的类型即可。
3、ENUM类型(枚举类型)
ENUM类型又称为枚举类型。在创建表时,ENUM类型的取值范围就以列表的形式指定了。
属性名 ENUM('值1', '值2',...., '值n');
其中, '属性名'参数指定字段名称;'值n'参数表示列表中的第n个值,这些值末尾的空格将会被系统直接删除。
ENUm类型的值只能列表中的一个元素。其取值列表中最多能有65535个值。列表中的每个值都有一个顺序排序的编号,MySQL中存入的是这个编号,而不是列表中的值。
如果ENUm类型加上了NOT NULL属性,其默认值为取值列表的第1个元素。如果不加NOT NULL属性,ENUm类型将允许插入NULL,而且NULL为默认值。
CREATE TABLE IF NOT EXISTS `test`.`enum_tbl`(
`a` ENUM('male','female'),
`b` ENUM('true','false') NOT NULL
);
INSERT INTO `test`.`enum_tbl`
VALUES('male', 'true'),(NULL, 'false'), (NULL, NULL),(20, 20);
SELECT * FROM `enum_tbl`;
4、SET类型
基本形式如下:
属性名 set('值1','值2','值3'...'值n');
其中,'属性名'参数指定字段名称;'值n'参数列表中的第n个值,这些值末尾的空格将会被系统直接删除。其基本形式与ENUM类型一样。
SET类型的值可以取列表中一个元素或者多个元素的组合。取多个元素时,不同元素之间用逗号隔开。SET类型的值最多只能是64个元素构成的组合。列表中的每一个值都有一个顺序排列的编号。MySQL中存入的是这个编号,而不是列表中的值。
插入记录时,SET字段里的元素顺序无关紧要。存入MySQL数据库后,数据库系统会自动按照定义时的顺序显示。
CREATE TABLE IF NOT EXISTS `test`.`set_tbl`(
`a` SET('a','b','c','d','e','f','g')
);
INSERT INTO `test`.`set_tbl` VALUES('f'),('a,b,c'),('d,e,a');
INSERT INTO `test`.`set_tbl` VALUES('h');
SELECT * FROM `set_tbl`;
五、二进制类型
二进制类型是在数据库中存储二进制数据的数据类型。
二进制类型 取值范围
BINARY(M) 字节数为M,允许长度为0~M的定长二进制字符串
VARBINARY(M) 允许长度为0~M的变长二进制字符串,字节数为值的长度加一
BIT(M) M位二进制数据,M最大值为64
TINYBLOB 可变长二进制数据,最多255个字节
BLOB 可变长二进制数据,最多(2[16]-1)个字节
MEDIUMBLOB 可变长二进制数据,最多(2[24]-1)个字节
LONGBLOB 可变长二进制数据,最多(2[32]-1)个字节
1、BINARY和VARBINARY类型
BINARY和VARBINARY类型都是在创建表时指定了最大长度,其基本形式如下 :
字符串类型(M)
这与CHAR类型和VARCHAR类型相似。
例如,BINARY(10)就是指数据类型为BINARY类型,其最大长度为10。
BINARY类型的长度是固定的,在创建表是就指定了。不足最大长度的空间由"/0"补全。例如,BINARY(50)就是指定BINARY类型的长度为50。
VARBINARY类型的长度是可变的,在创建表时指定了最大长度。指定好了VARBINARY类型的最大值以后,基长度可以在0到最大长度之间。例如,VARBINARY(50)的最大字节长度是50。但是,不是每条记录的字节长度都是50。在这个最大范围内,使用多少分配多少。VARBINARY类型实际占用的空间为实际长度加一。这样,可以有效的节约系统的空间。
2、BIT类型
BIT类型也是创建表时指定了最大长度,其基本形式如下:
BIT(M)
其中,'M'指定了该二进制的数的最大字节长度为M,M的最大值为64。例如,BIT(4)就是数据类型BIT类型,长度为4.若字段的类型BIT(4),存储的数据是从0到15。因为,变成二进制以后,15的值为1111,其长度为4。如果插入的值为16,其二进制数为10000,长度为5,超过了最大长度。因此大于等于16的数是不能插入到BIT(4)类型的字段中的。在查询BIT类型的数据时,要用BIN(字段名+0)来将值转换为二进制显示。
3、BLOB类型
BLOB类型是一种特殊的二进制类型。BLOB可以用来保存数据量很大的二进制数据,如图片等。BLOB类型包括TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB。这几种BLOB类型最大的区别就是能够保存的最大长度不同。LONGBLOB的长度最大,TINYBLOB的长度最小。
BLOB类型与TEXT类型很类似。不同点在于BLOB类型用于存储二进制数据,BLOB类型数据是根据其二进制编码进行比较和排序。而TEXT类型是文本模式进行比较和排序的。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.