Exforsys

Home arrow Reviews arrow PHP Oracle Training Tutorials

Inserting XML Data to Oracle Database

Page 2 of 2
Author : Exforsys Inc.     Published on: 1st May 2008

Using XML Schemas

Now that you have defined the XMLType storage for employee XML documents, you might want to load some data into the employees XML schema-based XMLType table generated during the schema registration. The simplest way to do this is to use the INSERT SQL statement, as follows:

Ads

Sample Code
  1.         CONN xmlusr/xmlusr
  2.         INSERT INTO employees VALUES(
  3.              XMLType(
  4.               '<employee id="100"></employee>
  5.                  <ename></ename>King
  6.                  <salary></salary>24000
  7.                '
  8.             ).createSchemaBasedXML('employee.xsd')
  9.           )
  10.         COMMIT
Copyright exforsys.com


In the above example, you use the createSchemaBasedXML method of XMLType to explicitly identify the employee XML schema when inserting a new row into the employees table.

Now, if you try to issue the INSERT statement shown in the listing again, you will receive the following error message:

ERROR at line 1: ORA-00001: unique constraint (XMLUSR.EMP_PKEY) violated

As you can see, an attempt to insert the same row into the employees table fails due to a EMP_PKEY primary key constraint violation. If you recall, you define the EMP_PKEY primary key on the id attribute of the EMPLOYEE element in the XML schema registered as discussed in this section earlier. This constraint makes it impossible to insert two employee XML documents with the same ID into the employees table.

NOTE Another way to load data into the employees table is via one of the internet protocols supported by Oracle XML DB. This mechanism is discussed in the Taking Advantage of Standard Internet Protocols section later in this chapter.

Finally, it's worth noting that you can always delete a registered XML schema along with all the database objects generated during its registration. For example, to delete the employee XML schema registered as discussed earlier in this section, you might issue the following PL/SQL block:

Sample Code
  1.                 CONN xmlusr/xmlusr
  2.                 BEGIN
  3.                 DBMS_XMLSCHEMA.deleteSchema(
  4.                 SCHEMAURL => 'employee.xsd',
  5.                 DELETE_OPTION => dbms_xmlschema.DELETE_CASCADE_FORCE)
  6.         END
  7. /
Copyright exforsys.com


NOTE Since the employees table is used in the subsequent examples, make sure to register the employee XML schema again as discussed earlier in this section. Also make sure to insert a row into the table as shown earlier in this section.

Ads

Besides deleting the employee XML schema, the above code deletes the employee_t object type and employees XMLType table generated during the schema registration process.

NOTE: For more information on using XML schemas in Oracle XML DB, you can refer to Oracle documentation: chapters: XML Schema Storage and Query: Basic and XML Schema Storage and Query: Advanced in the Oracle XML DB Developer's Guide. Also, for information on XML Schema language, you can refer to the W3C XML Schema Recommendation at http://www.w3.org/TR/xmlschema-0/" target="_blank" rel="nofollow".



 
This tutorial is part of a PHP Oracle Training Tutorials tutorial series. Read it from the beginning and learn yourself.

PHP Oracle Training Tutorials

 

Comments

 

Share


Connect

Related Articles

Exforsys e-Newsletter

Enhance Technical and Soft Skills every week, get our Job Interview Questions and Answers eBook free when you subscribe.

Subscribe and Get Free Bonus PDF Book

eBook

Subscribe