Oracle基本数据类型—RAW类型

oracle中用于保存位串的数据类型是RAW,LONG RAW(推荐使用BLOB)。

RAW,类似于CHAR,声明方式RAW(L),L为长度,以字节为单位,作为数据库列最大2000,作为变量最大32767字节。

LONG RAW,类似于LONG,作为数据库列最大存储2G字节的数据,作为变量最大32760字节

RAW类型的好处就是:在网络中的计算机之间传输 RAW 数据时,或者使用 Oracle 实用程序将 RAW 数据从一个数据库移到另一个数据库时,Oracle 服务器不执行字符集转换。存储实际列值所需要的字节数大小随每行大小而异,最多为 2,000 字节。可能这样的数据类型在数据库效率上会提高,而且对数据由于字符集的不同而导致的不一致的可能性在这边也排除了。

下面是官方的定义:
Note:

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.

常用于操作raw类型的函数:UTL_RAW.CAST_TO_RAW,hextoraw.

RAW保存的为16进制数当使用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类型
这里varchar2的字符集一般是GB2312。
因为RAW保存的为16进制数。故下面的SQL会报错:  insert into test_raw values (hextoraw('hh'))
insert into test_raw values (hextoraw('hh'))
*
ERROR 位于第 1 行:
ORA-01465: 无效的十六进制数字

raw类型数据列常用的位操作:
utl_raw.bit_or();
utl_raw.bit_and();
utl_raw.bit_xor();

你可能感兴趣的:(oracle)