XML Databases, SQL/XML, Sedna 5
Total Page:16
File Type:pdf, Size:1020Kb
MI-PDB, MIE-PDB: Advanced Database Systems http://www.ksi.mff.cuni.cz/~svoboda/courses/2015-2-MIE-PDB/ Lecture 7: XML Databases, SQL/XML, Sedna 5. 4. 2016 Lecturer: Martin Svoboda [email protected] Author: Martin Svoboda Faculty of Mathematics and Physics, Charles University in Prague Course NPRG036: XML Technologies Course NPRG039: Advanced Aspects and New Trends in XML Outline • XML databases . Introduction . SQL/XML ‒ Extension to SQL for XML data ‒ Used in XML-enabled (object-)relational database systems . Sedna ‒ Native XML database system MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 2 XML Databases Motivation • Why XML databases? . We have high volumes of data . We want to work with them efficiently ‒ Modeling, acquisition, creation, organization, evolution ‒ Retrieval, querying, processing, updates ‒ Administration, security, concurrency, recovery ‒ … MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 4 Documents vs. Databases • World of documents . Many small and usually static documents . Implicit structure given by tagging . Suitable for humans . Operations: editing, printing, retrieval, searching, … • World of databases . Several huge and usually dynamic databases . Explicit structure given by a schema . Suitable for machines . Operations: storing, querying, cleansing, updating, … MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 5 XML Data • Semi-structured data . Document-oriented ‒ Created and processed by humans ‒ Irregular and less structured ‒ Mixed contents, CDATA sections, … ‒ Important order of sibling elements . Hybrid . Data-oriented ‒ Created and processed by machines ‒ Regular and fully structured ‒ Unimportant order of sibling elements MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 6 Storage Strategies • General classification . File system . (Object-)relational database systems ‒ XML-enabled or not . Native XML database systems MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 7 Storage Strategies • (Object-)relational DBMS . BLOB/CLOB ‒ The highest level of round tripping ‒ No available XML-specific operations . Mapping techniques ‒ XML data shredded into relations . XML-enabled systems ‒ Extensions allowing to work with XML data ‒ XML datatype and SQL/XML MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 8 Storage Strategies • Native XML DBMS . Advantages ‒ Logical model – directly based on XML documents ‒ Query languages such as XPath, XQuery, … ‒ DOM/SAX interfaces ‒ … . Disadvantages ‒ Implementations from scratch MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 9 Current Database Systems • XML-enabled ORDBMS . Oracle DB . IBM DB2 . PostgreSQL . Microsoft SQL Server • Native XML DBMS . Sedna . BaseX . eXist-DB MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 10 SQL/XML Introduction • SQL/XML . Extension to SQL for XML data ‒ XML datatype ‒ Functions, constructors, mappings, XQuery embedding, … • Standards . SQL:2011-14 (ISO/IEC 9075-14:2011) ‒ Older versions 2003, 2006, 2008 MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 12 Sample XML Document <?xml version="1.0"?> <library> <book id="1" catalogue="c1" language="en"> <title>Red</title> <author>John</author> <author>Peter</author> </book> <book id="2" catalogue="c1"> <title>Black</title> <price>25</price> </book> <book id="3" catalogue="c2" language="en"> <title>White</title> <author>John</author> </book> </library> MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 13 Sample Relational Schema • Table: books id catalogue title details language <author>John</author> 1 c1 Red en <author>Peter</author> 2 c1 Black <price>25</price> NULL 3 c2 White <author>John</author> en • Table: languages code name en English cs Czech MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 14 Sample Query • Query statement SELECT id, XMLELEMENT( NAME "book", XMLELEMENT(NAME "title", title), details ) AS book FROM books WHERE (language = "en") ORDER BY title DESC MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 15 Sample Query • Query result id book <book> <title>White</title> 3 <author>John</author> </book> <book> <title>Red</title> 1 <author>John</author> <author>Peter</author> </book> MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 16 XML Datatype • Traditional types . BLOB, CLOB, VARCHAR, … • Native XML type . Collection of information items ‒ Based on XML Information Set (XML Infoset) • Elements, attributes, processing instructions, … • Fragments with more root elements are allowed as well . NULL MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 17 Parsing XML Values • XMLPARSE . Creates an XML value from a string ‒ DOCUMENT – well-formed document with right one root ‒ CONTENT – well-formed fragment SELECT XMLPARSE( DOCUMENT "<book><title>Red</title></book>" ) AS result result <book> <title>Red</title> </book> MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 18 Serializing XML Values • XMLSERIALIZE . Exports an XML value to a string SELECT id, title, XMLSERIALIZE(CONTENT details AS VARCHAR(100)) AS export FROM books id title export 1 Red <author>John</author><author>Peter</author> … … … MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 19 Well-Formedness Predicate • IS DOCUMENT . Tests whether an XML value is an XML document ‒ Returns TRUE if there is right one root element ‒ Otherwise FALSE MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 20 Constructors • Functions for construction of XML values… . XMLELEMENT – elements . XMLNAMESPACES – namespace declarations . XMLATTRIBUTES – attributes . XMLCOMMENT – comments . XMLPI – processing instructions . XMLFOREST – sequences of elements . XMLCONCAT – concatenations of values . XMLAGG – aggregates MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 21 Elements • XMLELEMENT . Creates an XML element with a given name and… ‒ optional namespace declarations ‒ optional attributes ‒ optional element content MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 22 Elements: Example 1 SELECT id, XMLELEMENT(NAME "book", title) AS result FROM books ORDER BY id id result 1 <book>Red</book> 2 <book>Black</book> 3 <book>White</book> MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 23 Elements: Example 2: Subelements SELECT id, XMLELEMENT( NAME "book", XMLELEMENT(NAME "title", title), XMLELEMENT(NAME "language", language) ) AS records FROM books id records <book> <title>Red</title> 1 <language>en<language> </book> … … MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 24 Elements: Example 3: Mixed Content SELECT id, XMLELEMENT( NAME "info", "Book ", XMLELEMENT(NAME "title", title), " with identifier ", id, "." ) AS description FROM books id description <info> 1 Book <title>Red</title> with identifier 1. </info> … … MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 25 Elements: Example 4: Subqueries SELECT id, XMLELEMENT(NAME "title", title) AS book, XMLELEMENT( NAME "language", (SELECT name FROM languages WHERE (code = language)) ) AS description FROM books id book description 1 <title>Red</title> <language>English</language> … … … MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 26 Attributes • XMLATTRIBUTES . Creates a set of attributes . Input: list of values ‒ Each value must have an explicit / implicit name • It is used as a name for the given attribute • Implicit names can be derived, e.g., from column names . Output: XML value with a set of attribute nodes MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 27 Attributes: Example SELECT id, XMLELEMENT(NAME "book", XMLATTRIBUTES( language, catalogue AS "location" ), XMLELEMENT(NAME "title", title) ) AS book FROM books id book <book language="en" location="c1"> 1 <title>Red</title> </book> … … MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 28 Element Sequences • XMLFOREST . Creates a sequence of XML elements ‒ … by encapsulating provided values into elements . Input: list of SQL values • Individual content expressions evaluated to NULL are ignored • If all the expressions are evaluated to NULL, then NULL is returned ‒ Each content value must have an explicit / implicit name • It is used as a name for the given element . Output: XML value with a sequence of element nodes MI-PDB, MIE-PDB: Advanced Database Systems | Lecture 7: XML Databases, SQL/XML, Sedna | 5. 4. 2016 29 Element Sequences: Example SELECT id, XMLFOREST( title, language, catalogue AS location ) AS book FROM books id book <title>Red</title> 1 <language>en</language> <location>c1</location> <title>Black</title>