raw数据类型

本文详细介绍了Oracle数据库中的RAW与LONGRAW数据类型,包括它们的特点、用途及如何在实际应用中进行操作。RAW类型适用于不需要解释的二进制数据,如图像或声音文件,而LONGRAW则主要用于向后兼容,新应用建议使用BLOB。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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] }

  

 

posted @ 2017-06-14 10:42 酸奶加绿茶 阅读( ...) 评论( ...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值