Lecture 12: XQuery in SQL Server

Slides:



Advertisements
Similar presentations
Web Data Management XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections.
Advertisements

XML May 3 rd, XQuery Based on Quilt (which is based on XML-QL) Check out the W3C web site for the latest. XML Query data model –Ordered !
XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
CSE 6331 © Leonidas Fegaras XML and Relational Databases 1 XML and Relational Databases Leonidas Fegaras.
XQUERY. What is XQuery? XQuery is the language for querying XML data The best way to explain XQuery is to say that XQuery is to XML what SQL is to database.
Database Management Systems, R. Ramakrishnan1 Introduction to Semistructured Data and XML Chapter 27, Part D Based on slides by Dan Suciu University of.
1 Lecture 6 XML/Xpath/XQuery Wednesday, November 3, 2010 Dan Suciu -- CSEP544 Fall 2010.
Introduction to XML, XPath, & XQuery CS186, Fall 2005 R &G - Chapters 7-27 Bill Gates, The Revolution, and a Network of Trees ( based on a true story)
1 Part 3: Query Languages Managing XML and Semistructured Data.
Querying XML (cont.). Comments on XPath? What’s good about it? What can’t it do that you want it to do? How does it compare, say, to SQL?
IS432: Semi-Structured Data Dr. Azeddine Chikh. 7. XQuery.
1 Lecture 12: XQuery in SQL Server Monday, October 23, 2006.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 357 Database Systems I Query Languages for XML.
XQuery language Presented by: Tayeb sbihi supervised by: Dr. H. Haddouti.
Query Languages - XQuery Slides partially from Dan Suciu.
XML May 1 st, XML for Representing Data John 3634 Sue 6343 Dick 6363 John 3634 Sue 6343 Dick 6363 row name phone “John”3634“Sue”“Dick” persons.
SDPL 2001Notes 8.2: XQuery1 8.2 W3C XML Query Language –Thanks for Helena Ahonen-Myka (University of Helsinki) for borrowing her slide originals for this.
Storing and Querying Ordered XML Using a Relational Database System By Khang Nguyen Based on the paper of Igor Tatarinov and Statis Viglas.
XML, XML Schema, Xpath and Xquery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
1 Lecture 4 XML/Xpath/XQuery Tuesday, January 30, 2007.
CS405G: Introduction to Database Systems Final Review.
XML, XML Schema, XPath and XQuery Query Languages CS561 Slides collated from several sources, including D. Suciu at Univ. of Washington.
Xpath to XQuery February 23rd, Other Stuff HW 3 is out. Instructions for Phase 3 are out. Today: finish Xpath, start and finish Xquery. From Wednesday:
Querying XML February 12 th, Querying XML Data XPath = simple navigation through the tree XQuery = the SQL of XML XSLT = recursive traversal –will.
Processing of structured documents Spring 2003, Part 8 Helena Ahonen-Myka.
XQuery Implementation in a Relational Database System Shankar Pal Istvan Cseri, Oliver Seeliger, Michael Rys, Gideon Schaller, Wei Yu, Dragan Tomic, Adrian.
Indexing XML Data Stored in a Relational Database VLDB`2004 Shankar Pal, Istvan Cseri, Gideon Schaller, Oliver Seeliger, Leo Giakoumakis, Vasili Vasili.
1 Lecture 12: XML Publishing, XML Storage Monday, October 24, 2005.
Deep Dive into XQuery and XML in Microsoft SQL Server: Common Problems and Best Practice Solutions Michael Rys Principal Program Manager Microsoft Corporation.
XQuery Your gateway to manipulating XML in SQL Server 2005.
2.2 SQL Server 2005 的 XML 支援功能. Overview XML Enhancements in SQL Server 2005 The xml Data Type Using XQuery.
Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:
Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations.
Introduction to XQuery Resources: Official URL: Short intros:
A Quilt, not a Camel Don Chamberlin Jonathan Robie Daniela Florescu May 19, 2000.
Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen.
End of XML February 19 th, FLWR (“Flower”) Expressions FOR... LET... WHERE... RETURN... FOR... LET... WHERE... RETURN...
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Module 18 Querying XML Data in SQL Server® 2008 R2.
5/2/20051 XML Data Management Yaw-Huei Chen Department of Computer Science and Information Engineering National Chiayi University.
PROCESSING AND QUERYING XML 1. ROADMAP Models for Parsing XML Documents XPath Language XQuery Language XML inside DBMSs 2.
XML query. introduction An XML document can represent almost anything, and users of an XML query language expect it to perform useful queries on whatever.
1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries.
XML May 6th, Instructor AnHai Doan Brief bio –high school in Vietnam & undergrad in Hungary –M.S. at Wisconsin –Ph.D. at Washington under Alon &
1 Lecture 13: XQuery XML Publishing, XML Storage Monday, October 28, 2002.
IS432 Semi-Structured Data Lecture 6: XQuery Dr. Gamal Al-Shorbagy.
19 th International Unicode Conference San Jose, CA September W3C XML Query Paul Cotton, Microsoft 19 th Unicode Conference Sept 12, 2001.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections and sorting 2.
Lecture 17: XPath and XQuery Wednesday, Nov. 7, 2001.
1 CSE544: Lecture 7 XQuery, Relational Algebra Monday, 4/22/02.
INLS 623– Database Systems II– File Structures, Indexing, and Hashing
End of XQuery DBMS Internals
Querying XML and Semistructured Data
Lecture 15: Midterm Review
CS405G: Introduction to Database Systems
Lecture 11 XML Wednesday, Oct. 24, 2001.
LINQ to DATABASE-2.
XML: Schemas, Queries Wednesday, 4/17/2002
Lecture 12: XML, XPath, XQuery
Introduction to Database Systems CSE 444 Lecture 12 More Xquery and Xquery in SQL Server April 25, 2008.
Wednesday, May 29, 2002 XML Storage Final Review
XQuery Leonidas Fegaras.
Lecture 24: Final Review Friday, March 10, 2006.
Lecture 30: Final Review Wednesday, December 10, 2003.
Introduction to Database Systems CSE 444 Lecture 12 Xquery in SQL Server October 22, 2007.
Processing and Querying XML
Lecture 13: XQuery XML Publishing, XML Storage
XML, XML Schema, XPath and XQuery Query Languages
Presentation transcript:

Lecture 12: XQuery in SQL Server Friday, April 20, 2007

Announcements Homework 2: deadline extended until tonight, at 11:59pm Midterm on Wednesday. To study: SQL E/R diagrams Functional dependencies and BCNF Open book exam !! Project phase 2 due next Friday

Sorting in XQuery <publisher_list> { FOR $b IN document("bib.xml")//book[year = “97”] ORDER BY $b/price/text() RETURN <book> { $b/title , $b/price } </book> </publisher_list>

If-Then-Else FOR $h IN //holding RETURN <holding> { $h/title, IF $h/@type = "Journal" THEN $h/editor ELSE $h/author } </holding>

Existential Quantifiers FOR $b IN //book WHERE SOME $p IN $b//para SATISFIES contains($p, "sailing") AND contains($p, "windsurfing") RETURN { $b/title }

Universal Quantifiers FOR $b IN //book WHERE EVERY $p IN $b//para SATISFIES contains($p, "sailing") RETURN { $b/title }

Duplicate Elimination distinct-values(list-of-text-values) How do we eliminate duplicate “tuples” ? <row> <a>3</a> <b>100</b> </row> <row> <a>8</a> <b>500</b> </row> <row> <a>3</a> <b>200</b> </row> <row> <a>3</a> <b>100</b> </row> <row> <a>8</a> <b>500</b> </row> <row> <a>3</a> <b>200</b> </row>

FOR v.s. LET FOR Binds node variables  iteration LET Binds collection variables  one value

FOR v.s. LET FOR $x IN /bib/book Returns: <result> <book>...</book></result> ... <result> <book>...</book> <book>...</book> </result> FOR $x IN /bib/book RETURN <result> { $x } </result> LET $x := /bib/book RETURN <result> { $x } </result>

XQuery Summary: FOR-LET-WHERE-RETURN = FLWR FOR/LET Clauses List of tuples WHERE Clause List of tuples RETURN Clause Instance of Xquery data model

Collections in XQuery Ordered and unordered collections /bib/book/author/text() = an ordered collection: result is in document order distinct-values(/bib/book/author/text()) = an unordered collection: the output order is implementation dependent LET $a := /bib/book  $a is a collection $b/author  a collection (several authors...) Returns: <result> <author>...</author> <author>...</author> ... </result> RETURN <result> { $b/author } </result>

Collections in XQuery What about collections in expressions ? $b/price  list of n prices $b/price * 0.7  list of n numbers $b/price * $b/quantity  list of n x m numbers ?? $b/price * ($b/quant1 + $b/quant2)  $b/price * $b/quant1 + $b/price * $b/quant2 !!

Other XML Topics Name spaces XML API: XML languages: XML Schema DOM = “Document Object Model” XML languages: XSLT XML Schema Xlink, XPointer SOAP Available from www.w3.org (but don’t spend rest of your life reading those standards !)

XML in SQL Server 2005 Create tables with attributes of type XML Use Xquery in SQL queries Rest of the slides are from: Shankar Pal et al., Indexing XML data stored in a relational database, VLDB’2004

CREATE TABLE DOCS ( ID int primary key, XDOC xml) SELECT ID, XDOC.query(’ for $s in /BOOK[@ISBN= “1-55860-438-3”]//SECTION return <topic>{data($s/TITLE)} </topic>') FROM DOCS

XML Methods in SQL Query() = returns XML data type Value() = extracts scalar values Exist() = checks conditions on XML nodes Nodes() = returns a rowset of XML nodes that the Xquery expression evaluates to

Examples From here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/html/sql2k5xml.asp

XML Type CREATE TABLE docs ( pk INT PRIMARY KEY, xCol XML not null )

Inserting an XML Value INSERT INTO docs VALUES (2, '<doc id="123"> <sections> <section num="1"><title>XML Schema</title></section> <section num="3"><title>Benefits</title></section> <section num="4"><title>Features</title></section> </sections> </doc>')

Query( ) SELECT pk, xCol.query('/doc[@id = 123]//section') FROM docs

Exists( ) SELECT xCol.query('/doc[@id = 123]//section') FROM docs WHERE xCol.exist ('/doc[@id = 123]') = 1

Value( ) SELECT xCol.value( 'data((/doc//section[@num = 3]/title)[1])', 'nvarchar(max)') FROM docs

Nodes( ) SELECT nref.value('first-name[1]', 'nvarchar(50)') AS FirstName, nref.value('last-name[1]', 'nvarchar(50)') AS LastName FROM @xVar.nodes('//author') AS R(nref) WHERE nref.exist('.[first-name != "David"]') = 1

Nodes( ) SELECT nref.value('@genre', 'varchar(max)') LastName FROM docs CROSS APPLY xCol.nodes('//book') AS R(nref)

Internal Storage XML is “shredded” as a table A few important ideas: Dewey decimal numbering of nodes; store in clustered B-tree indes Use only odd numbers to allow insertions Reverse PATH-ID encoding, for efficient processing of postfix expressions like //a/b/c Add more indexes, e.g. on data values

<BOOK ISBN=“1-55860-438-3”> <SECTION> <TITLE>Bad Bugs</TITLE> Nobody loves bad bugs. <FIGURE CAPTION=“Sample bug”/> </SECTION> <TITLE>Tree Frogs</TITLE> All right-thinking people <BOLD> love </BOLD> tree frogs. </BOOK>

Infoset Table

/BOOK[@ISBN = “1-55860-438-3”]/SECTION SELECT SerializeXML (N2.ID, N2.ORDPATH) FROM infosettab N1 JOIN infosettab N2 ON (N1.ID = N2.ID) WHERE N1.PATH_ID = PATH_ID(/BOOK/@ISBN) AND N1.VALUE = '1-55860-438-3' AND N2.PATH_ID = PATH_ID(BOOK/SECTION) AND Parent (N1.ORDPATH) = Parent (N2.ORDPATH)