搜索
查看: 1463|: 0

【MySql】MySQL浮点型表示

[复制链接]

128

主题

5

回帖

433

积分

中级会员

积分
433
发表于 2014-11-10 17:24:47 | 显示全部楼层 |阅读模式

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


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

大数据中国微信

QQ   

版权所有: Discuz! © 2001-2013 大数据.

GMT+8, 2024-11-15 20:35 , Processed in 0.082952 second(s), 25 queries .

快速回复 返回顶部 返回列表