Database Reference
In-Depth Information
Binary Strings: RAW Types
Oracle supports the storage of binary data as well as text. Binary data is not subject to the character set conversions we
discussed earlier with regard to the CHAR and VARCHAR2 types. Therefore, binary datatypes are not suitable for storing
user-supplied text, but are suitable for storing encrypted information—encrypted data is not “text,” but a binary
representation of the original text, word processing documents containing binary markup information, and so on. Any
string of bytes that should not be considered by the database to be “text” (or any other base datatype such as a number,
date, and so on) and that should not have character set conversion applied to it should be stored in a binary datatype.
Oracle supports three datatypes for storing binary data:
RAW type, which we focus on in this section, is suitable for storing RAW data up to
2,000 bytes in size. Starting with 12 c , you can configure a RAW to store up to 32,767 bytes of
information.
The
BLOB type, which supports binary data of much larger sizes. We'll defer coverage of this
until the “LOB Types” section later in the chapter.
The
LONG RAW type, which is supported for backward compatibility and should not be
considered for new applications.
The syntax for the binary RAW type is straightforward:
The
RAW( <size> )
For example, the following code creates a table capable of storing 16 bytes of binary information per row:
EODA@ORA12CR1> create table t ( raw_data raw(16) );
Table created.
The RAW type is much like the VARCHAR2 type in terms of storage on disk. The RAW type is a variable length binary
string, meaning that the table T just created, for example, may store anywhere from 0 to 16 bytes of binary data. It is
not padded out like the CHAR type.
When dealing with RAW data, you will likely find it being implicitly converted to a VARCHAR2 type—that is, many
tools, such as SQL*Plus, will not display the RAW data directly but will convert it to a hexadecimal format for display.
In the following example, we create some binary data in our table using SYS_GUID() , a built-in function that returns
a 16-byte RAW string that is globally unique ( GUID stands for globally unique identifier ):
EODA@ORA12CR1> insert into t values ( sys_guid() );
1 row created.
EODA@ORA12CR1> select * from t;
RAW_DATA
--------------------------------
EEF18AA30B563AF0E043B7D04F0A4A30
You can immediately note two things here. First, the RAW data looks like a character string. That is just how
SQL*Plus retrieved and printed it; that is not how it is stored on disk. SQL*Plus cannot print arbitrary binary data on
your screen, as that could have serious side effects on the display. Remember that binary data may include control
characters such as a carriage return or linefeed—or maybe a Ctrl-G character that would cause your terminal to beep.
 
Search WWH ::




Custom Search