Solved

Storing Hex values

Posted on 2010-09-08
4
491 Views
Last Modified: 2012-05-10
Good morning,
I have a collection of HEX values, which are 36 characters long I need to store efficently in a database.

My problem is I dont know what to use. If I use VARCHAR(36) then it seems to waste a lot of space. If I store it as Blobs I cant index the values.

The table has 2 coloums, one being a bigint with autonumbering, and the other being the field with the Hex values in.

Any suggestions that I can use to optimise my table would be appriciated.

Thank you
0
Comment
Question by:tonelm54
4 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33625885
If they are ALWAYS going to be 36-char, then BINARY(18) would be most efficient*.
Otherwise VARBINARY(18) for varying lengths up to hex(36).

*Storage-wise: Each BINARY byte will hold the equivalent data of 2 HEX values, but you need some processing to encode/decode between hex and binary.
0
 
LVL 3

Expert Comment

by:gremwell
ID: 33626175
Note that to have an index BLOB and BINARY fields you need to specify a prefix length.

"Prefix lengths are given in characters for nonbinary string types and in bytes for binary string types. That is, index entries consist of the first length  characters of each column value for CHAR, VARCHAR, and TEXT columns, and the first length bytes of each column value for BINARY, VARBINARY, and BLOB columns"

See http://dev.mysql.com/doc/refman/5.0/en/create-index.html for more details.
0
 

Author Comment

by:tonelm54
ID: 33627281
Is there a way to insert Hex into a binary field, and return hex?

So-
Insert into myTable (`blobData`) VALUES (Hex2Bin(0x3451346342456));

And then:-
select 2hex(`blobData`) from `myTable`
0
 
LVL 8

Accepted Solution

by:
wolfgang_93 earned 500 total points
ID: 33631432
This worked for me for MySQL version 5.0 :

mysql> create table fubar (aa binary(18));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into fubar values(0xF1E2D3C4B5010203040506070809A1A2B3C4);
Query OK, 1 row affected (0.01 sec)

mysql> select hex(aa) from fubar;
+--------------------------------------+
| hex(aa)                              |
+--------------------------------------+
| F1E2D3C4B5010203040506070809A1A2B3C4 |
+--------------------------------------+
1 row in set (0.00 sec)



0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now