I have a Table with 2 columns: 'Id' (datatype=INT) , 'Representation' (datatype=binary). I want to store a hexadecimal value in the form of binary digits in the 'Representation' Column. What is the Max number of Binary digits that i can store in the 'Representation' column ?
1 Answer
MySql
BINARY
The MySql docs on the binary data type, mention:
The permissible maximum length is the same for
BINARYandVARBINARYas it is forCHARandVARCHAR, except that the length forBINARYandVARBINARYis a length in bytes rather than in characters.
So binary is put on the same level as char, and varbinary as varchar.
The docs on the char data type, mention:
The length of a
CHARcolumn is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255.
So the maximum size for binary is therefore achieved with this:
CREATE TABLE mytable (
id int,
representation binary(255)
)
This corresponds to 255 bytes of data, which corresponds to 510 hexadecimal digits, or 2040 bits.
VARBINARY
The varbinary type can store up to 65,535 bytes, from which the sizes of the other columns must be subtracted. Again, this follows from the docs on varchar:
Values in
VARCHARcolumns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of aVARCHARis subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
So let's say you would need room for about 500 bytes in other columns, then you could defined this table:
CREATE TABLE mytable (
id int, // takes 4 bytes
representation binary(65000),
// other fields come here, taking up less than 532 bytes
)
... you would have 65,000 bytes, i.e. 130,000 hexadecimal digits or 520,000 bits.
SQL Server Binary
The Transact-SQL docs on binary state:
binary [ ( n ) ]
Fixed-length binary data with a length of n bytes, where n is a value from 1 through 8,000. The storage size is n bytes.
This means that with this table definition:
CREATE TABLE mytable (
id int,
representation binary(8000)
)
... you can store 8,000 bytes, i.e. 16,000 hexadecimal digits or 64,000 bits.
Note that the limit for varbinary is the same. The following advise is given in the docs:
Use
varbinarywhen the sizes of the column data entries vary considerably.