数据库数据类型decimal理解 您所在的位置:网站首页 declmal是数据类型 数据库数据类型decimal理解

数据库数据类型decimal理解

2023-11-27 17:14| 来源: 网络整理| 查看: 265

MySQL DECIMAL数据类型用于在数据库中存储精确的数值。

例如语法:column_name DECIMAL(P,D)

 

其中P表示的是有效数字的位数,D表示是小数的位数,D小于或等于P。

理解可看下面代码:

create table decimal_test(id int auto_increment PRIMARY key,score decimal(5,2) -- 取值范围是 -999.99 到 999.99);

-- 整数的位数必须小于等于m-d,不然报错。小数的位数可以大于d位。多出d位时会做四舍五入,截取到d位。-- 以上均不包括小数点、符号的位数。数字的总长度是m位,保存后的小数位最多是d位。如果保存后是整数,小数位不会补0。

select * from decimal_test;-- 正数:insert into decimal_test(score) VALUES(1.23); -- 1.23insert into decimal_test(score) VALUES(123.45); -- 123.45insert into decimal_test(score) VALUES(123.455); -- 123.46insert into decimal_test(score) VALUES(123.451); -- 123.45insert into decimal_test(score) VALUES(123.451123); -- 123.45insert into decimal_test(score) VALUES(12345.451123); -- Out of range value for column 'score' insert into decimal_test(score) VALUES(9999.451123); -- Out of range value for column 'score' insert into decimal_test(score) VALUES(999.451123234324); -- 999.45insert into decimal_test(score) VALUES(999.999999999); -- Out of range value for column 'score' insert into decimal_test(score) VALUES(999.99123); -- 999.99-- 负数:insert into decimal_test(score) VALUES(-1.23); -- -1.23insert into decimal_test(score) VALUES(-12.34); -- -12.34insert into decimal_test(score) VALUES(-123.45); -- -123.45insert into decimal_test(score) VALUES(-999.45); -- -999.45insert into decimal_test(score) VALUES(-12343); -- Out of range value for column 'score' insert into decimal_test(score) VALUES(12343); -- Out of range value for column 'score' insert into decimal_test(score) VALUES(1234); -- Out of range value for column 'score' insert into decimal_test(score) VALUES(123); -- 123insert into decimal_test(score) VALUES(-123); -- -123insert into decimal_test(score) VALUES(-999.99); -- -999.99insert into decimal_test(score) VALUES(-9990.99); -- Out of range value for column 'score'insert into decimal_test(score) VALUES(-1234.99); -- Out of range value for column 'score'insert into decimal_test(score) VALUES(-1234); -- Out of range value for column 'score'

select VERSION() ; -- 5.7.14



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有