MySQL資料類型decimal(m,d)的說明

來源:互聯網
上載者:User

MySQL資料類型decimal(m,d)的說明

關於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

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.