码迷,mamicode.com
首页 > 数据库 > 详细

MySQL 浮点型表示

时间:2014-11-07 11:26:55      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:des   ar   sp   for   数据   bs   ef   sql   mysql   

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)进行操作,如果数据超过了精度和标度值,则会报错。

MySQL 浮点型表示

标签:des   ar   sp   for   数据   bs   ef   sql   mysql   

原文地址:http://blog.csdn.net/zhuangjingyang/article/details/40888177

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!