int 转成decimal 数据类型的过程?

发布于 2024-09-04 19:07:56 字数 195 浏览 6 评论 0原文

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

在实际进行转换之前,有什么我应该了解的问题吗?我的应用程序是在 PHP/Zend 中,并且我没有使用 ORM,所以我怀疑我是否需要更新任何类型的类来一致地识别数据类型。

I have an int(11) column which is used to store money. I read some of the answers on SO and it seems I just need to update it to be a decimal (19,4) data type.

Are there any gotchas I should know about before I actually do the converting? My application is in PHP/Zend and I'm not using an ORM so I doubt I would need to update any sort of class to consistently identify the data type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

调妓 2024-09-11 19:07:56
    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 更改为十进制时, dd 不会有任何问题。但是您应该检查您的 PHP 代码,以确保您的代码能够正确使用十进制值。

    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)

You don't have any problem with dd when you alter int to decimal. But you should review your PHP code to ensure that your code works with decimal value correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文