首页 > 代码库 > raw数据类型

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 字节。可能这样的数据类型在数据库效率上会提高,而且对数据由于字符集的不同而导致的不一致的可能性在这边也排除了。

官方定义:

技术分享
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数据类型