1 CSE544: Lecture 7 XQuery, Relational Algebra Monday, 4/22/02.

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 !
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
Lecture 07: Relational Algebra
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
1 Relational Algebra Lecture #9. 2 Querying the Database Goal: specify what we want from our database Find all the employees who earn more than $50,000.
Relational Algebra Maybe -- SQL. Confused by Normal Forms ? 3NF BCNF 4NF If a database doesn’t violate 4NF (BCNF) then it doesn’t violate BCNF (3NF) !
Database Management Systems, R. Ramakrishnan1 Introduction to Semistructured Data and XML Chapter 27, Part D Based on slides by Dan Suciu University of.
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?
1 Lecture 12: XQuery in SQL Server Monday, October 23, 2006.
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.
1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008.
1 Lecture 11: Xpath/XQuery Friday, October 20, 2006.
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.
XML, XML Schema, Xpath and Xquery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
1 Lecture 07: Relational Algebra. 2 Outline Relational Algebra (Section 6.1)
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:
1 Lecture 16: Querying XML Data: XPath, XQuery Friday, February 11, 2005.
Querying XML February 12 th, Querying XML Data XPath = simple navigation through the tree XQuery = the SQL of XML XSLT = recursive traversal –will.
Relational Schema Design (end) Relational Algebra Finally, querying the database!
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Lecture 3: Relational Algebra and SQL Tuesday, March 25, 2008.
Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:
Introduction to XQuery Resources: Official URL: Short intros:
1 XQuery Slides From Dr. Suciu. 2 FLWR (“Flower”) Expressions FOR... LET... WHERE... RETURN... FOR... LET... WHERE... RETURN...
XML by Dan Suciu 1 Introduction to Semistructured Data and XML Based on slides by Dan Suciu University of Washington.
End of XML February 19 th, FLWR (“Flower”) Expressions FOR... LET... WHERE... RETURN... FOR... LET... WHERE... RETURN...
1 Introduction to Database Systems CSE 444 Lecture 20: Query Execution: Relational Algebra May 21, 2008.
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.
Transactions, Relational Algebra, XML February 11 th, 2004.
CSE 544: Relational Operators, Sorting Wednesday, 5/12/2004.
1 Lecture 7: Normal Forms, Relational Algebra Monday, 10/15/2001.
Relational Algebra 2. Relational Algebra Formalism for creating new relations from existing ones Its place in the big picture: Declartive query language.
1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries.
Lecture 13: Relational Decomposition and Relational Algebra February 5 th, 2003.
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.
1 Lecture 10: Database Design and Relational Algebra Monday, October 20, 2003.
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 Lecture 12: XML, XPath, XQuery Friday, October 24, 2003.
Relational Algebra.
COP4710 Database Systems Relational Algebra.
Relational Algebra at a Glance
Lecture 8: Relational Algebra
Lecture 11: Xpath/XQuery
End of XQuery DBMS Internals
Querying XML and Semistructured Data
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.
Lecture 33: The Relational Model 2
Relational Algebra Friday, 11/14/2003.
Lecture 3: Relational Algebra and SQL
Relational Schema Design (end) Relational Algebra SQL (maybe)
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
Lecture 11: Functional Dependencies
Presentation transcript:

1 CSE544: Lecture 7 XQuery, Relational Algebra Monday, 4/22/02

2 XQuery Based on Quilt (which is based on XML-QL) XML Query data model –Similar to the XPath data model, more complete

3 FLWR (“Flower”) Expressions FOR... LET... FOR... LET... WHERE... RETURN... FOR... LET... FOR... LET... WHERE... RETURN...

4 XQuery Find all book titles published after 1995: FOR $x IN document("bib.xml") /bib/book WHERE $x/year > 1995 RETURN { $x/title } FOR $x IN document("bib.xml") /bib/book WHERE $x/year > 1995 RETURN { $x/title } Result: abc def ghi

