- 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
Storing money amounts in MySQL?
To store money amounts in MySQL, the best choice is to use DECIMAL data type or NUMERIC type. Float data type is not a good choice for money amounts. It gives some rounding errors. Therefore, avoid float for money amounts.
Let us first create a table with data type DECIMAL. The following is the query to create a table −
mysql> create table MoneyStorageDemo -> ( -> Amount DECIMAL(4,2) -> ); Query OK, 0 rows affected (0.44 sec)
Inserting some values into the table with the help of insert command. The query is as follows −
mysql> insert into MoneyStorageDemo values(50.2); Query OK, 1 row affected (0.10 sec) mysql> insert into MoneyStorageDemo values(5.50); Query OK, 1 row affected (0.32 sec) mysql> insert into MoneyStorageDemo values(10.4); Query OK, 1 row affected (0.26 sec)
Now you can display all the values from the table with the help of select statement. The query is as follows −
mysql> select *from MoneyStorageDemo;
Here is the output −
+--------+ | Amount | +--------+ | 50.20 | | 5.50 | | 10.40 | +--------+ 3 rows in set (0.00 sec)
- Related Articles
- Best data type for storing large strings in MySQL?
- Which MySQL Datatype should be used for storing BloodType?
- Best data type for storing currency values in a MySQL database?
- The monthly pocket money of six friends is given below:Rs. 45, Rs. 30, Rs. 40, Rs. 50, Rs. 25, Rs. 45Arrange the amounts of pocket money in ascending order.
- Storing value from a MySQL SELECT statement to a variable?
- Storing Credentials in Local Storage
- Storing objects in PHP session
- Storing static attribute values in ABAP
- What is the best data type to store money values in MySQL?
- Using “SPELL AMOUNT” function to convert amounts in ABAP
- Difference between Broad Money and Narrow Money
- Storing a Command in a Variable in a Shell Script
- In-the-Money, At-the-Money, and Out-of-theMoney Options
- A certain sum of money amounts to rupees 7260 in 2 years and to rupees 7986 in 3 years, interest is compounded annually. Find the rate of interest in percent per annum.