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

Slides:



Advertisements
Similar presentations
Relational Algebra Chapter 4, Part A
Advertisements

From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 22, 2005.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
1 Relational Calculus Chapter 4 – Part II. 2 Formal Relational Query Languages  Two mathematical Query Languages form the basis for “real” languages.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Relational Algebra Content based on Chapter 4 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Relational Calculus. Another Theoretical QL-Relational Calculus n Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus.
Relational Calculus CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth   We will occasionally use this arrow notation unless there is danger of.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Relational Calculus R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham Elements of Ramsey.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Calculus CS 186, Fall 2003, Lecture 6 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
Rutgers University Relational Calculus 198:541 Rutgers University.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Chapter 8 Relational Calculus. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-2 Topics in this Chapter Tuple Calculus Calculus vs. Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Relational Calculus R&G, Chapter 4. Relational Calculus Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus.
Relational Calculus CS 186, Spring 2005, Lecture 9 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
IST 210 The Relational Language Todd S. Bacastow January 2004.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Copyright © Curt Hill The Relational Calculus Another way to do queries.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
Relational Algebra p BIT DBMS II.
Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
1 Relational Algebra. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational model supports.
Relational Calculus Chapter 4, Section 4.3.
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Calculus.
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
Relational Calculus and QBE
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B 7/1/2019.
Relational Calculus Chapter 4, Part B
Presentation transcript:

Review “Query Languages” Algebra, Calculus, and SQL

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