In Oracle 10gR2, two XMLTYPE tables are created.
registration xml-schemes
begin dbms_xmlschema.registerSchema( 'tab1_schema.xsd', '<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="man"> <xs:complexType> <xs:sequence> <xs:element name="man_id" type="xs:int" /> <xs:element name="man_name" type="xs:string" /> <xs:element name="man_date" type="xs:date" /> <xs:element name="cars"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" name="car"> <xs:complexType> <xs:sequence> <xs:element name="car_id" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>', true, true, false, false); end; / begin dbms_xmlschema.registerSchema( 'tab2_schema.xsd', '<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="car"> <xs:complexType> <xs:sequence> <xs:element name="car_id" type="xs:int" /> <xs:element name="car_name" type="xs:string" /> <xs:element name="car_date" type="xs:date" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>', true, true, false, false); end; /
creating tables based on xml schemas
create table tab1 of xmltype xmlschema "tab1_schema.xsd" element "man";
create table tab2 of xmltype xmlschema "tab2_schema.xsd" element "car";
key creation
alter table tab1 add constraint tab1_pk primary key (xmldata. "man_id");
alter table tab2 add constraint tab2_pk primary key (xmldata. "car_id");
4. Question: how to create a foreign key for the tab1 table (the element "/ cars / car / car_id") on the table tab2 (the element "/ car_id")
alter table tab1 add constraint tab1_fk foreign key (xmldata."/cars/car/car_id") references tab2(xmldata."car_id");