时间:2021-07-01 10:21:17 帮助过:27人阅读
创建下表:
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)进行操作,如果数据超过了精度和标度值,则会报错。