引言:
数据库中的xmltype类型数据主要用于存储和管理XML(可扩展标记语言)数据。这种数据类型允许在SQL中直接访问和操作XML文档,为XML开发人员提供了在XML文档中使用XML标准的能力。如果数据库不支持xmltype类型数据,那么存储、查询和管理XML数据的方式将会有所不同,会对数据库的存储复杂性,查询性能带来极大影响。本次是对磐维数据库V2.0版本xmltype支持的一次实践。
前置条件:
在数据库中执行:
drop table if exists test_xmltype;
create table test_xmltype(i int, d xmltype);
清空表test_xmltype,并重建
- DML操作:
表中插入数据:
insert into test_xmltype values(1,'abc');
insert into test_xmltype values(2,'<test>abc</test>');
insert into test_xmltype values(3,'abc \153\154\155 \052\251\124');
查询插入的数据:
select * from test_xmltype;
修改数据:
update test_xmltype set d='abc11' where i=1;
select * from test_xmltype;
update test_xmltype set d=' <foo>abc</foo><bar>123</bar>' where i=3;
删除数据
delete from test_xmltype where i=1;
select * from test_xmltype;
- 结合函数操作:
--插入数据
Insert INTO test_xmltype
VALUES (1,xmlType.createXML('<?xml version="1.0" encoding="UTF-8" ?>
<collection xmlns="">
<record>
<leader>-----nam0-22-----^^^450-</leader>
<datafield tag="200" ind1="1" ind2=" ">
<subfield code="a">抗震救灾</subfield>
<subfield code="f">奥运会</subfield>
</datafield>
<datafield tag="209" ind1=" " ind2=" ">
<subfield code="a">经济学</subfield>
<subfield code="b">计算机</subfield>
<subfield code="c">10001</subfield>
<subfield code="d">2005-07-09</subfield>
</datafield>
<datafield tag="610" ind1="0" ind2=" ">
<subfield code="a">计算机</subfield>
<subfield code="a">笔记本</subfield>
</datafield>
</record>
</collection>')) ;
--使用exact提取标签
select extract(x.d,'/collection/record/datafield/subfield') xmlseq from test_xmltype x;
select extract(x.d,'/collection/record/datafield') xmlseq from test_xmltype x;
--使用XmlSequence将提取的数据转为数组并通过unnest函数转为表
select unnest(XMLSequence(extract(x.d,'/collection/record/datafield/subfield'))) xmlseq from test_xmltype x;
--使用GetStringVal函数以字符串形式返回xml示例的值
select getStringVal(extract(x.d,'/collection/record/datafield/subfield')) a from test_xmltype x;
select getStringVal(extract(x.d,'/collection/record/datafield')) a from test_xmltype x;
--使用ExistsNode函数检查节点是否存在
select existsnode(x.d,'/collection/record/datafield[@tag="209"]/subfield[@code="a"]') as a from test_xmltype x;
select existsnode(x.d,'/collection/record/datafield[@tag="209"]/subfield[@code="f"]') as a from test_xmltype x;
--创建视图
create view v_xmltype as select * from test_xmltype;
select * from v_xmltype;