Instructor: Mohamed Eltabakh

Slides:



Advertisements
Similar presentations
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Advertisements

E-R Diagram for a Banking Enterprise
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
©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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
CMSC424: Database Design Instructor: Amol Deshpande
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
Relational Model. 2 Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations Extended Relational-Algebra-Operations.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Relational Algebra Instructor: Mohamed Eltabakh 1.
©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.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
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.
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.
Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations.
©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.
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.
Relational Algebra Instructor: Mohamed Eltabakh 1 Part II.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 07 of 42 Tuesday, 30 January 2007.
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.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Relational Algebra Instructor: Mohamed Eltabakh 1.
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.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
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.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
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, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Aljabar Relasi.
Structured Query Language
Relational Algebra - Part 1
Session 3 Welcome: To session 3-the fifth learning sequence
Chapter 3: Relational Model III
Chapter 2: Intro to Relational Model
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Chapter 2: Relational Model
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Session 3 Welcome: To session 3-the sixth learning sequence
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
More Complex Operators
Relational Algebra.
Chapter 2: Intro to Relational Model
SQL: Structured Query Language
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Presentation transcript:

Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu Relational Algebra Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

More Relational Operators

Joins We mentioned Cartesian Product multiplies two relations R X S S R What if I want to join R and S based on a certain condition? Natural and Theta Joins

Natural Join: R ⋈ S (Join on the common attributes) Consider relations R with attributes AR, and S with attributes AS. Let A = AR ∩ AS = {A1, A2, …, An} The common attributes In English Natural join R ⋈ S is a Cartesian Product R X S with equality predicates on the common attributes (Set A)

Natural Join: R ⋈ S R ⋈ S can be defined as : πAR – A, A, AS - A (σR.A1 = S.A1 AND R.A2 = S.A2 AND … R.An = S.An (R X S)) Project the union of all attributes Equality on common attributes Cartesian Product Common attributes appear once in the result

Natural Join: R ⋈ S: Example Implicit condition (R.B = S.B and R.D = S.D) S R R ⋈ S

Theta Join: R ⋈C S Theta Join is cross product, with condition C It is defined as : R ⋈C S = (σC (R X S)) Theta join can express both Cartesian Product & Natural Join Recommendation: Always use Theta join (more explicit and more clear) R S A B 1 2 3 D C 2 3 4 5 R ⋈ R.A>=S.CS A B D C 3 2

Example Queries Find customer names having account balance below 100 or above 10,000 πcustomer_name (depositor ⋈ πaccount_number(σbalance <100 OR balance > 10,000 (account))) This projection is optional

Assignment Operator:  The assignment operation (←) provides a convenient way to express complex queries on multiple line Write query as a sequence of line consisting of: Series of assignments Result expression containing the final answer Assignment must always be made to a temporary relation variable May use a variable multiple times in subsequent expressions Example: R1  (σ ((A=B) ^ (D>5)) (R – S)) ∩ W R2  R1 ⋈(R.A = T.C) T Result  R1 U R2

Example Queries For branches that gave loans > 100,000 or hold accounts with balances >50,000, report the branch name along whether it is reported because of a loan or an account R1  πbranch_name, ‘Loan’ As Type (σamount >100,000 (loan)) R2  πbranch_name, ‘Account’ As Type(σbalance > 50,000 (account))) Result  R1 U R2

Example Queries Find customers having account balance below 100 and loans above 10,000 R1  πcustomer_name (depositor ⋈ πaccount_number(σbalance <100 (account))) R2  πcustomer_name (borrower ⋈ πloan_number(σamount >10,000 (loan))) Result  R1 ∩ R2

More Relational Operators

Outer Join An extension of the join operation that avoids loss of information Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result Uses null values to fill in empty attributes with no matching Types of outer join between R and S Left outer (R o⋈ S): preserve all tuples from the left relation R Right outer (R ⋈o S): preserve all tuples from the right relation S Full outer (R ⋈ S): preserve all tuples from both relations o

Left Outer Join (R o⋈ S): Example R ⋈ S (R o⋈ S)

Right Outer Join (R ⋈o S): Example R ⋈ S (R ⋈o S)

Full Outer Join (R ⋈ S): Example

Outer Join R ⋈o S Outer Join also applies to theta join (R ⋈ S) R o⋈ S c o c c Any arbitrary condition for the join is allowed

Duplicate Elimination:  (R) Delete all duplicate records Convert a bag to a set R  (R) A B 1 2 3 4 A B 1 2 3 4

Grouping & Aggregation operator:  Aggregation function takes a collection of values and returns a single value as a result avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Grouing & Aggregate operation in relational algebra g1,g2, …gm, F1(A1), F2(A2), …Fn(An) (R) R is a relation or any relational-algebra expression g1, g2, …gm is a list of attributes on which to group (can be empty) Each Fi is an aggregate function applied on attribute Ai within each group

Grouping & Aggregation Operator: Example S R branch_name,sum(balance)(S) sum(c)(R)

Example Queries Find customer names having loans with sum > 20,000 πcustomer_name (σsum > 20,000 (customer_name, sum  sum(amount)(loan ⋈ borrower)))

Example Queries Find the branch name with the largest number of accounts R1  branch_name, countAccounts  count(account_number)(account) R2  Max  max(countAccounts)(R1) Result  πbranch_name(R1 ⋈countAccounts = Max R2)

Example Queries Find account numbers and balances for customers having loans > 10,000 πaccount_number, balance ( (depositor ⋈ account) ⋈ (πcustomer_name (borrower ⋈ (σamount >10,000 (loan)))) )

Reversed Queries (what does it do)? πcustomer_name(customer) - πcustomer_name(borrower) Find customers who did not take loans

Reversed Queries (what does it do)? R1  (MaxLoan  max(amount)(σbranch_name= “ABC” (loan))) Result  πcustomer_name(borrower ⋈ (R1 ⋈MaxLoan=amount^branch_name= “ABC” loan)) Find customer name with the largest loan from a branch “ABC”

Example Queries Find customer name with the largest loan from a branch in “NY” city

Summary of Relational-Algebra Operators Set operators Union, Intersection, Difference Selection & Projection & Extended Projection Joins Natural, Theta, Outer join Rename & Assignment Duplicate elimination Grouping & Aggregation