
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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.
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)
Look at the above output, we got 5 digits total and added 2 digits after decimal point because we have set the function as
Decimal(5,2)
- Related Articles
- 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?
- Add data to existing data in a MySQL Database?
- How to structure some data in a MySQL database for easier retrieval?
- What is the data type for unix_timestamp in MySQL?
- Storing static attribute values in ABAP
- Query the database for the values not in the MySQL table?
- Storing money amounts in MySQL?
- How to add columns values with suffixed currency sign in MySQL
- Which MySQL Datatype should be used for storing BloodType?
- Which MySQL data type is used for long decimal?
