Oracle 中LONG RAW BLOB CLOB型別介紹

indexman發表於2015-08-31

說明:

RAW: 未加工型別,可儲存二進位制資料或位元組符
LONG: 可變長的字串資料,最長2G,LONG具有VARCHAR2列的特性,可以儲存長文字一個表中最多一個LONG列【不建議使用】
LONG RAW: 可變長二進位制資料,最長2G 【不建議使用】
CLOB: 字元大物件Clob 用來儲存單位元組的字元資料;大型文字,例如XML資料。
NCLOB: 用來儲存多位元組的字元資料
BLOB: 用於儲存二進位制大物件資料;例如數碼照片;
BFILE: 儲存在檔案中的二進位制資料,這個檔案中的資料只能被只讀訪。但該檔案不包含在資料庫內。
bfile欄位實際的檔案儲存在檔案系統中,欄位中儲存的是檔案定位指標.bfile對oracle來說是隻讀的,也不參與事務性控制和資料恢復.
  
CLOB,NCLOB,BLOB都是內部的LOB(Large Object)型別,最長4G,沒有LONG只能有一列的限制

注意: LONG 和 LONG RAW在Oracle新版已不推薦使用(使用BLOB替代),只是為了向後相容而保留著。

本文著重介紹:RAW/CLOB/BLOB

1、RAW型別
1.1 介紹
You use the RAW datatype to store binary data or byte strings. For example, a RAW
variable might store a sequence of graphics characters or a digitized picture. Raw data
is like VARCHAR2 data, except that PL/SQL does not interpret raw data. Likewise,
Oracle Net does no character set conversions when you transmit raw data from one
system to another.
The RAW datatype takes a required parameter that lets you specify a maximum size up
to 32767 bytes. The syntax follows:
RAW(maximum_size)
You cannot use a symbolic constant or variable to specify the maximum size; you must
use an integer literal in the range 1 .. 32767.
You cannot insert RAW values longer than 2000 bytes into a RAW column. You can insert
any RAW value into a LONG RAW database column because the maximum width of a
LONG RAW column is 2147483648 bytes or two gigabytes. However, you cannot retrieve
a value longer than 32767 bytes from a LONG RAW column into a RAW variable. Note
that the LONG RAW datatype is supported only for backward compatibility; see “LONG
and LONG RAW Datatypes” on page 3-5 for more information.

RAW英語的意思為:生的;未加工的;
你可以使用RAW型別儲存二進位制資料或位元組符。例如,一個RAW變數可以儲存一系列圖形字元或一張數碼照片。
RAW資料就像VARCHAR2資料,除了一點:PL/SQL不會對其進行解釋。同樣的,當你在傳輸RAW資料時,Oracle Net不會對其進行字符集轉換。

RAW資料型別要求指定一個最大值到32767的引數;

宣告格式如下: RAW(maximum_size)
你不能使用一個符號常量或變數來代替該引數而必須使用1..32767中的任一整數。

你不能往RAW列中插入超過2000位元組的字元;
你可以往long raw列中插入任何raw資料,最大支援2G。然而,反過來則無法一次性取出超過32767位元組的raw資料。

此處需要注意,long raw是早起版本的型別;現在已不建議使用;詳細見以下內容:

1.2 相關工具
–包
utl_raw

–函式
utl_raw.cast_to_raw
utl_raw.cast_to_number
utl_raw.cast_to_varchar2
hextoraw

RAW儲存的為16進位制數。當使用HEXTORAW時,會把字串中資料當作16進位制數。
而使用UTL_RAW.CAST_TO_RAW時,直接把字串中每個字元的ASCII碼存放到RAW型別的欄位中。

1.3 例子

drop table test_raw;
create table test_raw(msg   raw(2000));


SCOTT@orcl> insert into test_raw values('<xml><name>Dylan</name><score>100</score></xml>');
insert into test_raw values('<xml><name>Dylan</name><score>100</score></xml>')
                            *
第 1 行出現錯誤:
ORA-01465: 無效的十六進位制數字

--這個地方注意是十六進位制
SCOTT@orcl> insert into test_raw values(utl_raw.cast_to_raw('<xml><name>Dylan</name><score>100</score></xml>'));

已建立 1 行。

SCOTT@orcl> commit;

--檢視
select msg from test_raw;
MSG
------------------------------------------------------------------------------
3C786D6C3E3C6E616D653E44796C616E3C2F6E616D653E3C73636F72653E3130303C2F73636F72
653E3C2F786D6C3E

0ABC

SCOTT@orcl> select utl_raw.cast_to_varchar2(msg) from test_raw;

