Relational Algebra Instructor: Mohamed Eltabakh 1.

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.
CMSC424: Database Design Instructor: Amol Deshpande
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.
Instructor: Mohamed Eltabakh
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.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Relational Algebra Instructor: Mohamed Eltabakh 1.
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.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
©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.
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.
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.
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.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
1 Session 3 Welcome: To session 3-the fourth learning sequence “Relational algebra “ Recap : In the previous learning sequences, we discussed the four.
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”
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
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.
Session 3 Welcome: To session 3-the fourth learning sequence
Relational Algebra - Part 1
Chapter 3: Relational Model III
Chapter 2: Intro to Relational Model
SQL Structured Query Language 11/9/2018 Introduction to Databases.
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
Relational Algebra.
Chapter 2: Intro to Relational Model
SQL: Structured Query Language
Chapter 2: Intro to Relational Model
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Presentation transcript:

Relational Algebra Instructor: Mohamed Eltabakh 1

More Relational Operators 2

Joins We mentioned Cartesian Product multiplies two relations 3 R S R X S 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 A R, and S with attributes A S. Let A = A R ∩ A S = {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) 4

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

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

Theta Join: R ⋈ C S Theta Join is cross product, with condition C It is defined as : R ⋈ C S = (σ C (R X S)) AB R DC S R ⋈ R.A>=S.C S ABDC 3223 Theta join can express both Cartesian Product & Natural Join 7 Recommendation: Always use Theta join (more explicit and more clear)

Example Queries Find customer names having account balance below 100 or above 10,000 π customer_name (depositor ⋈ π account_number (σ balance 10,000 (account))) 8 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 Example: R1  ( σ ((A=B) ^ (D>5)) (R – S)) ∩ W R2  R1 ⋈ (R.A = T.C) T Result  R1 U R2 9

Example Queries 10 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 11

More Relational Operators 12

Duplicate Elimination:  (R) Delete all duplicate records Convert a bag to a set R AB  (R) AB

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 14

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

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

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

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

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

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

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

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