简体   繁体   中英

Best data type for hex values in Oracle database

What is the best data type for storing hex values in an Oracle database? With a 33-byte value like this:

0x54906a620d3c1e82c8e812b0f8d1214beafc05c74820c1dd3675b0c38f4a83aa

is there a preferred data type that will:

a) use minimal disk space, and b) be queryable via that full string?

To store that value, I can use VARCHAR2(132) . But does that use 132 bytes on the disk, or 33 bytes?

If it uses 132 bytes on the disk, then what data type could I use to store only 33 bytes? And will that datatype allow me to use the full string in a query, like:

SELECT * FROM TABLE WHERE FIELD = '0x54906a620d3c1e82c8e812b0f8d1214beafc05c74820c1dd3675b0c38f4a83aa'

or would it require me to convert my 132 characters to 33 bytes before using in a SQL query?

if "a8" is a hex value of a single byte, will varchar2 store 1 byte, or will it store 2 bytes (one for the letter 'a' and one for the number 8)?

The latter, if you store it as a string.

You can store as RAW :

These data types are intended for binary data or byte strings.

For example:

create table test (hex_value raw(128));
insert into test (hex_value) values (hextoraw('54906a620d3c1e82c8e812b0f8d1214beafc05c74820c1dd3675b0c38f4a83aa'));
select hex_value, dump(hex_value)
from test
where hex_value = hextoraw('54906a620d3c1e82c8e812b0f8d1214beafc05c74820c1dd3675b0c38f4a83aa');
HEX_VALUE DUMP(HEX_VALUE)
0x54906A620D3C1E82C8E812B0F8D1214BEAFC05C74820C1DD3675B0C38F4A83AA Typ=23 Len=32: 84,144,106,98,13,60,30,130,200,232,18,176,248,209,33,75,234,252,5,199,72,32,193,221,54,117,176,195,143,74,131,170

You can see from the dump that it is stored in 32 bytes. (The RAW value is converted implicitly to a string for display, and the 0x is being added by db<>fiddle; other clients won't show that, and might not show RAW values at all.)

You can convert back to a string if you need to, which will then be 64 bytes:

select rawtohex(hex_value), dump(rawtohex(hex_value)) from test;
RAWTOHEX(HEX_VALUE) DUMP(RAWTOHEX(HEX_VALUE))
54906A620D3C1E82C8E812B0F8D1214BEAFC05C74820C1DD3675B0C38F4A83AA Typ=1 Len=64: 53,52,57,48,54,65,54,50,48,68,51,67,49,69,56,50,67,56,69,56,49,50,66,48,70,56,68,49,50,49,52,66,69,65,70,67,48,53,67,55,52,56,50,48,67,49,68,68,51,54,55,53,66,48,67,51,56,70,52,65,56,51,65,65

fiddle

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