Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store User's money in database?

Tags:

mysql

What is the best way to store people's current balance in MySQL? I'm developing something like Neobux, so each user have a balance data in MySQL..

like image 563
Rho Avatar asked Dec 10 '10 17:12

Rho


People also ask

How do you store money in a database?

It can be tempting to store a value such as $10 as 10.00 in your database. However, storing monetary values with a decimal point can cause a lot of issues. Instead, you should always store monetary values in their minor unit form. So to record $10, you would save it as an integer in your database as 1000 .

Which data type is best for storing currency data?

The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data.

Is there a currency data type in MySQL?

We can store the money values in MySQL in decimal(value1,value2). Here, value1 is the total range including value2. The value2 specifies the number of digits after the decimal point.

What should I store in a database?

The purpose of every database is to store information, texts, images, even media files. All dynamic modern websites rely on one or more databases for storing articles and other published content, information about the users, contact information, connections to other websites, ads, etc.


2 Answers

Use decimal(10,2). float is susceptible to rounding errors, see here.

For further information, you can take a look at these pages:

  • What's the best datatype to properly store money/currency in MySQL?
  • Storing money amounts in MySQL
like image 146
Donut Avatar answered Sep 25 '22 19:09

Donut


I say store all of the transactions rather than just the current balance. That way you have history, and you can run different aggregates on it rather than just "final balance."

like image 20
Parris Varney Avatar answered Sep 23 '22 19:09

Parris Varney