Relational Query Languages

Similar documents
Relational Algebra and SQL. Basic Operations Algebra of Bags

Relational Algebra. Algebra of Bags

Relational Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Chapter 2 The relational Model of data. Relational algebra

1 Relational Data Model

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

Databases-1 Lecture-01. Introduction, Relational Algebra

Relational Query Languages: Relational Algebra. Juliana Freire

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

CS411 Database Systems. 04: Relational Algebra Ch 2.4, 5.1

Relational data model

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

The Relational Data Model. Data Model

They like one or more beers and frequent one. Bars have unique names and addresses. They. serve one or more beers and are frequented

MIS Database Systems Relational Algebra

The Relational Model of Data (ii)

The Relational Model. Chapter 3

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Review: Where have we been?

EECS 647: Introduction to Database Systems

Database Management Systems. Chapter 3 Part 1

Relational Model & Algebra. Announcements (Tue. Sep. 3) Relational data model. CompSci 316 Introduction to Database Systems

Database Applications (15-415)

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

SQL: The Query Language Part 1. Relational Query Languages

CS425 Midterm Exam Summer C 2012

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Relational Model & Algebra. Announcements (Thu. Aug. 27) Relational data model. CPS 116 Introduction to Database Systems

Chapter 6 The database Language SQL as a tutorial

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

Basic operators: selection, projection, cross product, union, difference,

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

The Relational Model

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

Relational Algebra. Chapter 4, Part A. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

The Relational Model

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes

Relational Model and Relational Algebra

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Introduction to Data Management. Lecture #4 E-R Model, Still Going

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Chapter 6 The database Language SQL as a tutorial

The Relational Model. Week 2

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

DBMS. Relational Model. Module Title?

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

What is an Algebra. Core Relational Algebra. What is Relational Algebra? Operação de Seleção. Álgebra Relacional: Resumo

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

Relational Algebra. Relational Query Languages

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

Introduction to Data Management. Lecture #11 (Relational Algebra)

Database Applications (15-415)

Relational Algebra 1. Week 4

I Relational Database Modeling how to define

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Relational Databases. Relational Databases. Extended Functional view of Information Manager. e.g. Schema & Example instance of student Relation

The Relational Model 2. Week 3

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Relational Algebra. ICOM 5016 Database Systems. Roadmap. R.A. Operators. Selection. Example: Selection. Relational Algebra. Fundamental Property

SQL: Data Manipulation Language

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

CS2300: File Structures and Introduction to Database Systems

Chapter 2: The Relational Algebra

Database Systems ( 資料庫系統 )

Relational Algebra 1

Chapter 2: The Relational Algebra

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

Relational Database: The Relational Data Model; Operations on Database Relations

I Relational Database Modeling how to define

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

CSCC43H: Introduction to Databases. Lecture 3

Relational Algebra 1

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

v Conceptual Design: ER model v Logical Design: ER to relational model v Querying and manipulating data

CompSci 516: Database Systems

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

CIS 330: Applied Database Systems

Database Applications (15-415)

Relational Algebra and Calculus. Relational Query Languages. Formal Relational Query Languages. Yanlei Diao UMass Amherst Feb 1, 2007

Lecture 16. The Relational Model

CSC 261/461 Database Systems Lecture 13. Fall 2017

Modeling access. Relational query languages. Queries. Selection σ P (R) Relational model: Relational algebra

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Transcription:

Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful, declarative QLs with precise semantics: Strong formal foundation based on logic. Allows for much optimization. Query Languages!= programming languages!

Preliminaries A query is applied to relation instances, and the result of a query is also a relation instance. Schemas of input and output relations for a query are determined by query Since each operation returns a relation, operations can be composed! (Algebra is closed.)

Formal Relational Query Languages Two mathematical Query Languages and one real QL language (i.e., SQL) Relational Algebra: More operational, very useful for representing execution plans. Relational Calculus/SQL: Lets users describe what they want, rather than how to compute it. (Non-operational, declarative.)

The SQL Query Language To find all 18 year old students, we can write: SELECT * FROM Students S WHERE S.age=18 sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@ee 18 3.2 To find just names and logins replace the first line: SELECT S.name, S.login. 24

Querying Multiple Relations What does the following SELECT S.name, E.cid query compute? FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade= A Given the following instances of Enrolled and Students: sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 sid cid grade 53831 Carnatic101 C 53831 Reggae203 B 53650 Topology112 A 53666 History105 B we get: S.name Smith E.cid Topology112

What is an Algebra Mathematical system consisting of: Operands --- variables or values from which new values can be constructed. Operators --- symbols denoting procedures that construct new values from given values. 26

What is Relational Algebra? An algebra whose operands are relations or variables that represent relations. Operators are designed to do the most common things that we need to do with relations in a database. The result is an algebra that can be used as a query language for relations. 27

Roadmap on RA Core relational algebra (set semantics) Bag semantics Extended relational algebra: additional operators 28

Core Relational Algebra Union, intersection, and difference. Usual set operations, but require both operands have the same relation schema. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes. 29

Union, intersection, and difference R1 := R2 UNION R3 R1 := R2 INTERSECT R3 R1 := R2 DIFFERENCE R3 R1 Schema? Data? 30

Selection R1 := SELECT C (R2) R1: Schema? Data? C is a condition (as in if statements) that refers to attributes of R2. R1 is all those tuples of R2 that satisfy C. 31

