Home  >  Article  >  Database  >  Best data type to store monetary values ​​in MySQL database?

Best data type to store monetary values ​​in MySQL database?

WBOY
WBOYforward
2023-09-07 16:01:02791browse

在 MySQL 数据库中存储货币值的最佳数据类型?

To represent money, we need to use the Decimal (TotalDigitsinteger, DigitsAfterDecimalinteger) method.

Suppose we need to display the value 345.66. To do this, calculate how many bits are available. The value is 345.66, which has 5 digits in total and 2 digits after the decimal point, which is 66.

We can use MySQL's Decimal() method to represent the same content. This is an accurate representation.

DECIMAL(5,2)

Let us first create a table and consider the same above representation for our example -

mysql> create table MoneyRepresentation
   -> (
   -> Money Decimal(5,2)
   -> );
Query OK, 0 rows affected (0.65 sec)

Let us insert the same value i.e. 345.66

mysql> insert into MoneyRepresentation values(345.66);
Query OK, 1 row affected (0.13 sec)

Display all records with the help of SELECT statement. The query is as follows -

mysql> select *from MoneyRepresentation;

The following is the output.

+--------+
| Money  |
+--------+
| 345.66 |
+--------+
1 row in set (0.00 sec)

Looking at the output above, we got a total of 5 digits and added 2 digits after the decimal point because we set the function to

DECIMAL(5,2)

The above is the detailed content of Best data type to store monetary values ​​in MySQL database?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete