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




歡迎關注我的公衆號

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章