I'm using the Ethereum api. I want to store the information from the api into a mysql table.
The address data looks like:
0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be
0x1d80982502f3bb75654df13aa32bbd5ac9cab7d6
0xaf13bbdbe1ff53c2df7109a53c217320d2d76ee2
...
I've been using just varchar column for these characters. Are there a better data type? I'm thinking maybe varbinary but I don't know if there's any advantages. The disadvantage is that the sql code will be messier as I will have to HEX() and UNHEX().
You can use varbinary type of column to store the hexa values. You can cast/convert them to integer as and when required using the cast/convert functions.
HEX() : This function in MySQL is used to return an equivalent hexadecimal string value of a string or numeric Input. If the input is a string then each byte of each character in the string is converted to two hexadecimal digits.
For me no specific type exists, you need to choose between text and binary :
CHAR(40) : (charset is not important here)
non hexa
dataBINARY(20) :
For me data consistency is the most important point : I prefer binary option.
Moreover Hex
/unHex
are very simple function
Hint : You can store your data in BINARY column and create an hexa
view to see easily hexa value.
Here is an example to store data in binary zone http://rextester.com/SEV11235
Advantage CHAR
: simplicity.
Advantage BINARY
: you save 22 bytes per row.
In my opinion, the complexity increase isn't worth the space saving unless you're looking at a minimum of million-row databases. Even then I probably wouldn't bother.
In either case, if you care that much you can save a byte or two (and possibly improve your DB structure optimization) by using a fixed-length column rather than variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With