06-MYSQL数据类型 日期 字符串
----整数类型
整数类型 |
字节 |
取值范围 |
取值范围 |
tinyint |
1 |
有符号 -128 无符号 0 |
有符号 127 无符号 255 |
smallint |
2 |
有符号 -32768 无符号 0 |
有符号 32767 无符号65535 |
mediumint |
3 |
有符号 -8388608 无符号 0 |
有符号 8388607 无符号1677215 |
int,integer |
4 |
有符号 -2147483648 无符号 0 |
有符号 2147483647 无符号 4294967295 |
bigint |
8 |
有符号 -263 无符号 0 |
有符号 263-1 无符号264-1 |
(1)、如何选择整数类型
整数类型和浮点数类型最大的区别在于能否表达小数。那么我们的整数是不能表达小数的,而浮点却可以,
不同的整数类型取值范围不一样,tinyint类型取值范围0~255,如果字段不超过255。那么选择tinyint就足够了,bigint取值范围最大,常用的都是int类型。
----浮点类型与定点数类型
浮点数类型 |
字节 |
负数的取值范围 |
非负数得取值范围 |
float 单精度 |
4 |
-3.402823466E+38~ -1.175494351E-38
|
0和1.175494351E-38~ 3.402823466E+38 |
double 双精度 |
8 |
-1.7976931348623157E+308~ -2.2250738585072014E-308 |
0和2.2250738585072014E-308~1.7976931348623157E |
定点类型 |
字节 |
描述 |
|
decimal(m,d) |
M+2 |
最大取值范围与double相同,给定decimal的有效取值范围由M和D决定 |
(2)、如何选择浮点数类型和定点数类型
double比float类型的精度比要高,那么如果需要精确到小数点10位以上,那么我们就用float类型,普通用float类型就够了。
在Mysql中,定点数的精度比浮点要高,而且,浮点数会出现误差,如果要对数据的精度要求比较高的话,那么应该选择定点数。
----日期与时间类型
日期和时间类型 |
字节 |
最小值 |
最大值 |
Date 年月日 |
4 |
1000-01-01 |
9999-12-31 |
Datetime 年月日 时分秒 |
8 |
1000-01-01 00:00:00 |
9999-12-31 23:59:59 |
Timestamp 时区对应时间 |
4 |
19700101080001 |
20380119111407 |
Time 单独表示时间 |
3 |
-838:59:59 |
838:59:59 |
Year 单独表示年 |
1 |
1901 |
2155 |
(4)如何选择时间和日期类型
year类型只表示年份,如果单单只记录年份那么选择year就OK,还可以节约空间,
time类型只表示时间,如果只需要记录时间那么只选择time类型,
date类型只表示年月日,如果只需要记录年月日,那么只选择date类型
如果既需要记录年月日和时间,可以选择datetime类型和timestamp类型,
datetime类型表示的时间范围比timestamp的类型要大,因此,需要时间范围比较大的选择datetime类型比较合适,
timestamp类型的时间是根据时区来选择的,如果需要显示的时间与时区对应,那么选择timestamp类型。
----字符串类型
字符串类型 |
字节 |
描述 |
char(m) |
M |
M为0-255之间的整数 |
varchar(m) |
|
M为0-65535之间的整数,值的长度为+1个字节 |
tinytext |
|
允许长度0-255字节,值为长度+2个字节 |
text |
|
允许长度0-65535字节,值为长度+2个字节 |
mediumtext |
|
允许长度0~167772150字节 值为长度+3个字节 |
longtext |
|
允许长度0~4294967295字节 值为长度+4个字节 |
char(5) 比如这行实际只用了3个字节,但是还占用5个字节的空间,
varchar(5) 比如这行实际只用了3个字节,它就只是占用了3个字节的长度
Text类型石一种特殊的文字串类型,text只有保存字符数据,比如:新闻内容等。
Text包含了(tinytext mediumtext,langtext)
----二进制类型
binary(10)
varbinary(20)
存储普通二进制字符类串型.两者区别和char varchar一样,一个占用实际字节,一个占用分配固定字节。
(6)、text类型和blob类型
text类型与blob类型很类似,text只能存储字符数据,纯文本之类的。选择text类型
blob 类型可以存储二进制数据,可以存储图片pdf等的二进制数据,选择blob类型
----枚举类型
enum 取值范围0~65535
set 取值范围0~64
(5)、enum 类型和set类型
Enum类型可以有65535个成员,而set类型最多只能包含64个成员,两者取值范围只能在成员列表中选取,enum类型只能从成员当中选择一个,而set 类型可以选择多个,
enum用法:那么对于多个值当中选取一个的话,可以选择enum类型,比如,性别(男女)二选一
set 类型用法:比如个人爱好,可以选择多个,那么这个使用我们用set类型

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

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.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool