您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL浮点型表示_MySQL

2024/3/22 9:24:44发布24次查看
mysql浮点型和定点型可以用类型名称后加(m,d)来表示,m表示该值的总共长度,d表示小数点后面的长度,m和d又称为精度和标度,如float(7,4)的可显示为-999.9999,mysql保存值时进行四舍五入,如果插入999.00009,则结果为999.0001。float和double在不指定精度时,默认会按照实际的精度来显示,而decimal在不指定精度时,默认整数为10,小数为0。
创建下表:
mysql> create table t2(id1 float(5,2) default null,id2 double(5,2) default null,id3 decimal(5,2) default null);
mysql> desc t2;
+-------+--------------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+--------------+------+-----+---------+-------+
| id1 | float(5,2) | yes | | null | |
| id2 | double(5,2) | yes | | null | |
| id3 | decimal(5,2) | yes | | null | |
+-------+--------------+------+-----+---------+-------+
往id1,id2,id3这三个字段中插入数据1.23:
mysql> insert into t2(id1,id2,id3) values(1.23,1.23,1.23);
mysql> select * from t2;
+------+------+------+
| id1 | id2 | id3 |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
+------+------+------+
数据都正确插入,再向id1插入1.234,id2插入1.234,id3仍然插入1.23:
mysql> insert into t2(id1,id2,id3) values(1.234,1.234,1.23);
mysql> select * from t2;
+------+------+------+
| id1 | id2 | id3 |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
| 1.23 | 1.23 | 1.23 |
+------+------+------+
数据全部正确插入,但是id1和id2由于标度的限制,舍去了最后一位。
同时向id1,id2,id3中插入数据1.234:
mysql> insert into t2(id1,id2,id3) values(1.234,1.234,1.234);
query ok, 1 row affected, 1 warning (0.02 sec)
mysql> select * from t2;
+------+------+------+
| id1 | id2 | id3 |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
| 1.23 | 1.23 | 1.23 |
| 1.23 | 1.23 | 1.23 |
+------+------+------+
3 rows in set (0.00 sec)
数据也插入成功,但是有一个错误提示,
mysql> show warnings;
+-------+------+------------------------------------------+
| level | code | message |
+-------+------+------------------------------------------+
| note | 1265 | data truncated for column 'id3' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)
将id1,id2,id3的精度和标度都去掉,再插入数据1.234:
mysql> alter table t2 modify id1 float;
query ok, 0 rows affected (0.14 sec)
records: 0 duplicates: 0 warnings: 0
mysql> alter table t2 modify id2 double;
query ok, 0 rows affected (0.04 sec)
records: 0 duplicates: 0 warnings: 0
mysql> alter table t2 modify id3 decimal;
query ok, 4 rows affected, 4 warnings (0.06 sec)
records: 4 duplicates: 0 warnings: 4
mysql> desc t2;
+-------+---------------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+---------------+------+-----+---------+-------+
| id1 | float | yes | | null | |
| id2 | double | yes | | null | |
| id3 | decimal(10,0) | yes | | null | |
+-------+---------------+------+-----+---------+-------+
mysql> insert into t2(id1,id2,id3) values(1.234,1.234,1.234);
query ok, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings;
+-------+------+------------------------------------------+
| level | code | message |
+-------+------+------------------------------------------+
| note | 1265 | data truncated for column 'id3' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from t2;
+-------+-------+------+
| id1 | id2 | id3 |
+-------+-------+------+
| 1.234 | 1.234 | 1 |
+-------+-------+------+
1 row in set (0.00 sec)
id1和id2的数据正确插入,而id3被截断。
浮点数如果不写精度和标度,则会按照实际显示,如果有精度和标度,则会将数据四舍五入后插入,系统不报错,定点数如果不设置精度和标度,刚按照默认的(10,0)进行操作,如果数据超过了精度和标度值,则会报错。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product