What is the best type in SQL to use with Cryptocurrency?

Asked

Viewed 279 times

8

I am developing a system where I will store transactions of Cryptocurrencies, type Bitcoin, I can not in any way have problems of conversion and rounding, in C# I checked and the best is decimal, right ?

The values will be in this format:

0,000001

I know that money in SQL Server is not a good option, I must go to a decimal (x,x)?

I can use SQL Server or Mysql database, so I put the tag of the two here, because it is still being decided

  • 2

    Basically what you want is to know which is the best type in the database to represent monetary values with many decimals after the comma, is that it? If it is, the fact that it is bitcoin or anything else, becomes irrelevant.

  • Exactly, I used the example of bitcoin for the number of decimals, which normally with normal monetary transactions (I think) does not have as many houses as.

  • @Josédiz thanks, maybe I expressed myself badly talking about money type, but it is not a good practice, I always see people talking to NOT use in any way.. would you know in mysql if decimal applies the same way? obg.

  • @vnbrs I researched about the type to work with money, but my doubt on this topic I was not sure if it would meet the amount of decimal places and precision required for my need, generating doubt.

Show 1 more comment

1 answer

3


To store monetary values one should avoid the type of data float (floating point). This goes for any language. As for the type of data money, works correctly, with 4 decimal places fixed.

By example you need 6 decimal places, which eliminates the use of data type money. In SQL Server you can use decimal (p, s), where p is the accuracy and s is the scale. That is, use 6 as the scale. To define the precision, be aware that it is necessary to count both the whole part and the fractional part. For example, to store values up to 9,999,999,999 are 4 digits in the whole part and 6 digits in the fractional part. Therefore, the accuracy should be
4 + 6 = 10
That is to say, decimal (10, 6).

As for Mysql, I haven’t used it for some time, except for sporadic testing. But, referring to the documentation, it is the same rule of SQL Server regarding the definition of precision and scale.

Documentation:


Read also:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.