MySQL 8.0 decimal 数据类型

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/vkingnew/article/details/89498353
官方的定义:
DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]

A packed “exact” fixed-point number. M is the total number of digits (the precision) and D is the 
number of digits after the decimal point (the scale). The decimal point and (for negative numbers) 
the - sign are not counted in M. If D is 0, values have no decimal point or fractional part.
 The maximum number of digits (M) for DECIMAL is 65. The maximum number of supported decimals (D) 
is 30. If D is omitted, the default is 0. If M is omitted, the default is 10.

UNSIGNED, if specified, disallows negative values.

All basic calculations (+, -, *, /) with DECIMAL columns are done with a precision of 65 digits.

--示例:
mysql> create table t1(c1 decimal,c2 decimal(65),c3 decimal(30,30),c4 decimal(65,30));
Query OK, 0 rows affected (0.01 sec)

mysql> desc t1;
+-------+----------------+------+-----+---------+-------+
| Field | Type           | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| c1    | decimal(10,0)  | YES  |     | NULL    |       |
| c2    | decimal(65,0)  | YES  |     | NULL    |       |
| c3    | decimal(30,30) | YES  |     | NULL    |       |
| c4    | decimal(65,30) | YES  |     | NULL    |       |
+-------+----------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

decimal:数字型,不存在精度损失,常用于银行帐目计算
decimal(P,m)
P的取值范围是1--65,
M的取值是0--30.
精度M最大为30,表示有30位小数。

decimal 整数和小数位均不写 默认为decimal(10,0)
若小数位不写默认的小数位为0.

--做极限值测试插入:

mysql> insert into t1(c1,c2,c3,c4)select repeat(9,10),repeat(9,65),1/POWER(10,30),CONCAT(REPEAT(9,35),1/POWER(10,30));
Query OK, 1 row affected, 2 warnings (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 2


mysql> select * from t1\G
*************************** 1. row ***************************
c1: 9999999999
c2: 99999999999999999999999999999999999999999999999999999999999999999
c3: 0.000000000000000000000000000001
c4: 100000.000000000000000000000000000000
1 row in set (0.00 sec)
mysql> select length(c1),length(c2),length(c3),length(c4) from t1;
+------------+------------+------------+------------+
| length(c1) | length(c2) | length(c3) | length(c4) |
+------------+------------+------------+------------+
|         10 |         65 |         32 |         37 |
+------------+------------+------------+------------+
1 row in set (0.00 sec)

在实际中常用的类型为:
decimal(10,7) 用于存储经纬度 ,可以精确到米。
decimal(18,2) 存储的金额为9999千亿,存储整数位16个
若存储的小数位比较多可以扩展整数位 decimal(22,6) 整数位16个小数位6个。
在产品设计的时候可以约定。

decimal 还可以存储超过bigint的整数位,最大存储65位的整数类型。

还可以存储对应位数的负数:
mysql> insert into t1(c1) values(-9999999999);
Query OK, 1 row affected (0.00 sec)


-- 存储:

-- 性能:

--IO:

https://dev.mysql.com/doc/refman/8.0/en/numeric-type-overview.html

猜你喜欢

转载自blog.csdn.net/vkingnew/article/details/89498353