1 Lecture 12: XQuery in SQL Server Monday, October 23, 2006.

Slides:



Advertisements
Similar presentations
Querying on the Web: XQuery, RDQL, SparQL Semantic Web - Spring 2006 Computer Engineering Department Sharif University of Technology.
Advertisements

Web Data Management XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections.
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 Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer.
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.
1 XQuery Web and Database Management System. 2 XQuery XQuery is to XML what SQL is to database tables XQuery is designed to query XML data What is XQuery?
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 9: XQuery. 2 XQuery Motivation XPath expressivity insufficient –no join queries (as in SQL) –no changes to the XML structure possible –no quantifiers.
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.
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.
Computing & Information Sciences Kansas State University Friday, 20 Oct 2006CIS 560: Database System Concepts Lecture 24 of 42 Friday, 20 October 2006.
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.
1 CSE544 XML, XQuery Wednesday, April 5, Announcements Project Ideas are posted (to discuss in class) Groups due today Proposals due Monday Two.
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.
1 Lecture 5: Relational Algebra and XML Monday, April 26th, 2004.
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.
End of XQuery DBMS Internals
Database Application Development
Querying XML and Semistructured Data
Lecture 15: Midterm Review
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 12: XQuery in SQL Server
Introduction to Database Systems CSE 444 Lecture 12 Xquery in SQL Server October 22, 2007.
Processing and Querying XML
XML? What’s this doing in my database? Adam Koehler
Presentation transcript:

1 Lecture 12: XQuery in SQL Server Monday, October 23, 2006

2 Announcements Homework 2 due on Wednesday Midterm on Friday. To study: –SQL –E/R diagrams –Functional dependencies and BCNF Project phase 2 due next Wednesday

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

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

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

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

7 Duplicate Elimination distinct-values(list-of-text-values) How do we eliminate duplicate “tuples” ?

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

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

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

11 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...) RETURN { $b/author } Returns:...

12 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 !!

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

14 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

15 CREATE TABLE DOCS ( ID int primary key, XDOC xml) SELECT ID, XDOC.query(’ for $s in “ ”]//SECTION return {data($s/TITLE)} ') FROM DOCS SELECT ID, XDOC.query(’ for $s in “ ”]//SECTION return {data($s/TITLE)} ') FROM DOCS

16 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

17 Examples From here: p?url=/library/en- us/dnsql90/html/sql2k5xml.asp p?url=/library/en- us/dnsql90/html/sql2k5xml.asp

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

19 Inserting an XML Value INSERT INTO docs VALUES (2, ' XML Schema Benefits Features ') INSERT INTO docs VALUES (2, ' XML Schema Benefits Features ')

20 Query( ) SELECT pk, = 123]//section') FROM docs SELECT pk, = 123]//section') FROM docs

21 Exists( ) SELECT = 123]//section') FROM docs WHERE xCol.exist = 123]') = 1 SELECT = 123]//section') FROM docs WHERE xCol.exist = 123]') = 1

22 Value( ) SELECT xCol.value( = 3]/title)[1])', 'nvarchar(max)') FROM docs SELECT xCol.value( = 3]/title)[1])', 'nvarchar(max)') FROM docs

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

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

25 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

26 Bad Bugs Nobody loves bad bugs. Tree Frogs All right-thinking people love tree frogs.

27

28 Infoset Table

29 = “ ”]/SECTION SELECT SerializeXML (N2.ID, N2.ORDPATH) FROM infosettab N1 JOIN infosettab N2 ON (N1.ID = N2.ID) WHERE N1.PATH_ID = AND N1.VALUE = ' ' AND N2.PATH_ID = PATH_ID(BOOK/SECTION) AND Parent (N1.ORDPATH) = Parent (N2.ORDPATH) SELECT SerializeXML (N2.ID, N2.ORDPATH) FROM infosettab N1 JOIN infosettab N2 ON (N1.ID = N2.ID) WHERE N1.PATH_ID = AND N1.VALUE = ' ' AND N2.PATH_ID = PATH_ID(BOOK/SECTION) AND Parent (N1.ORDPATH) = Parent (N2.ORDPATH)