将int转换为十进制数据类型的过程?

时间:2010-06-07 17:16:48

标签: php mysql decimal int types

我有一个int(11)列,用于存储资金。我在SO上阅读了一些答案,似乎我只需将其更新为decimal (19,4)数据类型。

在我实际进行转换之前,是否有任何我应该知道的问题?我的应用程序是PHP / Zend,我没有使用ORM,所以我怀疑我需要更新任何类来持续识别数据类型。

1 个答案:

答案 0 :(得分:3)

    mysql> CREATE TABLE t1 (
    -> id INT NOT NULL AUTO_INCREMENT,
    -> money INT(11) NOT NULL,
    -> PRIMARY KEY(id))
    -> ENGINE = MyISAM;
Query OK, 0 rows affected (0.01 sec)

mysql> DESCRIBE t1;
+-------+---------+------+-----+---------+----------------
| Field | Type    | Null | Key | Default | Extra
+-------+---------+------+-----+---------+----------------
| id    | int(11) | NO   | PRI | NULL    | auto_increment
| money | int(11) | NO   |     | NULL    |
+-------+---------+------+-----+---------+----------------
2 rows in set (0.01 sec)

mysql> SELECT * FROM t1;
Empty set (0.00 sec)

mysql> INSERT INTO t1 (money) VALUES (`10.01`);
ERROR 1054 (42S22): Unknown column '10.01' in 'field list'
mysql> INSERT INTO t1 (money) VALUES ('10.01');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM t1;
+----+-------+
| id | money |
+----+-------+
|  1 |    10 |
+----+-------+
1 row in set (0.00 sec)

mysql> ALTER TABLE t1 MODIFY COLUMN money DECIMAL(19,4);
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM t1;
+----+---------+
| id | money   |
+----+---------+
|  1 | 10.0000 |
+----+---------+
1 row in set (0.00 sec)

mysql> INSERT INTO t1 (money) VALUES ('10.12');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM t1;
+----+---------+
| id | money   |
+----+---------+
|  1 | 10.0000 |
|  2 | 10.1200 |
+----+---------+
2 rows in set (0.00 sec)

将int改为decimal时,dd没有任何问题。但是,您应该检查您的PHP代码,以确保您的代码正确地使用十进制值。