阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL数据类型decimal(m,d)的说明

144次阅读
没有评论

共计 1824 个字符,预计需要花费 5 分钟才能阅读完成。

关于 MySQL 数据类型 decimal(m,d) 的说明,自己亲测一遍:

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。
— 以下测试版本是 5.7.14

select  *  from  decimal_test;
— 正数:
insert into decimal_test(score) VALUES(1.23); — 1.23
insert into decimal_test(score) VALUES(123.45); — 123.45
insert into decimal_test(score) VALUES(123.455); — 123.46
insert into decimal_test(score) VALUES(123.451); — 123.45
insert into decimal_test(score) VALUES(123.451123); — 123.45
insert 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.45
insert 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.23
insert into decimal_test(score) VALUES(-12.34); — -12.34
insert into decimal_test(score) VALUES(-123.45); — -123.45
insert into decimal_test(score) VALUES(-999.45); — -999.45
insert 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); — 123
insert into decimal_test(score) VALUES(-123); — -123
insert into decimal_test(score) VALUES(-999.99); — -999.99
insert 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

MySQL 数据类型 decimal(m,d) 的说明

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2018-02/150736.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计1824字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中