Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
XML and
Object-Relational DBMS
Joseph Woodside
IST 734 – Professor Matos
Cleveland State University
Introduction
Structured
data
Relational model
Table-row-column
Semi-structured/unstructured
Hierarchical
Content oriented
Introduction
Language
on different platforms
XML support from major suppliers
•
•
•
•
•
•
IBM
Microsoft - SOAP
Sun
Oracle – XML DB
Novell - DirXML
Linux
XML
Browser
support
Internet Explorer
Firefox
Mozilla
Netscape
Opera
XML
XML
eXtensible Markup Language
Similar to HTML
Designed to describe data
Tags are defined by the user
Does not do anything - XML used to structure,
store and send information
Platform independent
XML
Electronic
Commerce (EC)
Two common types of EC
• Business to Business (B2B)
• Business to Customer (B2C)
XML exchange
standard
XML extensions
WML – Wireless Markup Language
GML – Geographic Markup Language
XML
Closing tag required
Case sensitive
<DATA>Hello</DATA>
<dAtA>Hello<dAtA>
Proper nesting
<data>Hello</data>
<first><second>Hello</second></first>
Root element required
<root>
• <child>Hello</child>
</root>
White space preserved
Comment - <!– Comment -->
XML
Namespace
Name conflicts
Default namespace avoids using prefixes in all the
child elements
Defined in the start tag of an element
Not used to lookup information
Purpose is to give the namespace a unique name
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Oracle XML DB
XMLType
Native server datatype to indicate the column or table
contains XML
Allows schema validation and XSL transformations
Storage
Unstructured - stores the content of the document as
XML text using the Character Large Object datatype;
keeps integrity
Structured (object-relational) - decomposes or shreds
the content of the XML document and stores it as a
set of SQL objects
Semi-structured – portion of data is unstructured and
structured
Oracle XML DB
Oracle XML DB
Name for a set of Oracle Database technologies related to XML
storage and retrieval
Features
W3C XML and XML Schema data models and standard access
methods for XML
Ability to store, query, update, transform XML through SQL
Ability to perform XML operations on SQL data
Storage, content and language independent infrastructure for
storing XML
Industry-standard ways to access and update XML
APIs provide programmatic access and manipulation of XML
content using Java, C, and PL/SQL
Oracle XML DB
Oracle XML DB
Expanded Model
HTTPS
XML
Other Functions
XML DB
XML/
Rel View
Applications
ERP
Data
Request
XML
DSS/BI
Customer
Reports
Steps
1- XML schema
2- DML Commands
3- Triggers
4- Querying
5- Relational views
6- .NET
Company Database
Table
Fields
EMPLOYEE
fname, minit, lname, ssn, bdate, sex, salary, superssn, dno
DEPARTMENT
dname, dnumber, mgrssn, mgrstartdate
PROJECT
pname, pnumber, plocation, dnum
WORKS_ON
essn, pno, hours
DEPENDENT
essn, dependentname, sex, bdate, relationship
XML Document
XML Schema
Annotations - Allows influence of the objects and tables during
schema registration
GENTYPES or GENTABLES
defaultTable
Used to specify the name of the SQL attribute that corresponds to each
element or attribute in the XML schema
SQLType
Used to control the name of the default table generated for each global
element
SQLName
Ensure names of the tables, objects, and attributes created by
registerSchema are well-known/compliant
Used to specify the name of the SQL object type corresponding to the
definitions
storeVarrayAsTable
Used to force all collections to be stored as nested tables
XML Schema
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xdb="http://xmlns.oracle.com/xdb"
version="1.0" xdb:storeVarrayAsTable="true">
<xsd:element name="EmployeeTable" type="EmployeeType" xdb:defaultTable="EMPLOYEE"/>
<xsd:complexType name="EmployeeType" xdb:SQLType="EMPLOYEE_T">
<xsd:sequence>
<xsd:element name="FNAME" type="FNameType" xdb:SQLName="FNAME"></xsd:element>
<xsd:element name="MINIT" type="MInitType" xdb:SQLName="MINIT"></xsd:element>
<xsd:element name="LNAME" type="LNameType" xdb:SQLName="LNAME"></xsd:element>
<xsd:element name="SSN" type="SSNType" xdb:SQLName="SSN"></xsd:element>
<xsd:element name="BDATE" type="BDateType" xdb:SQLName="BDATE"></xsd:element>
<xsd:element name="ADDRESS" type="AddressType" xdb:SQLName="ADDRESS"></xsd:element>
<xsd:element name="SEX" type="SexType" xdb:SQLName="SEX"></xsd:element>
<xsd:element name="SALARY" type="SalaryType" xdb:SQLName="SALARY"></xsd:element>
<xsd:element name="SUPERSSN" type="SuperSSNType"
xdb:SQLName="SUPERSSN"></xsd:element>
<xsd:element name="DNO" type="DNOType" xdb:SQLName="DNO"></xsd:element>
</xsd:sequence>
</xsd:complexType>
…
</xsd:schema>
XML Schema
<xsd:simpleType name="FNameType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
…
<xsd:simpleType name="SSNType">
<xsd:restriction base="xsd:string">
<xsd:length value="9">
</xsd:length>
</xsd:restriction>
</xsd:simpleType>
…
</xsd:simpleType>
<xsd:simpleType name="SalaryType">
<xsd:restriction base="xsd:decimal">
<xsd:fractionDigits value="2"/>
<xsd:totalDigits value="10"/>
</xsd:restriction>
</xsd:simpleType>
XML Schema
Register XML Schema
BEGIN
DBMS_XMLSCHEMA.registerURI(
'http://localhost/public/Employee.xsd',
'http://localhost/public/Employee.xsd',
LOCAL=>TRUE,
GENTYPES=>TRUE, GENTABLES=>TRUE);
END;
/
Register XML Schema
XML Programming Languages
SQL/XML
SQLXML
Microsoft
Proprietary to SQL Server
XQuery
ANSI/ISO
SQL-centric
World Wide Web Consortium
XML-centric
XPath
XQuery expression
XML only
DML Commands
INSERT INTO EMPLOYEE VALUES(XMLType('<EmployeeTable>
<FNAME>John</FNAME>
<MINIT>B</MINIT>
<LNAME>Smith</LNAME>
<SSN>123456789</SSN>
<BDATE>1965-01-09</BDATE>
<ADDRESS>731 Fondren, Houston, TX</ADDRESS>
<SEX>M</SEX>
<SALARY>30000</SALARY>
<SUPERSSN>333445555</SUPERSSN>
<DNO>5</DNO>
</EmployeeTable>').createSchemaBasedXML(
'http://localhost/public/Employee.xsd'));
DML Commands
Triggers
CREATE OR REPLACE TRIGGER Employee_Trigger
BEFORE INSERT OR UPDATE ON EMPLOYEE
FOR EACH ROW
BEGIN
IF (:new.OBJECT_VALUE IS NOT NULL) THEN
:new.OBJECT_VALUE.schemavalidate();
END IF;
END;
/
Triggers
INSERT INTO EMPLOYEE VALUES(XMLType('<EmployeeTable>
<FNAME>John</FNAME>
<MINIT>B</MINIT>
<LNAME>Smith</LNAME>
<SSN>12345678900000</SSN>
<BDATE>1965-01-09</BDATE>
<ADDRESS>731 Fondren, Houston, TX</ADDRESS>
<SEX>M</SEX>
<SALARY>30000</SALARY>
<SUPERSSN>333445555</SUPERSSN>
<DNO>5</DNO>
</EmployeeTable>').createSchemaBasedXML(
'http://localhost/public/Employee.xsd'));
Triggers
Triggers
<xsd:element name="SSN" type="SSNType"
xdb:SQLName="SSN">
</xsd:element>
<xsd:simpleType name="SSNType">
<xsd:restriction base="xsd:string">
<xsd:length value="9">
</xsd:length>
</xsd:restriction>
</xsd:simpleType>
SQL/XML
GetName
SQL
SELECT
FNAME || ‘ ‘ || MINIT || ‘ ‘ || LNAME AS FULLNAME
FROM
EMPLOYEE
WHERE
SSN LIKE ‘123456789’;
SQL/XML
SELECT
EXTRACTVALUE(OBJECT_VALUE, '/EmployeeTable/FNAME')
|| ‘ ‘ ||
EXTRACTVALUE(OBJECT_VALUE, '/EmployeeTable/MINIT')
|| ‘ ‘ ||
EXTRACTVALUE(OBJECT_VALUE, '/EmployeeTable/LNAME')
AS FULLNAME
FROM
EMPLOYEE
WHERE
EXTRACTVALUE(OBJECT_VALUE , '/EmployeeTable/SSN')
LIKE '123456789';
SQL/XML
XQuery
select xtab.column_value
from employee, XMLTable('for $i in
/EmployeeTable
where $i/SSN eq "123456789"
return <Emp >
{$i/LNAME} {$i/MINIT} {$i/FNAME}
</Emp>'
passing object_value) xtab;
XQuery
Relational Views
CREATE OR REPLACE VIEW EMPLOYEEVIEW AS
SELECT
extractValue(OBJECT_VALUE, '/EmployeeTable/FNAME’) FNAME,
extractValue(OBJECT_VALUE, '/EmployeeTable/MINIT’) MINIT,
extractValue(OBJECT_VALUE, '/EmployeeTable/LNAME’) LNAME,
extractValue(OBJECT_VALUE, '/EmployeeTable/SSN’) SSN,
extractValue(OBJECT_VALUE, '/EmployeeTable/BDATE’) BDATE,
extractValue(OBJECT_VALUE, '/EmployeeTable/ADDRESS’) ADDRESS,
extractValue(OBJECT_VALUE, '/EmployeeTable/SEX’) SEX,
extractValue(OBJECT_VALUE, '/EmployeeTable/SALARY’) SALARY,
extractValue(OBJECT_VALUE, '/EmployeeTable/SUPERSSN’)
SUPERSSN,
extractValue(OBJECT_VALUE, '/EmployeeTable/DNO’) DNO
FROM
EMPLOYEE;
Relational Views
Relational Views
SELECT
FNAME || ‘ ‘ || MINIT || ‘ ‘ || LNAME
AS FULLNAME
FROM
EMPLOYEEVIEW
WHERE
SSN LIKE '123456789';
Relational Views
Relational Data
XQuery Relational Data
select xmlquery('
for $i in ora:view("EMPLOYEEVIEW")
where $i/ROW/FNAME = "John"
return $i'
RETURNING CONTENT)
from dual;
XQuery Relational Data
SQL/XML Relational Data
SELECT
XMLELEMENT(“SSN”, SSN) AS SSN,
XMLELEMENT (“FULLNAME", FNAME ||'
'|| MINIT ||’ ‘ || LNAME) AS FULLNAME
FROM employeeview
WHERE SSN = 123456789
SQL/XML Relational Data
.NET
GetName
Private Sub btnGetNameXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnGetNameXML.Click
Dim oradb As String = "Data Source=orcl;User Id=system;Password=orcl;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim sql As String = "SELECT EXTRACTVALUE(OBJECT_VALUE, '/EmployeeTable/FNAME') || ' ' ||" & _
" EXTRACTVALUE(OBJECT_VALUE, '/EmployeeTable/MINIT') || ' ' ||" & _
" EXTRACTVALUE(OBJECT_VALUE, '/EmployeeTable/LNAME') AS FULLNAME " & _
" FROM EMPLOYEE" & _
" WHERE EXTRACTVALUE(OBJECT_VALUE , '/EmployeeTable/SSN') LIKE " &
Me.txtSSN.Text
Dim cmd As New OracleCommand(sql, conn)
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
dr.Read()
Me.txtName.Text = dr.Item(0)
getXML()
End Sub
GetXML
Private Sub getXML()
Dim odb As String = "Data Source=orcl;User Id=system;Password=orcl;"
Dim conn As New OracleConnection(odb)
conn.Open()
Dim sql As String = "SELECT Value(x) “ & _
“ FROM EMPLOYEE x WHERE” & _
“ x.XMLDATA.SSN = 123456789"
Dim cmd As New OracleCommand(sql, conn)
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
dr.Read()
Me.txtXML.Text = dr.Item(0)
End Sub
.NET
References
http://www.oracle.com/technology/tech/xml/xmldb/index.
html
http://www.altova.com
http://www.w3.org/XML/
http://www.sqlx.org
http://www-128.ibm.com/developerworks/xml/
http://www.w3schools.com
Yen, Huang. The impact and implementation of XML on business-tobusiness commerce. 2002. Computer Standards & Interfaces 24.
Elmasri, Navathe. Fundamentals of Database Systems. 2004.
Addison/Wesley Pub Co. Fourth Edition, California.