Oracle 中LONG RAW BLOB CLOB类型介绍

说明:

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));

[email protected]> 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: 无效的十六进制数字

--这个地方注意是十六进制
[email protected]> insert into test_raw values(utl_raw.cast_to_raw(‘<xml><name>Dylan</name><score>100</score></xml>‘));

已创建 1 行。

[email protected]> commit;

--查看
select msg from test_raw;
MSG
------------------------------------------------------------------------------
3C786D6C3E3C6E616D653E44796C616E3C2F6E616D653E3C73636F72653E3130303C2F73636F72
653E3C2F786D6C3E

0ABC

[email protected]> 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函数。

--例如:
[email protected]> create table long_test(id number, msg long);

表已创建。

[email protected]> insert into long_test values(1,‘hello world‘);

已创建 1 行。

[email protected]> commit;

提交完成。

[email protected]> select * from long_test where msg=‘123‘;
select * from long_test where msg=‘123‘
                              *
第 1 行出现错误:
ORA-00997: 非法使用 LONG 数据类型

[email protected]> /

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

[email protected]> 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;

[email protected]>  create table blob_test(   id number primary key,   content blob not null);

表已创建。

[email protected]> insert into blob_test values(1,‘11111000011111‘);

已创建 1 行。

[email protected]orcl> commit;

提交完成。

[email protected]> select * from blob_test;

[email protected]> set linesize 2000
[email protected]> /

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

[email protected]> insert into blob_test values(1,‘11111000011111>‘);
insert into blob_test values(1,‘11111000011111>‘)
                                             *
第 1 行出现错误:
ORA-01465: 无效的十六进制数字

 [email protected]> update blob_test set content=to_blob(‘110010000110011‘) where id=1;

已更新 1 行。

[email protected]> rollback
  2  ;

回退已完成。

[email protected]> select * from blob_test;

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

 delete from blob_test where id=1;
 commit;

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 12:20:44

Oracle 中LONG RAW BLOB CLOB类型介绍的相关文章

Hibernate的Annotation中实体BLOB CLOB类型的注解

在Hibernate Annotation中,实体BLOB.CLOB类型的注解与普通的实体属性有些不同,具体操作如下: BLOB类型的属性声明为byte[]或者java.sql.Blob: @Lob @Basic(fetch=FetchType.LAZY) @Column(name="IMGS", columnDefinition="BLOB", nullable=true) private byte[] imgs; public byte[] getImgs()

oracle中int类型和number类型区别

INT类型是NUMBER类型的子类型.下面简要说明:(1)NUMBER(P,S)该数据类型用于定义数字类型的数据,其中P表示数字的总位数(最大字节个数),而S则表示小数点后面的位数.假设定义SAL列为NUMBER(6,2)则整数最大位数为4位(6-2=4),而小数最大位数为2位.(2)INT类型当定义整数类型时,可以直接使用NUMBER的子类型INT,顾名思义:INT用于整型数据. oracle本来就没有int类型,为了与别的数据库兼容,新增了int类型作为number类型的子集.int类型只能

oracle中一些关于blob字段的操作

---恢复内容开始--- 1.在IDE中查看blob字段的内容可以采用: UTL_RAW.CAST_TO_VARCHAR2(blob)的方法,其中blob为表中blob字段的列名.这个方法限定结果不可超过2000字节. 2. 更新blob时,碰到德文乱码问题,最后采用的是在转换为byte[]后,再次转换为new String(ISO-8839-1)就可以

MyBatis 杂项(分页,缓存,处理BLOB\CLOB数据)

1.处理CLOB,BLOB数据 oracle中的 clob:clob blob:blobmysql中的 clob:longtext blob:longblob 2.传入多个输入参数,mybatis自带的param属性(但是不经常用,我们用map就足够了) 3.MyBatis分页 逻辑分页:将数据全部取出先放到内存中,之后在内存中进行分页,性能不好.不推荐使用 物理分页:通过语句进行分页. 4.MyBatis缓存 MyBatis默认情况下:MyBatis默认使用一级缓存,即同一个SqlSessio

Oracle中DDL的基础知识

1.SQL分为5大类: DDL:数据定义语言 DCL:数据控制语言 DML:数据操纵语言 DTL:数据事务语言 DQL:数据查询语言 2.DDL(data definition language):create,drop,alter,rename to 创建表格分为两个步骤: a.定义列和数据类型 b.添加约束 数据类型: 1.数字类型,可以做所有的数学运算 number number(4)代表整数,最大能存9999 number(7,2)代表double类型,整数长度为5,小数位2 2.字符型

oracle中Blob和Clob类型的区别

一.oracle中Blob和Clob类型的区别BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图片.文件.音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去.而像文章或者是较长的文字,就用CLOB存储,这样对以后的查询更新存储等操作都提供很大的方便. BLOB全称为二进制大型对象(Binary Large Object).它用于存储数据

oracle中,有4个大对象(lobs)类型可用,分别是blob,clob,bfile,nclob。

1)blob:二进制lob,为二进制数据,最长可达4GB,存贮在数据库中. 2)clob:字符lob,字符数据,最长可以达到4GB,存贮在数据库中. 3)bfile:二进制文件;存贮在数据库之外的只读型二进制数据,最大长度由操作系统限制. 4)nclob:支持对字节字符集合(nultibyte characterset)的一个clob列.

Oracle中Blob转换成Clob

假如tab表中的c_xml字段原来是blob类型,我们要将其转换为clob类型,如果表中有数据的话,是无法直接通过alert语句去修改的.通过以下方法可以将blob类型的字段改为clob类型. 首先在oracle中创建一个function,代码如下: --先创建Blog转换为Clob的function CREATE OR REPLACE FUNCTION BlobToClob(blob_in IN BLOB) RETURN CLOB AS v_clob CLOB; v_varchar VARCH

解决:oracle+myBatis ResultMap 类型为 map 时,表字段类型有 Long/Blob/Clob 时报错

前言:最近在做一个通用查询单表的组件,所以 sql 的写法就是 select *,然后 resultType="map" .如果数据库中的表里有字段类型为 Long 等类型时,mybatis 在执行 sql 时会报错,如果表中有 Blob.Clob 类型在转 json 是也会报错,而且我这里也需要将这几种类型都转为 String 类型到前端. long 类型 sql 报错: Blob/Clob 转 json 报错: 解决方案: 自定义 typeHandle 来统一处理数据库这些特殊的字