mysql 8.3_8.3.2 - mysql 数据类型

存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

http://www.runoob.com/mysql/mysql-data-types.html

http://dev.mysql.com/doc/refman/5.7/en/data-type-overview.html

参考

mysql常用数据类型概览

#1. 数字:

整型:tinyinitintbigint

小数:float:在位数比较短的情况下不精准double:在位数比较长的情况下不精准0.000001230123123123存成:0.000001230000

decimal:(如果用小数,则用推荐使用decimal)

精准

内部原理是以字符串形式去存

#2. 字符串:char(10):简单粗暴,浪费空间,存取速度快

root存成root000000

varchar:精准,节省空间,存取速度慢

sql优化:创建表时,定长的类型往前放,变长的往后放

比如性别 比如地址或描述信息>255个字符,超了就把文件路径存放到数据库中。

比如图片,视频等找一个文件服务器,数据库中只存路径或url。

#3. 时间类型:

最常用:datetime

#4. 枚举类型与集合类型

1、整数类型

整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT

作用:存储年龄,等级,id,各种号码等

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

========================================tinyint[(m)] [unsigned] [zerofill]

小整数,数据类型用于保存一些范围的整数数值范围:

有符号:-128 ~ 127无符号:0 ~ 255PS: MySQL中无布尔值,使用tinyint(1)构造。========================================

int[(m)][unsigned][zerofill]

整数,数据类型用于保存一些范围的整数数值范围:

有符号:-2147483648 ~ 2147483647无符号:0 ~ 4294967295

========================================bigint[(m)][unsigned][zerofill]

大整数,数据类型用于保存一些范围的整数数值范围:

有符号:-9223372036854775808 ~ 9223372036854775807无符号:0 ~ 18446744073709551615

View Code

mysql>create database db1;

Query OK,1 row affected (0.00sec)

mysql>user db1;

db1;^C

mysql>use db1;

Database changed

mysql>create table t1(x tinyint);

Query OK,0 rows affected (0.38sec)

mysql> insert into t1 values(-1);

Query OK,1 row affected (0.01sec)

mysql> select * fromt1;+------+

| x |

+------+

| -1 |

+------+

1 row in set (0.00sec)

mysql> # -128 ~ 127mysql> insert into t1 values(-129),(128);

ERROR1264 (22003): Out of range value for column 'x' at row 1mysql># 无符号

mysql>create table t2 (x tinyint unsigned);

Query OK,0 rows affected (0.03sec)

mysql> insert into t2 values(-129),(128);

ERROR1264 (22003): Out of range value for column 'x' at row 1mysql> insert into t2 values(-1),(256);

ERROR1264 (22003): Out of range value for column 'x' at row 1mysql> insert into t2 values(1),(255);

Query OK,2 rows affected (0.01sec)

Records:2 Duplicates: 0 Warnings: 0mysql> select * fromt2;+------+

| x |

+------+

| 1 |

| 255 |

+------+

2 rows in set (0.01sec)

mysql> create table t3(id int(1) unsigned)->;

Query OK,0 rows affected (0.06sec)

mysql>desc t3;+-------+-----------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-----------------+------+-----+---------+-------+

| id | int(1) unsigned | YES | | NULL | |

+-------+-----------------+------+-----+---------+-------+

1 row in set (0.01sec)

mysql> insert into t3 values(255);

Query OK,1 row affected (0.00sec)

mysql> select * fromt3;+------+

| id |

+------+

| 255 |

+------+

1 row in set (0.00sec)

mysql> insert into t3 values(2551111111111111111111111111111111111);

ERROR1264 (22003): Out of range value for column 'id' at row 1mysql> create table t5 (id int(5) unsigned zerofill);

Query OK,0 rows affected (0.37sec)

mysql> insert into t5 values(1),(123456);

Query OK,2 rows affected (0.36sec)

Records:2 Duplicates: 0 Warnings: 0mysql> select * fromt5;+--------+

| id |

+--------+

| 00001 |

| 123456 |

+--------+

2 rows in set (0.00sec)

mysql># 显示宽度,为5.不够用0补全

mysql> create table t6 (id intunsigned);

Query OK,0 rows affected (0.39sec)

mysql>desc t6;+-------+------------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+------------------+------+-----+---------+-------+

| id | int(10) unsigned | YES | | NULL | |

+-------+------------------+------+-----+---------+-------+

1 row in set (0.00 sec)

注意:为该类型指定宽度时,仅仅只是指定查询结果的显示宽度,与存储范围无关,存储范围如下

其实我们完全没必要为整数类型指定显示宽度,使用默认的就可以了

默认的显示宽度,都是在最大值的基础上加1

08b426543540b0c7e35ca16c958177bc.png

int的存储宽度是4个Bytes,即32个bit,即2**32

无符号最大值为:4294967296-1

有符号最大值:2147483648-1

有符号和无符号的最大数字需要的显示宽度均为10,而针对有符号的最小值则需要11位才能显示完全,所以int类型默认的显示宽度为11是非常合理的

最后:整形类型,其实没有必要指定显示宽度,使用默认的就ok

2、浮点型

定点数类型 DEC等同于DECIMAL

浮点类型:FLOAT DOUBLE

作用:存储薪资、身高、体重、体质参数等

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

======================================#FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]

定义:

单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30

有符号:-3.402823466E+38 to -1.175494351E-38,1.175494351E-38 to 3.402823466E+38无符号:1.175494351E-38 to 3.402823466E+38精确度:**** 随着小数的增多,精度变得不准确 ****

======================================#DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]

定义:

双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30

有符号:-1.7976931348623157E+308 to -2.2250738585072014E-308

2.2250738585072014E-308 to 1.7976931348623157E+308无符号:2.2250738585072014E-308 to 1.7976931348623157E+308精确度:****随着小数的增多,精度比float要高,但也会变得不准确 ****

======================================

decimal[(m[,d])] [unsigned] [zerofill]

定义:

准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。

精确度:**** 随着小数的增多,精度始终准确 ****对于精确数值计算时需要用此类型

decaimal能够存储精确值的原因在于其内部按照字符串存储。

View Code

mysql> create table t8(x float(255,30));

Query OK,0 rows affected (0.40sec)

mysql> create table t9(x double(255,30));

Query OK,0 rows affected (0.04sec)

mysql> create table t10(x decimal(65,30));

Query OK,0 rows affected (0.14sec)

mysql> insert into t8 values(1.111111111111111111111111111111);

Query OK,1 row affected (0.36sec)

mysql> insert into t9 values(1.111111111111111111111111111111);Query OK,1 row affected (0.00sec)

mysql> insert into t10 values(1.111111111111111111111111111111);

Query OK,1 row affected (0.01sec)

mysql> select * from t8;  #随着小数的增多,精度开始不准确

+----------------------------------+

| x |

+----------------------------------+

| 1.111111164093017600000000000000 |

+----------------------------------+

1 row in set (0.00 sec)

mysql> select * from t9;   #精度比float要准确点,但随着小数的增多,同样变得不准确

+----------------------------------+

| x |

+----------------------------------+

| 1.111111111111111200000000000000 |

+----------------------------------+

1 row in set (0.00 sec)

mysql> select * from t10;  #精度始终准确,d为30,于是只留了30位小数

+----------------------------------+

| x |

+----------------------------------+

| 1.111111111111111111111111111111 |

+----------------------------------+

1 row in set (0.00 sec)

精度高低分 DEC>double>float

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值