5 XQuery For each author of a book by Morgan Kaufmann, list all books she published: FOR $a IN distinct( document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN { $a, FOR $t IN /bib/book[author=$a]/title RETURN $t } FOR $a IN distinct( document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN { $a, FOR $t IN /bib/book[author=$a]/title RETURN $t } distinct = a function that eliminates duplicates

6 XQuery Jones abc def Smith ghi Jones abc def Smith ghi Result:

7 XQuery FOR $x in expr -- binds $x to each value in the list expr LET $x = expr -- binds $x to the entire list expr –Useful for common subexpressions and for aggregations

8 XQuery count = a (aggregate) function that returns the number of elms FOR $p IN distinct(document("bib.xml")//publisher) LET $b := document("bib.xml")/book[publisher = $p] WHERE count($b) > 100 RETURN { $p } FOR $p IN distinct(document("bib.xml")//publisher) LET $b := document("bib.xml")/book[publisher = $p] WHERE count($b) > 100 RETURN { $p }

9 XQuery Find books whose price is larger than average: LET $a=avg( document("bib.xml") /bib/book/price) FOR $b in document("bib.xml") /bib/book WHERE $b/price > $a RETURN { $b } LET $a=avg( document("bib.xml") /bib/book/price) FOR $b in document("bib.xml") /bib/book WHERE $b/price > $a RETURN { $b }

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

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

12 FOR v.s. LET FOR $x IN document("bib.xml") /bib/book RETURN { $x } FOR $x IN document("bib.xml") /bib/book RETURN { $x } Returns:... LET $x IN document("bib.xml") /bib/book RETURN { $x } LET $x IN document("bib.xml") /bib/book RETURN { $x } Returns:...

13 Collections in XQuery Ordered and unordered collections –/bib/book/author = an ordered collection –distinct(/bib/book/author) = an unordered collection LET $a = /bib/book  $a is a collection $b/author  a collection (several authors...) RETURN { $b/author } Returns:...

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

15 Sorting in XQuery FOR $p IN distinct(document("bib.xml")//publisher) RETURN { $p/text() }, FOR $b IN document("bib.xml")//book[publisher = $p] RETURN { $b/title, $b/price } SORTBY(price DESCENDING) SORTBY(name) FOR $p IN distinct(document("bib.xml")//publisher) RETURN { $p/text() }, FOR $b IN document("bib.xml")//book[publisher = $p] RETURN { $b/title, $b/price } SORTBY(price DESCENDING) SORTBY(name)

16 Sorting in XQuery Sorting arguments: refer to the name space of the RETURN clause, not the FOR clause

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

18 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 }

19 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 }

20 Other Stuff in XQuery BEFORE and AFTER –for dealing with order in the input FILTER –deletes some edges in the result tree Recursive functions –Currently: arbitrary recursion –Perhaps more restrictions in the future ?

21 Foundations of Database Systems Why is theory important ? Roadmap to database theory in CSE544 –Relational algebra (today) –First order logic (a.k.a. relational calculus) –Conjunctive queries and datalog

22 Relational Algebra at a Glance Algebra on relations –Set algebra: e.g. the boolean algebra –Algebra on relations: e.g. Tarski’s cylindrical algebra Five basic RA operators: –Union, difference (from the boolean algebra): , - –Selection:  –Projection:  –Cartesian Product:  Derived operators: intersection, complement, joins Renaming: 

23 Union Union: all tuples in R1 or R2 Notation: R1  R2 R1, R2 must have the same schema R1  R2 has the same schema as R1, R2 Example: –ActiveEmployees  RetiredEmployees

24 Difference Difference: all tuples in R1 and not in R2 Notation: R1 – R2 R1, R2 must have the same schema R1 – R2 has the same schema as R1, R2 Example –AllEmployees – RetiredEmployees

25 Intersection Difference: all tuples both in R1 and in R2 Notation: R1  R2 R1, R2 must have the same schema R1  R2 has the same schema as R1, R2 Example –UnionizedEmployees  RetiredEmployees Derived operation: – R1  R2 = R1 – (R1 – R2)

26 Selection Returns all tuples which satisfy a condition Notation:  c (R) c is a condition: =,, and, or, not Output schema: same as input schema Find all employees with salary more than $40,000: –  Salary > (Employee)

27 Find all employees with salary more than $40,000.  Salary > (Employee)

28 Projection Unary operation: returns certain columns Eliminates duplicate tuples ! Notation:  A1,…,An (R) Input schema R(B1,…,Bm) Condition: {A1, …, An}  {B1, …, Bm} Output schema S(A1,…,An) Example: project social-security number and names: –  SSN, Name (Employee)

29  SSN, Name (Employee)

30 Cartesian Product Each tuple in R1 with each tuple in R2 Notation: R1  R2 Input schemas R1(A1,…,An), R2(B1,…,Bm) Condition: {A1,…,An}  {B1,…Bm} =  Output schema is S(A1, …, An, B1, …, Bm) Notation: R1  R2 Example: Employee  Dependents Very rare in practice; but joins are very often

31

32 Renaming Does not change the relational instance Changes the relational schema only Notation:  B1,…,Bn (R) Input schema: R(A1, …, An) Output schema: S(B1, …, Bn) Example:  LastName, SocSocNo (Employee)

33 Renaming Example Employee NameSSN John Tony LastNameSocSocNo John Tony  LastName, SocSocNo (Employee)

34 Natural Join Notation: R1 R2 Input Schema: R1(A1, …, An), R2(B1, …, Bm) Output Schema: S(C1,…,Cp) –Where {C1, …, Cp} = {A1, …, An}  {B1, …, Bm} Meaning: combine all pairs of tuples in R1 and R2 that agree on the attributes: –{A1,…,An}  {B1,…, Bm} (called the join attributes) Equivalent to a cross product followed by selection Example Employee Dependents

35 Natural Join Example Employee NameSSN John Tony Dependents SSNDname Emily Joe NameSSNDname John Emily Tony Joe Employee Dependents =  Name, SSN, Dname (  SSN=SSN2 (Employee   SSN2, Dname (Dependents))

36 Natural Join R= S= R S= AB XY XZ YZ ZV BC ZU VW ZV ABC XZU XZV YZU YZV ZVW

37 Natural Join Given the schemas R(A, B, C, D), S(A, C, E), what is the schema of R S ? Given R(A, B, C), S(D, E), what is R S ? Given R(A, B), S(A, B), what is R S ?

38 Theta Join A join that involves a predicate Notation: R1  R2 where  is a condition Input schemas: R1(A1,…,An), R2(B1,…,Bm) {A1,…An}  {B1,…,Bm} =  Output schema: S(A1,…,An,B1,…,Bm) Derived operator: R1  R2 =   (R1 x R2)

39 Eq-join Most frequently used in practice: R1  R2 Natural join is a particular case of eqjoin A lot of research on how to do it efficiently

40 Semijoin R S =  A1,…,An (R S) Where the schemas are: –Input: R(A1,…An), S(B1,…,Bm) –Output: T(A1,…,An)

41 Semijoin Applications in distributed databases: Product(pid, cid, pname,...) at site 1 Company(cid, cname,...) at site 2 Query:  price>1000 (Product) cid=cid Company Compute as follows: T1 =  price>1000 (Product) site 1 T2 =  cid (T1) site 1 send T2 to site 2 (T2 smaller than T1) T3 = T2 Company site 2 (semijoin) send T3 to site 1 (T3 smaller than Company) Answer = T3 T1 site 1 (semijoin)

42 Relational Algebra Summary Five basic operators, many derived Combine operators in order to construct queries: relational algebra expressions, usually shown as trees