【Mysql 學習】數值型別轉換

楊奇龍發表於2011-01-01

數值型別轉換問題,當從一個欄位從double 變為 float 精度不變,而從float 轉變為double 時會發生精度的改變,實驗如下:
mysql> create table t2 ( id1 float (5,2) default null, id2 double (5,2) default null, id3 decimal (5,2) default null );
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t2 values (1.23,1.23,1.23);
Query OK, 1 row affected (0.01 sec)

mysql> alter table t2 modify id2 double;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t2;
+------+------+------+
| id1  | id2  | id3  |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
+------+------+------+
1 row in set (0.01 sec)

mysql> alter table t2 modify id2 float;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t2;
+------+------+------+
| id1  | id2  | id3  |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
+------+------+------+
1 row in set (0.00 sec)

mysql> desc t2;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id1   | float(5,2)   | YES  |     | NULL    |       |
| id2   | float        | YES  |     | NULL    |       |
| id3   | decimal(5,2) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table t2 modify id2 double;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t2;
+------+------------------+------+
| id1  | id2              | id3  |
+------+------------------+------+
| 1.23 | 1.23000001907349 | 1.23 |
+------+------------------+------+
1 row in set (0.01 sec)

mysql>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-683133/,如需轉載,請註明出處,否則將追究法律責任。

相關文章