What MySQL type is most suitable for "price" column?

Mysql

Mysql Problem Overview


I have a price column in products table.

I wonder which MySQL type is the most suitable for this column. Is it DECIMAL, FLOAT, or something else ?

The price can be for example: 139.99, 40, 14.5 (2 digits after the decimal point, like in shops).

Please advise.

Mysql Solutions


Solution 1 - Mysql

DECIMAL beacuse decimal value is stored precisely. E.g. DECIMAL(10, 2) will suit perfectly for prices not higher than 99999999,99. MySQL Docs reference

Solution 2 - Mysql

Field type "Decimal" will work best. Like:

`product_price` decimal(8, 2) NOT NULL,

It will store a price up to 999999.99

If you have high prices then you can use

`product_price` decimal(12, 2) NOT NULL,

i.e. up to 9999999999.99

Solution 3 - Mysql

You should certainly use a decimal type for money. Never floating point, contrary to other answers. And using varchar prevents you doing calculations.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMisha MoroshkoView Question on Stackoverflow
Solution 1 - MysqlbeastofmanView Answer on Stackoverflow
Solution 2 - MysqlAli NawazView Answer on Stackoverflow
Solution 3 - Mysqluser207421View Answer on Stackoverflow