1. 程式人生 > >raw數據類型

raw數據類型

doc nbsp 實用 nor 聲明 word 一個數據庫 document ade

Oracle中用於保存位串的數據類型是RAW,LONG RAW(推薦使用BLOB)。

RAW,類似於CHAR,聲明方式RAW(L),L為長度,以字節為單位,作為數據庫列最大2000,作為變量最大32767字節。

LONG RAW,類似於LONG,作為數據庫列最大存儲2G字節的數據,作為變量最大32760字節

RAW類型的好處就是:在網絡中的計算機之間傳輸 RAW 數據時,或者使用 oracle 實用程序將 RAW 數據從一個數據庫移到另一個數據庫時,Oracle 服務器不執行字符集轉換。存儲實際列值所需要的字節數大小隨每行大小而異,最多為 2,000 字節。可能這樣的數據類型在數據庫效率上會提高,而且對數據由於字符集的不同而導致的不一致的可能性在這邊也排除了。

官方定義:

技術分享
The LONG RAW datatype is provided for backward compatibility with existing applications. For new applications, use the BLOB and BFILEdatatypes for large amounts of binary data.

The RAW and LONG RAW datatypes are used for data that is not to be interpreted (not converted when moving data between different systems) by Oracle. These datatypes are intended for
binary data or byte strings. For example, LONG RAW can be used to store graphics, sound, documents, or arrays of binary data. The interpretation depends on the use. RAW is a variable-length datatype like the VARCHAR2 character datatype, except Oracle Net Services (which connects user sessions to the instance) and the Import and Export utilities do
not perform character conversion when transmitting RAW or LONG RAW data. In contrast, Oracle Net Services and Import/Export automatically convertCHAR, VARCHAR2, and LONG data between the database character set and the user session character set (set by the NLS_LANGUAGE parameter of the ALTER SESSION statement), if the two character sets are different. When Oracle automatically converts RAW or LONG RAW data to and from CHAR data, the binary data is represented in hexadecimal form with one hexadecimal character representing every four bits of RAW data. For example, one byte of RAW data with bits 11001011 is displayed and entered as CB. LONG RAW data cannot be indexed, but RAW data can be indexed.
View Code

建表操作:

  create table raw_test (id number, raw_date raw(10));

  插入raw數據操作:

  insert into raw_test values (1, hextoraw(‘ff‘));

  insert into raw_test values (1,utl_raw.cast_to_raw(‘051‘));

  刪除表操作:

  drop table raw_test;

  可以使用dump函數,查詢存儲情況:

  select id,raw_date, dump(raw_date, 16) dump_raw from raw_test;

  當使用HEXTORAW時,會把字符串中數據當作16進制數。而使用UTL_RAW.CAST_TO_RAW時,直接把字符串中每個字符的ASCII碼存放到RAW類型的字段中.

  下面是常用到了兩個函數:
utl_raw.cast_to_raw([varchar2]);--將varchar2轉換為raw類型
utl_raw.cast_to_varchar2([raw]);--將raw轉換為varchar2類型

其實RAW和VARCHAR是類似的,只是存儲在RAW裏的是二進制值,在任何時候不會做自動的字符集轉換,這是RAW和VARCHAR的不同,RAW只是一種外部類型,其內部存儲是VARRAW

 VARCHAR的Oracle內部定義是:struct { ub2 len; char arr[n] }

 VARRAW的ORACLE內部定義是: struct { ub2 len; unsigned char arr[n] }

  

raw數據類型