UTL_RAW.CAST_TO_VARCHAR2(MSG)
------------------------------------------------------------------------------
<xml><name>Dylan</name><score>100</score></xml>

2、LONG和LONG RAW型別

可以使用LONG型別儲存變長字串。Long型別就像VARCHAR2一樣,除了LONG的最大容量為32760;

使用LONG RAW型別儲存二進位制資料或位元組字串。LONG RAW資料就像LONG資料,除了LONG RAW資料不會被PL/SQL解釋。
LONG RAW的最大容量也為32760.

你可以往LONG列中插入任何LONG資料,最大長度為2G。然而,PL/SQL中的LONG型別變數只能支援到32760。
這條規則同樣適用於LONG RAW型別。

表中的LONG列可以儲存文字,字元陣列,甚至短文件。可以針對該型別列做UPDATE, INSERT, 和SELECT 操作。
但是無法再表示式,SQL函式呼叫或特定的SQL條件語句例如WHERE, GROUP BY和CONNECT BY。

In SQL statements, PL/SQL binds LONG values as VARCHAR2, not as LONG. However,
if the length of the bound VARCHAR2 exceeds the maximum width of a VARCHAR2
column (4000 bytes), Oracle converts the bind type to LONG automatically, then issues
an error message because you cannot pass LONG values to a SQL function

SQL語句中, PL/SQL將LONG型別作為VARCHAR2型別繫結。然而,如果所繫結的VARCHAR2長度超出了4000,ORACLE會自動轉換到LONG,
然後丟擲一個錯誤因為你不能將LONG值傳遞給SQL函式。

--例如:
SCOTT@orcl> create table long_test(id number, msg long);

表已建立。

SCOTT@orcl> insert into long_test values(1,'hello world');

已建立 1 行。

SCOTT@orcl> commit;

提交完成。

SCOTT@orcl> select * from long_test where msg='123';
select * from long_test where msg='123'
                              *
第 1 行出現錯誤:
ORA-00997: 非法使用 LONG 資料型別


SCOTT@orcl> /

        ID MSG
---------- --------------------------------------------------------------------------------
         1 hello world

SCOTT@orcl> select id, trim(msg) from long_test where id = 1;
select id, trim(msg) from long_test where id = 1
                *
第 1 行出現錯誤:
ORA-00932: 資料型別不一致: 應為 NUMBER, 但卻獲得 LONG

3、CLOB
可以使用CLOB型別大塊的字元資料。每一個CLOB變數儲存一個定位器,指向一個大塊字元資料。

CLOBs participate fully in transactions, are recoverable, and can be replicated. Changes
made by package DBMS_LOB can be committed or rolled back. CLOB locators can span
transactions (for reads only), but they cannot span sessions.

CLOB參與整體事務,可恢復,並且可以重複。
由DBMS_LOB包改變的資料可以提交和回滾。CLOB定位器可以跨事務,但不能跨會話。

4、BLOB
You use the BLOB datatype to store large binary objects in the database, in-line or
out-of-line. Every BLOB variable stores a locator, which points to a large binary object.
BLOBs participate fully in transactions, are recoverable, and can be replicated. Changes
made by package DBMS_LOB can be committed or rolled back. BLOB locators can span
transactions (for reads only), but they cannot span sessions.

用於儲存大二進位制物件,BLOB參與整體事務,可恢復,並且可以重複。
由DBMS_LOB包改變的資料可以提交和回滾。BLOB定位器可以跨事務,但不能跨會話。

drop table blob_test;

SCOTT@orcl>  create table blob_test(   id number primary key,   content blob not null);

表已建立。

SCOTT@orcl> insert into blob_test values(1,'11111000011111');

已建立 1 行。

SCOTT@orcl> commit;

提交完成。

SCOTT@orcl> select * from blob_test;

SCOTT@orcl> set linesize 2000
SCOTT@orcl> /

        ID CONTENT
---------- -----------------------------------
         1 11111000011111


SCOTT@orcl> insert into blob_test values(1,'11111000011111>');
insert into blob_test values(1,'11111000011111>')
                                             *
第 1 行出現錯誤:
ORA-01465: 無效的十六進位制數字


 SCOTT@orcl> update blob_test set content=to_blob('110010000110011') where id=1;

已更新 1 行。

SCOTT@orcl> rollback
  2  ;

回退已完成。

SCOTT@orcl> select * from blob_test;

        ID CONTENT
---------- ---------------------------------------------------------------------
         1 11111000011111

 delete from blob_test where id=1; 
 commit;

相關文章