Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review “Query Languages” Algebra, Calculus, and SQL.

Similar presentations


Presentation on theme: "Review “Query Languages” Algebra, Calculus, and SQL."— Presentation transcript:

1 Review “Query Languages” Algebra, Calculus, and SQL

2 2 Review - Objectives u Why learn about formal query languages (QLs)? u How to write a (general) algebra expression u How to write a (general) calculus expression u Differences between TRC and DRC u How to write easy SQL queries u How to write complex SQL queries

3 3 Why Formal QLs? u Relational Calculus enables you to write complex SQL queries that are difficult to write “on the fly” –Anyone can write simple SQL u Relational Algebra enables you to write more efficient SQL queries –You help the ‘optimizer’ subsystem u Formal theory means RDBMS has strong foundations –Understanding the foundation makes you a better DBMS specialist

4 4 How to write general RA expressions u Steps to take: 1.Identify which relations (tables) are needed 2.Identify which join operations (if any) are needed »Or identify if a set operation is necessary instead 3.Identify which extra (non-join) conditions are necessary, and where to place those selections 4.Identify which attributes should be in the result, and thus projected.

5 5 General structure of simple RA expressions u Then see whether some selection conditions can be moved ‘inwards’, closer to the relation –Make sure condition ranges over attributes that are in scope u Then see whether some attribute projections can be moved ‘inwards’, closer to the relation –Make sure conditions are still valid (attributes must still be in scope of the selection). Π list of attributes (σ conditions (T1 jop T2 jop T3 …)) (Where jop is a join operation such as natural join, …)

6 6 How to write simple RC expressions u Steps to take: 1.Identify which relations (tables) are needed, placing them in the [Table Range] part of the [Declarations Part] see next slide 2.Identify which join conditions (if any) are needed in the [Condition Part] 3.Identify which extra (non-join) conditions are necessary in the [Condition Part] 4.Identify which attributes should be in the [Result Part], and thus which variables are free. 5.Identify which other variables are used in the Condition Part, and bind these variables in the [Quantify Part] of the [Declarations Part]

7 7 General structure of simple RC expressions { var-list |  var-list (Table-plus-var)  list-of-conditions } [Result Part] [Quantify Part] [Table Range] [Condition Part] [Declarations Part]

8 8 Differences between TRC and DRC u Differences are minor u ‘Type’ of variables is different –Thus, vars have different values: tuples or attribs u DRC: –More variables needed, and declared –Join and constant conditions can be written ‘directly’ within relations »Eg: Hotel(hotNo, ‘Grosvenor’, cit)  Room(romNo, hotNo, typ, pric) u TRC: –Fewer variables needed, and declared –Need to explicitly state join conditions and comparisons between an attribute and a constant »Eg: Hotel(H)  Room(R )  H.hotNo = R.hotNo  H.hotNa = ‘Grosvenor’

9 9 How to write easy SQL expressions u Generally possible to write ‘on the fly’ –No need to try RC first u Steps: 1.Which tables needed  put in FROM clause 2.Which joins needed  put in FROM or WHERE 3.Which conditions needed  put in WHERE 4.Which attribs in result  put in SELECT 5.Grouping?  check rules for SELECT and WHERE 6.Sorting? 7.Set operations between such easy SQL statements necessary?

10 10 How to write complex SQL expressions u Check English query statement, look for words: –All combinations, exactly, at most, at least, … ? –Not necessarily complex! But good hint… u Write Relational Calculus expression that logically answers the question –‘Complex’ will mean one or more sub-conditions quantified with  or  u Translate RC expression into SQL –Use of sub-queries with EXISTS or NOT EXISTS necessary to translate sub-conditions


Download ppt "Review “Query Languages” Algebra, Calculus, and SQL."

Similar presentations


Ads by Google