One thing that can be quite nice to have in the XML store is constraints to avoid duplicate data, indexes on XML data are pretty much plain standard pseudo-column indexes (or "functional indexes" as some refer to them as).
We just use the basic extract() or extractValue() functions in Oracles XML feature set.
Here's an example on how to to create unique constraints (indexes) on XML elements (or attributes):
SQL> create table x (a xmltype);And some docs to read.
Table created.
SQL> insert into x values('<type><name>dog</name></type>');
1 row created.
SQL> insert into x values('<type><name>cat</name></type>');
1 row created.
SQL> create unique index xui on x(extractValue(a, '/type/name'));
Index created.
SQL> insert into x values('<type><name>cat</name></type>');
insert into x values('<type><name>cat</name></type>')
*
ERROR at line 1:
ORA-00001: unique constraint (HLINDEN.XUI) violated
SQL> insert into x values('<type><name>fish</name></type>');
1 row created.
-- Lets try the constraint on an attribute.
-- attributes are handeled just like elements but need a @ sign prefix
SQL> drop index xui;
Index dropped.
SQL> truncate table x;
Table truncated.
SQL> insert into x(a) values('<type id="1"><name>sally</name></type>');
1 row created.
SQL> insert into x(a) values('<type id="2"><name>bob</name></type>');
1 row created.
SQL> create unique index xui on x(extractValue(a, '/type/@id'));
Index created.
SQL> insert into x(a) values('<type id="2"><name>carol</name></type>');
insert into x(a) values('<type id="2"><name>carol</name></type>')
*
ERROR at line 1:
ORA-00001: unique constraint (HLINDEN.XUI) violated
SQL> insert into x(a) values('<type id="3"><name>carol</name></type>');
1 row created.
-- Ok, lets see if we can have duplicate names.
SQL> insert into x(a) values('<type id="4"><name>carol</name></type>');
1 row created.
SQL>
1 comment:
I was so happy when I realized that Oracle introduced XML support in its version 9i, I was going to buy the software, but in the end I decided to Buy cialis, and I have not regret it so far :)
Post a Comment