简体   繁体   中英

Store 128 bit binary number in MYSQL binary data type

In my research I've looked at MySql insert statement to binary datatype? and https://dba.stackexchange.com/questions/904/mysql-data-type-for-128-bit-integers

But I can't seem to replicate their results.

I am converting the 64 squares of a chess board into a 64 bit binary number

Like so 000010010100010110100011011001000100011110001010001000100000110

I have a column in the mysql table of type binary(8)

My assumption being it needs to be of size 8 bytes because it is 64 bits, however it is claiming the input I am trying to provide is too large.

I can find very little resources on the binary data type only and I know I must be making some mistake, I assume it is trying to read each value as a char to something but I am not sure why.

Please assist me on how I may store this large number in my table.

MySQL interprets the number literal 000010010100010110100011011001000100011110001010001000100000110 as a decimal number, which is larger than 8 bytes.

The binary data types work similarly to char/varchar/text, but the values can be any sequence of bytes.

You can use string literals with proper escaping, but it's difficult to read those manually.

The most convenient and universal way (that I know of) is to use the HEX and UNHEX functions to convert between hexadecimal representation and the bytes in the actual data. Eg:

INSERT INTO tbl (col) VALUES (UNHEX('5BA238AEB1E956BDEA214C4C7FE9951B'))


SELECT HEX(col) FROM tbl

You can also use binary number literals:

INSERT INTO tbl (col) VALUES (0b000010010100010110100011011001000100011110001010001000100000110)

If the values are 64 bit, you can use a BIGINT field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM