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

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

WBOY
Release: 2023-09-07 16:01:02
forward
858 people have browsed it

在 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)
Copy after login
Copy after login

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)
Copy after login

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)
Copy after login

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

mysql> select *from MoneyRepresentation;
Copy after login

The following is the output.

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

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)
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template