Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crypto Currency MySQL Datatypes ?

Tags:

The infamous question about datatypes when storing money values in an SQL database.

However in these trying times, we now have currencies that have worth up to 18 decimal places (thank you ETH).

This now reraises the classic argument.

IDEAS

Option 1 BIGINT Use a big integer to save the real value, then store how many decimal places the currency has (simply dividing A by 10^B in translation)?

Option 2 Decimal(60,30) Store the datatype in a large decimal, which inevitibly will cost a large amount of space.

Option 3 VARCHAR(64) Store in a string. Which would have a performance impact.


I want to know peoples thoughts and what they are using if they are dealing with cryptocurrency values. As I am stumped with the best method for proceeding.

like image 905
Kevin Upton Avatar asked Oct 10 '17 04:10

Kevin Upton


People also ask

What type of data is cryptocurrency?

In computer science, a cryptocurrency, crypto-currency, or crypto is a digital currency that does not rely on any central authority to uphold or maintain it. Instead, transaction and ownership data is stored in a digital ledger using distributed ledger technology, typically a blockchain.

What are the 4 types of cryptocurrency?

Q #1) What are the four types of cryptocurrency? Answer: The four major types include utility, payment, security, and stablecoins. There also are DeFi tokens, NFTs, and asset-backed tokens. Of all cryptocurrencies, the most common are utility and payment tokens.

What are the 3 types of crypto?

Each cryptocurrency solves a particular problem and fits into one of three categories. These categories are transactional, platform, and utility . It is important to note that some cryptocurrencies can fit into more than one category.

How is data used in cryptocurrency?

The data science aspect of cryptocurrency is usually in forecasting and predicting the prices of these digital coins. Data science is used to figure out what causes the changes in the prices of these coins. Then, indicate whether the price will go up or down in the future.


1 Answers

There's a clear best option out of the three you suggested (plus one from the comments).

BIGINT — uses just 8 bytes, but the largest BIGINT only has 19 decimal digits; if you divide by 1018, the largest value you can represent is 9.22, which isn't enough range.

DOUBLE — only has 15–17 decimal digits of precision; has all the known drawbacks of floating-point arithmetic.

VARCHAR — will use 20+ bytes if you're dealing with 18 decimal places; will require constant string↔int conversions; can't be sorted; can't be compared; can't be added in DB; many downsides.

DECIMAL(27,18) – if using MySQL, this will take 12 bytes (4 for each group of 9 digits). This is quite a reasonable storage size, and has enough range to support amounts as large as one billion or as small as one Wei. It can be sorted, compared, added, subtracted, etc. in the database without loss of precision.

I would use DECIMAL(27,18) (or DECIMAL(36,18) if you need to store truly huge values) to store cryptocurrency money values.

like image 86
Bradley Grainger Avatar answered Oct 22 '22 10:10

Bradley Grainger