Example Relation Sells: bar beer price Joe s Bud 2.50 Joe s Miller 2.75 Sue s Bud 2.50 Sue s Miller 3.00 JoeMenu := SELECT bar= Joe s (Sells): bar beer price Joe s Bud 2.50 Joe s Miller 2.75 32

Projection R1 := PROJ L (R2) L is a list of attributes from the schema of R2. R1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1. Eliminate duplicate tuples, if any. 33

Example Relation Sells: bar beer price Joe s Bud 2.50 Joe s Miller 2.75 Sue s Bud 2.50 Sue s Miller 3.00 Prices := PROJ beer,price (Sells): beer price Bud 2.50 Miller 2.75 Miller 3.00 34

Product R3 := R1 R2 Schema of R3 is the attributes of R1 and then R2, in order. But beware attribute A of the same name in R1 and R2: use R1.A and R2.A. Pair each tuple t1 of R1 with each tuple t2 of R2. Concatenation t1t2 is a tuple of R3. 35

Example: R3 := R1 R2 R1( A, B ) 1 2 3 4 R2( B, C ) 5 6 7 8 9 10 R3( A, R1.B, R2.B, C ) 1 2 5 6 1 2 7 8 1 2 9 10 3 4 5 6 3 4 7 8 3 4 9 10 36

Theta-Join R3 := R1 JOIN C R2 Take the product R1 R2. Then apply SELECT C to the result. C can be any boolean-valued condition. Why called Theta-Join? Common form of boolean condition: A B, where is =, <, etc. 37

Example Sells( bar, beer, price ) Bars( name, addr ) Joe s Bud 2.50 Joe s Maple St. Joe s Miller 2.75 Sue s River Rd. Sue s Bud 2.50 Sue s Coors 3.00 BarInfo := Sells JOIN Sells.bar = Bars.name Bars BarInfo( bar, beer, price, name, addr ) Joe s Bud 2.50 Joe s Maple St. Joe s Miller 2.75 Joe s Maple St. Sue s Bud 2.50 Sue s River Rd. Sue s Coors 3.00 Sue s River Rd. 38

Natural Join A frequent type of join connects two relations by: Equating attributes of the same name, and Projecting out one copy of each pair of equated attributes. Called natural join. Denoted R3 := R1 JOIN R2. 39

Example Sells( bar, beer, price ) Bars( bar, addr ) Joe s Bud 2.50 Joe s Maple St. Joe s Miller 2.75 Sue s River Rd. Sue s Bud 2.50 Sue s Coors 3.00 BarInfo := Sells JOIN Bars BarInfo( bar, beer, price, addr ) Joe s Bud 2.50 Maple St. Joe s Milller 2.75 Maple St. Sue s Bud 2.50 River Rd. Sue s Coors 3.00 River Rd. 40

Renaming The RENAME operator gives a new schema to a relation. R1 := RENAME R1(A1,,An) (R2) makes R1 be a relation with attributes A1,,An and the same tuples as R2. Simplified notation: R1(A1,,An) := R2. 41

Example Bars( name, addr ) Joe s Maple St. Sue s River Rd. R(bar, addr) := Bars R( bar, addr ) Joe s Maple St. Sue s River Rd. 42

Building Complex Expressions Combine operands and operators with parentheses and precedence rules. Three notations, just as in arithmetic: 1. Sequences of assignment statements. 2. Expressions with several operators. 3. Expression trees. 43

Sequences of Assignments Example: R3 := R1 JOIN C R2 can be written: R4 := R1 R2 R3 := SELECT C (R4) Create temporary relation names. 44

Expressions in a Single Assignment Example: the theta-join R3 := R1 JOIN C R2 can be written: R3 := SELECT C (R1 R2) Precedence of relational operators: 1. [SELECT, PROJECT, RENAME] (highest). 2. [PRODUCT, JOIN]. 3. INTERSECTION. 4. [UNION, DIFFERENCE] 45

Expression Trees Leaves are operands --- either variables of relations or constant relations. Interior nodes are operators, applied to their child or children relation(s). Example: R3 := SELECT C (R1 R2) 46

Example Using the relations Bars(name, addr) and Sells(bar, beer, price), find the names of all the bars that are either on Maple St. or sell Bud for less than $3. Sequence of assignment? Complex expression? Expression tree? 47

As a Tree: UNION RENAME R(name) PROJECT name PROJECT bar SELECT addr = Maple St. SELECT price<3 AND beer= Bud Bars Sells 48

Example 2 Using Sells(bar, beer, price), find the bars that sell two different beers at the same price. Sequence of Assignments? Expression? Expression Tree? Strategy: Use renaming to define a copy of Sells, called S(bar, beer1, price) Use natural join of Sells and S such that the bar sells both beers at this price. Resulting schema (bar, beer, beer1, price) 49

The Tree PROJECT bar SELECT beer!= beer1 JOIN RENAME S(bar, beer1, price) Sells Sells 50

Summary: Schemas for Results Union, intersection, and difference: the schemas of the two operands must be the same, so use that schema for the result. Selection: schema of the result is the same as the schema of the operand. Projection: list of attributes in L tells us the schema. 51

Summary: Schemas for Results Product: schema is the attributes of both relations. Use R.A, etc., to distinguish two attributes named A. Theta-join: same as product. Natural join: union of the attributes of the two relations. Renaming: the operator tells the schema. 52