- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Best data type for storing currency values in a MySQL database?
For representation of money, we need to use Decimal (TotalDigitsinteger, DigitsAfterDecimalinteger) method.
Let’s say, we need to display the value 345.66. For that, count how many digits are available. In value 345.66, there are 5 digits in total and 2 digits after decimal point, which is 66.
We can represent the same with the help of Decimal() method from MySQL. Here is the exact representation.
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)
Look at the above output, we got 5 digits total and added 2 digits after decimal point because we have set the function as
- Best data type for storing large strings in MySQL?
- What is the best data type to use for currency in C#?
- How should I store data into my Mysql database, what type should I assign to a column storing salt values?
- What is the best data type to store money values in MySQL?
- The best data type to store 0, 1, null values in MySQL?
- MySQL database field type for search query?
- Storing static attribute values in ABAP
- What is the data type for unix_timestamp in MySQL?
- Storing money amounts in MySQL?
- Which MySQL Datatype should be used for storing BloodType?
- Add data to existing data in a MySQL Database?
- How to structure some data in a MySQL database for easier retrieval?
- Which MySQL data type is used for long decimal?
- Query the database for the values not in the MySQL table?
- How to add columns values with suffixed currency sign in MySQL