设为首页 收藏本站
查看: 1851|回复: 1

[经验分享] oracle中RAW数据类型

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2013-12-2 08:56:08 | 显示全部楼层 |阅读模式

近日在研究v$latch视图时,发现一个从未见过的数据类型。v$latch 中ADDR属性的数据类型为RAW(4|8)  同时也发现v$process中的ADDR属性的数据类型也为RAW(4|8)。于是查了一下oracle 的SQL Language Reference文档,文档如下描述:


The RAW and LONG RAW data types store data that is notto be explicitly converted by Oracle Database when moving data between differentsystems. These data types are intended for binary data or byte strings.For example, you can use LONG RAW to store graphics,sound, documents, or arrays of binary data, for which the interpretation isdependent on the use.


Oracle strongly recommends that you convertLONG RAW columns to binary LOB (BLOB) columns. LOB columns are subject to farfewer restrictions than LONG columns. See TO_LOB for more information.


RAW is a variable-lengthdata type like VARCHAR2, except that Oracle Net (whichconnects client software to a database or one database to another) and theOracle import and export utilities do not perform character conversion whentransmitting RAW or LONG RAW data. In contrast, Oracle Net and the Oracleimport and export utilities automatically convert CHAR, VARCHAR2, and LONG databetween different database character sets, if data is transported betweendatabases, or between the database character set and the client character set,if data is transported between a database and a client. The client characterset is determined by the type of the client interface, such as OCI or JDBC, andthe client configuration (for example, the NLS_LANG environment variable).


When Oracle implicitlyconverts RAW or LONG RAW data to CHAR data, the resulting character valuecontains a hexadecimal representation of the binary input, where each character is a hexadecimal digit (0-9, A-F)representing four consecutive bits of RAW data. For example, one byte of RAWdata with bits 11001011 becomes the value CB.


When Oracle implicitly converts CHAR datato RAW or LONG RAW, it interprets each consecutive input character as ahexadecimal representation of four consecutive bits of binary data and buildsthe resulting RAW or LONG RAW value by concatenating those bits. If any of theinput characters is not a hexadecimal digit (0-9, A-F, a-f), then an error isreported. If the number of characters is odd, then the result is undefined.


The SQL functions RAWTOHEX and HEXTORAWperform explicit conversions that are equivalent to the above implicitconversions. Other types of conversions between RAW and CHAR data are possiblewith functions in the Oracle-supplied PL/SQL packages [url=]UTL_RAW[/url] and UTL_I18N

大概意思是该数据类型用于存储二进制格式的数据,像图像,声音,文档等等,但是oracle建议使用lob替代raw,LOB列比LONG受到更少的限制


Raw的优势:
在网络传输,或者使用导入导出工具时,oracle服务器不执行字符集转换,这样在数据库的效率上会有所提高,而且不会因为字符集不同而导致数据的不一致性


以下引用网友的测试,来说明Oracle implicitly converts RAW or LONG RAW data to CHAR data,the resulting character value contains a hexadecimal representation of thebinary input以及UTL_RAW的使用


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

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


测试:


SQL>create table datatype_test_raw(paddr raw(8));



Tablecreated


SQL>insert into datatype_test_raw(paddr) values(utl_raw.cast_to_raw('This is a rawtype test!'));


insertinto datatype_test_raw(paddr) values(utl_raw.cast_to_raw('This is a raw typetest!'))


ORA-01401:inserted value too large for column


SQL>alter table datatype_test_raw modify paddr raw(20);


Tablealtered


SQL>insert into datatype_test_raw(paddr) values(utl_raw.cast_to_raw('This is a rawtype test!'));


insertinto datatype_test_raw(paddr) values(utl_raw.cast_to_raw('This is a raw typetest!'))


ORA-01401:inserted value too large for column


SQL>insert into datatype_test_raw(paddr) values(utl_raw.cast_to_raw('This is a rawtest!'));


1row inserted


SQL>commit;


Commitcomplete


SQL>select * from datatype_test_raw;


PADDR

----------------------------------------

54686973206973206120726177207465737421


SQL>select utl_raw.cast_to_varchar2(paddr) from datatype_test_raw;


UTL_RAW.CAST_TO_VARCHAR2(PADDR

--------------------------------------------------------------------------------

Thisis a raw test!


SQL>insert into datatype_test_raw(paddr) values(utl_raw.cast_to_raw('中文测试'));


1row inserted


SQL>commit;


Commitcomplete


SQL>select utl_raw.cast_to_varchar2(paddr) from datatype_test_raw;


UTL_RAW.CAST_TO_VARCHAR2(PADDR

--------------------------------------------------------------------------------

Thisis a raw test!

中文测试


SQL>select paddr, utl_raw.cast_to_varchar2(paddr) from datatype_test_raw;


PADDR                          UTL_RAW.CAST_TO_VARCHAR2(PADDR

------------------------------------------------------------------------------------------------------------------------

54686973206973206120726177207465737421This is a raw test!

D6D0CEC4B2E2CAD4中文测试


这里用到了两个函数:

utl_raw.cast_to_raw([varchar2]);--将varchar2转换为raw类型

utl_raw.cast_to_varchar2([raw]);--将raw转换为varchar2类型

这里varchar2的字符集一般是GB2312。


另外:

utl_raw包的几个其他的函数用法:

utl_raw.cast_from_number([number]);

utl_raw.cast_to_number([number]);

位操作:

utl_raw.bit_or();

utl_raw.bit_and();

utl_raw.bit_xor();


另外还有转换函数:

hextoraw();--将对应16进制数转换为raw

关于raw和utl_raw的介绍到此结束。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-10861-1-1.html 上篇帖子: 解析oracle数据库优化利器OWI 下篇帖子: Oracle11gR2 找出ASM diskgroup中的ASM spfile oracle

尚未签到

发表于 2014-1-1 09:52:01 | 显示全部楼层
友情的界线要怎么分辨不能手牵手只好肩并肩…

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表