Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Relational Databases - Amit Bhawnani & Nimesh Shah.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
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.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple 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.
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.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Midterm 2 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Instructor: Mohamed Eltabakh
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
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.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Relational Model Database Management Systems I Alex Coman, Winter 2006.
Relational Model. 2 Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations Extended Relational-Algebra-Operations.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Yufis Azhar – Teknik Informatika – UMM.  Aggregation function takes a collection of values (of a single attribute) and returns a single value as a result.
Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.
Chapter 2: Relational Model. 2.2Unite International CollegeDatabase Management Systems Chapter 2: Relational Model Structure of Relational Databases Fundamental.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Database System Concepts, 5 th Ed. Bin Mu at Tongji University Chapter 2: Relational Model.
Computing & Information Sciences Kansas State University Wednesday, 03 Sep 2008CIS 560: Database System Concepts Lecture 3 of 42 Wednesday, 03 September.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan Chapter 2: Relational Model.
Aljabar Relasi.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Relational Algebra - Part 1
Relational Algebra Chapter 4 1.
Chapter 3: Relational Model III
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Chapter 2: Relational Model
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Relational Algebra Chapter 4 1.
Relational Algebra Chapter 4 - part I.
Instructor: Mohamed Eltabakh
CENG 351 File Structures and Data Managemnet
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
Relational Algebra Chapter 4 - part I.
CS 405G: Introduction to Database Systems
Presentation transcript:

Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire

Formal relational query languages

Is this the Algebra you know? Algebra -> operators and atomic operands Expressions -> applying operators to atomic operands and/or other expressions Algebra of arithmetic: operands are variables and constants, and operators are the usual arithmetic operators E.g., (x+y)*2 or ((x+7)/(y-3)) + x Relational algebra: operands are variables that stand for relations and relations (sets of tuples), and operations include union, intersection, selection, projection, Cartesian product, etc – E.g., (π c-ownerChecking-account) ∩ (π s-ownerSavings-account)

What is a query? A query is applied to relation instances, and the result of a query is also a relation instance. (view, query) – Schemas of input and output fixed, but instances not. Operators refer to relation attributes by position or name: – E.g., Account(number, owner, balance, type) – Positional notation easier for formal definitions, named-field notation more readable. – Both used in SQL

Relational Algebra Operations The usual set operations: union, intersection, difference Operations that remove parts of relations: selection, projection Operations that combine tuples from two relations: Cartesian product, join Since each operation returns a relation, operations can be composed!

Removing Parts of Relations Selection – rows Projection - columns

Selection: Example

Another selection

Example of Projection

Projection removes duplicates

Set Operations Union Intersection Difference

What happens when sets unite?

Union Operation – Example Relations r, s: r  s: AB  AB  2323 r s AB 

Union Example

Union Compatibility

Intersection

Set Difference Operation – Example Relations r, s: r – s: AB  AB  2323 r s AB  1111

Difference

Another way to show intersection?

Summary so far: E 1 U E 2 : union E 1 - E 2 : difference E 1 x E 2 : cartesian product  c (E 1 ) : select rows, c = condition (book has p for predicate) II s (E 1 ) : project columns : s =selected columns  x(c1,c2) (E 1 ) : rename, x is new name of E 1, c1 is new name of column

Combining Tuples of Two Relations Cross product (Cartesian product) Joins

Cartesian-Product Operation – Example Relations r, s: r x s: AB  1212 AB  CD  E aabbaabbaabbaabb CD  E aabbaabb r s

Cross Product Example

Cross Product Renaming operator:  Rename whole relation: Teacher X  secondteacher (Teacher)  Teacher.t-num, Teacher.t-name, secondteacher.t-num, secondteacher.t-name OR rename attribute before combining: Teacher X  secondteacher(t-num2, t-name2) (Teacher)  t-num, t-name, t-num2, t-name2 OR rename after combining  c(t-num1, t-name1, t-num2, t-name2) (Teacher X Teacher)  t-num1, t-name1, t-num2, t-name2 How to resolve????

Join: Example

Condition Join

Equi and Natural Join

Divide operator

Divide Operation

Divide Definition

When to divide?

Division Example

Dividing without division sign

Working out an example

Assignment operation

Why would we use Relational Algebra?????

Equivalencies help

ER vs RA Both ER and the Relational Model can be used to model the structure of a database. Why is it the case that there are only Relational Databases and no ER databases?

RA vs Full Programming Language Relational Algebra is not Turing complete. There are operations that cannot be expressed in relational algebra. What is the advantage of using this language to query a database?

Summary of Operators updated Summary so far: E 1 U E 2 : union E 1 - E 2 : difference E 1 x E 2 : cartesian product  c (E 1 ) : select rows, c = condition (book has p for predicate) II s (E 1 ) : project columns : s =selected columns  x(c1,c2) (E 1 ) : rename, x is new name of E 1, c1 is new name of column E 1  E 2 : division E 1 E 2 : join, c = match condition

Practice Find names of stars who’ve appeared in a 1994 movie Information about movie year available in Movies; so need an extra join: σ year=1994 (πname(Stars ⋈ AppearIn ⋈ Movies)) An even more efficient solution: π name (Stars ⋈ π name (AppearIn ⋈ (π movieId σ year=1994 (Movies))) A query optimizer can find this, given the first solution! A more efficient solution: π name (Stars ⋈ AppearIn ⋈ (σ year=1994 ( Movies))

Extended Relational Algebra Operations Generalized projection Outer join Aggregate functions

Generalized projection – calculate fields

Aggregate Operation – Example Relatio n r: AB   C g sum(c ) (r) sum(c ) 27

Aggregate Functions on more than one tuple Samples: –Sum –Count-distinct –Max –Min –Count –Avg Use “as” to rename branchname g sum(balance) as totalbalance ( account )

Aggregate Operation – Example Relation account grouped by branch-name: branch_name g sum( balance ) ( account ) branch_nameaccount_numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A branch_namesum(balance) Perryridge Brighton Redwood

Outer Join Keep the outer side even if no join Fill in missing fields with nulls

Outer Join – Example Relation loan Relation borrower customer_nameloan_number Jones Smith Hayes L-170 L-230 L loan_numberamount L-170 L-230 L-260 branch_name Downtown Redwood Perryridge

Outer Join – Example Inner Join loan Borrower loan_numberamount L-170 L customer_name Jones Smith branch_name Downtown Redwood Jones Smith null loan_numberamount L-170 L-230 L customer_namebranch_name Downtown Redwood Perryridge Left Outer Join loan Borrower

Outer Join – Example loan_numberamount L-170 L-230 L null customer_name Jones Smith Hayes branch_name Downtown Redwood null loan_numberamount L-170 L-230 L-260 L null customer_name Jones Smith null Hayes branch_name Downtown Redwood Perryridge null Full Outer Join loan borrower Right Outer Join loan borrower

Summary of Operators - Full E 1 U E 2 : union E 1 - E 2 : difference E 1 x E 2 : cartesian product  c (E 1 ) : select rows, c = condition (book has p for predicate) II s (E 1 ) : project columns : s =selected columns separated by commas, can have calculations included  x(c1,c2) (E 1 ) : rename, x is new name of E 1, c1 is new name of column E 1  E 2 : division E 1 E 2 : join, c = match condition E 1 E 2 : outer join, c = match condition, keep the side with the arrows  : assignment – give a new name to an expression to make it easy to read as : rename a calculated column attribute1 g function (attribute2) (E 1 ) : perform function on attribute2 whenever attribute1 changes