1 Lecture No 16 Asma Ahmad Relational Algebra I March 10, 2011 Database Systems.

Slides:



Advertisements
Similar presentations
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
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.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Relational Databases - Amit Bhawnani & Nimesh Shah.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
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 Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
BRANCH-SCHEME (BRANCH-NAME, ASSETS, BRANCH-CITY)
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. 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.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
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.
CSCD343- Introduction to databases- A. Vaisman1 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.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
CS 370 Database Systems Lecture 11 Relational Algebra.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
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.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
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.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Advanced Relational Algebra & SQL (Part1 )
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
Relational Algebra p BIT DBMS II.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Jianping Fan.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Relational Algebra Chapter 4 1.
Relational Algebra Chapter 4, Part A
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Relational Algebra.
Relational Algebra 1.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
Relational Algebra Chapter 4 - part I.
Instructor: Mohamed Eltabakh
Relational Algebra Chapter 4, Sections 4.1 – 4.2
CENG 351 File Structures and Data Managemnet
Relational Algebra & Calculus
Relational Algebra Chapter 4 - part I.
Presentation transcript:

1 Lecture No 16 Asma Ahmad Relational Algebra I March 10, 2011 Database Systems

2 Relational Algebra Relational Query Languages Formal Relational Query Languages Operations in Relational Algebra Selection Projection Cross Product Renaming Union, Intersection, Set Difference Join and its types Division Grouping & Aggregate Functions Examples Summary

3 Relational Query Languages Query languages: allow manipulation and retrieval of data from a database. Relational QLs are simple & powerful.  Strong formal foundation based on logic.  Allows for much optimization. Query languages != programming languages!  Not intended for complex calculations.  Support easy, efficient access to large data sets.

4 Formal Relational Query Languages Two mathematical query languages form the basis for “real” languages (e.g., SQL), and for implementation: 1. Relational algebra: more operational, very useful for understanding meanings of queries and for representing execution plans. 2. Relational calculus: lets users describe what they want, rather than how to compute it.

Difference The relational algebra might suggest these steps to retrieve the phone numbers and names of book stores that supply Some Sample Book:  Join books and titles over the BookstoreID.  Restrict the result of that join to tuples for the book Some Sample Book.  Project the result of that restriction over StoreName and StorePhone. The relational calculus would formulate a descriptive, declarative way:  Get StoreName and StorePhone for supplies such that there exists a title BK with the same BookstoreID value and with a BookTitle value of Some Sample Book. The relational algebra and the relational calculus are essentially logically equivalent: for any algebraic expression, there is an equivalent expression in the calculus, and vice versa 5

6 Relational Algebra Basic Operations:  Selection (  ): choose a subset of rows.  Projection (  ): choose a subset of columns.  Cross Product (  ): Combine two tables.  Union (  ): unique tuples from either table.  Set difference (  ): tuples in R1 not in R2.  Renaming (  ): change names of tables & columns Additional Operations (for convenience):  Intersection, joins (very useful), division, outer joins, aggregate functions, etc.

7 Selection Format:  selection-condition (R). Choose tuples that satisfy the selection condition. Result has identical schema as the input.  Major = ‘ CS ’ (Students) Students Result  Selection condition is a Boolean expression including =, ,, , and, or, not.

8 Projection Format:  attribute-list (R). Retain only those columns in the attribute-list. Result must eliminate duplicates.  Major (Students) StudentsResult  Operations can be composed.  Name, GPA (  Major = ‘CS’ (Students))

9 Cross Product Format: R1  R2. Each row of R1 is paired with each row of R2. Result schema consists of all attributes of R1 followed by all attributes of R2.  Problem: Columns may have identical names. Use notation R.A, or renaming attributes.  Only some rows make sense. Often need a selection to follow.

10 Example of Cross Product Students  Awards Students Awards

11 Renaming Format:  S (R) or  S(A1, A2, …) (R): change the name of relation R, and names of attributes of R  CS_Students (  Major = ‘ CS ’ (Students)) Students CS_Students

12 Union, Intersection, Set Difference Format: R1  R2 (R1  R2, R1  R2). Return all tuples that belong to either R1 or R2 (to both R1 and R2; to R1 but not to R2). Requirement: R1 and R2 are union compatible.  With same number of attributes.  Corresponding attributes have same domains. Schema of result is identical to that of R1. May need renaming.  Duplicates are eliminated.

13 Examples of Set Operations TAs RAs TAs  RAs TAs  RAs TAs  RAs

14 Joins Theta Join.  Format: R1 join-condition R2.  Returns tuples in  join-condition (R1  R2) Equijoin.  Same as Theta Join except the join-condition contains only equalities. Natural Join.  Same as Equijoin except that equality conditions are on common attributes and duplicate columns are eliminated.

15 Examples of Joins Theta Join. Students Students.Age<=Profs.Age Profs Students Profs Result

16 Examples of Joins (cont.) Equijoin. Students Prof=PID AND Name=Pname Profs Result  Natural Join. Students Profs Result

Relational Algebra Defined: Where is it in DBMS? parser SQL Relational algebra expression Optimized Relational algebra expression Query optimizer Code generator Query execution plan Executable code DBMS

18 Dangling Tuples in Join Usually, only a subset of tuples of each relation will actually participate in a join. Tuples of a relation not participating in a join are dangling tuples. How do we keep dangling tuples in the result of a join? (Why do we want to do that?)  Use null values to indicate a “no-join” situation.

19 Outer Joins Left Outer Join.  Format: R1 R2. Similar to a natural join but keep all dangling tuples of R1. Right Outer Join.  Format: R1 R2. Similar to a natural join but keep all dangling tuples of R2. (Full) Outer Join.  Format: R1 R2. Similar to a natural join but keep all dangling tuples of both R1 & R2.

20 Examples of Outer Joins Left Outer Join. Students Awards Students Awards Result

21 Join: An Observation Some tuples don’t contribute to the result, they get lost. EmployeeDepartment Brown Jones Smith A B B Department B C Head Black White EmployeeDepartmentHead Jones Smith B B Black

22 Outer Join An outer join extends those tuples with null values that would get lost by an (inner) join. The outer join comes in three versions  left: keeps the tuples of the left argument, extending them with nulls if necessary  right:... of the right argument...  full:... of both arguments...

23 Left Outer Join EmployeeDepartment Brown Jones Smith A B B Employee Department B C Head Black White Department EmployeeDepartmentHead Brown Jones A B null Black SmithBBlack EmployeeDepartment Left

24 Right Outer Join EmployeeDepartment Brown Jones Smith A B B Employee Department B C Head Black White Department EmployeeDepartmentHead Jones Smith B B Black nullCWhite Employee Department Right

25 Full Outer Join EmployeeDepartment Brown Jones Smith A B B Employee Department B C Head Black White Department EmployeeDepartmentHead Brown Jones A B null Black SmithBBlack EmployeeDepartment Full nullCWhite

26 Division Format: R1  R2. Restriction: Every attribute in R2 is in R1. For R1(A1,..., An, B1,..., Bm)  R2(B1,..., Bm) and T =  A1,..., An (R1), Return the subset of T, say W, such that every tuple in W  R2 is in R1.  W is the largest subset of T, such that, (W  R2)  R1

27 An Example of Division Takes  CS_Req Takes Result CS_Req  What is the meaning of this expression?

28 Division (Definition) R  S  Defines a relation over the attributes C that consists of set of tuples from R that match combination of every tuple in S. Expressed using basic operations: T 1   C (R) T 2   C ((S X T 1 ) – R) T  T 1 – T 2

29 Example - Division Identify all clients who have viewed all properties with three rooms. (  clientNo, propertyNo (Viewing))  (  propertyNo (  rooms = 3 (PropertyForRent)))

30 DIVISION (  ) : QUERIES THAT INCLUDE THE PHRASE “FOR ALL”. Q. FIND ALL CUSTOMERS WHO HAVE AN ACCOUNT AT ALL BRANCHES LOCATED IN BROOKLYN. TO OBTAIN ALL BRANCHES IN BROOKLYN : R =  BRANCH-NAME (  BRANCH-CITY=“BROOKLYN” (BRANCH)) TO OBTAIN ALL CUSTOMER-NAME, BRANCH-NAME PAIRS FOR WHICH THE CUSTOMER HAS AN ACCOUNT AT A BRANCH : S =  CUSTOMER-NAME,BRANCH-NAME (DEPOSIT) NOW TO FIND CUSTOMERS WHO APPEAR IN S WITH EVERY BRANCH NAME IN R.  CUSTOMER-NAME,BRANCH-NAME (DEPOSIT)   BRANCH-NAME (  BRANCH- CITY=“BROOKLYN” (BRANCH))

31 Grouping & Aggregate Functions Format: group_attributes F aggregate_functions ( r ) Partition a relation into groups Apply aggregate function to each group Output grouping and aggregation values, one tuple per group Ex: Major F count(SID), avg(GPA) (Students) Students Result

Sample Schema for Exercises Student(ID, name, address, GPA, SAT) Campus(location, enrollment, rank) Apply(ID, location, date, major, decision)

Sample Queries Find Names and addresses of all students with GPA > 3.7 who applied to CS major and were rejected. List name and address of all students who didn’t apply anywhere.  name, address ((Students) Students.ID= Not_Apply.ID (  Not_Apply (  ID (Students) -  ID (Apply))))  name, address (  GPA>3.7  decision=‘No’  major=‘CS’ (Student Student.ID=Apply.ID Apply))

Queries In Relational Algebra Consider another database schema: Students(SSN, Name, GPA, Age, MajorDept) Enrollment(SSN, CourseNo, Grade) Courses(CourseNo, Title, DName) Departments(DName, Location, Phone)

Queries In Relational Algebra List name and id of CS students along with titles of courses that they take.  Students.SSN, Name, Title (  Students.MajorDept = ‘CS’ and Students.SSN = Enrollment.SSN and Enrollment.CourseNo = Courses.CourseNo (Students  Enrollment  Courses)) =  Students.SSN,Name,Title ((  MajorDept=‘CS’ (Students) Enrollment) Enrollment.CourseNo=Courses.CourseNo Courses) =  Students.SSN,Name,Title (  MajorDept=‘CS’ (Students) Students.SSN=Enrollment.SSN (Enrollment Courses))

BRANCH-SCHEME (BRANCH-NAME, ASSETS, BRANCH-CITY) CUSTOMER (CUSTOMER-NAME, STREET, CUSTOMER-CITY). DEPOSIT (BRANCH-NAME, ACCOUNT-NUMBER, CUSTOMER-NAME, BALANCE) BORROW-SCHEME = (BRANCH-NAME, LOAN- NUMBER, CUSTOMER-NAME, AMOUNT) CLIENT (CLIENT-NAME, BANKER-NAME) Another Database Schema

SELECT (  ) : Q. SELECT THOSE TUPLES OF THE BORROW RELATION WHERE BRANCH IS “PERRYRIDGE”.  BRANCH-NAME = “PERRYRIDGE” (BORROW) Q. FIND ALL TUPLES IN WHICH AMOUNT BORROWED IS LESS THAN $1200.  AMOUNT < 1200 (BORROW) WE CAN USE THE FOLLOWING OPERATIONS : =, , , , , . ALSO, WE DENOTE AND BY , OR BY . Q. FIND THOSE TUPLES PERTAINING TO LOANS OF MORE THAN $1200 MADE BY THE PERRYRIDGE BRANCH.  BRANCH-NAME = “PERRYRIDGE”  AMOUNT > 1200 (BORROW )

PROJECT (  ) : Q. SHOW CUSTOMERS AND THEIR BORROWING BRANCH-NAME.  BRANCH-NAME, CUSTOMER-NAME (BORROW) Q. FIND ALL THOSE CUSTOMERS WHO HAVE THE SAME NAME AS THEIR PERSONAL BANKER.  CUSTOMER-NAME  CUSTOMER-NAME = BANKER-NAME (CLIENT).

CARTESIAN PRODUCT (  ) : Q. FIND ALL CLIENTS OF BANKER JOHNSON AND THE CITY IN WHICH THEY LIVE.  BANKER-NAME=“JOHNSON” (CLIENT  CUSTOMER) NOW CLIENT.CUTOMER-NAME COLUMN CONTAINS ONLY CUSTOMERS of Banker JOHNSON. TO FIND ALL THE CLIENTS OF BANKER JOHNSON, WE WRITE  CLIENT.CUSTOMER-NAME = CUSTOMER.CUSTOMER- NAME (  BANKER-NAME=“JOHNSON” (CLIENT  CUSTOMER))

NOW WE WANT ONLY CUSTOMER-NAME AND CUSTOMER- CITY, WE WRITE  CLIENT.CUSTOMER-NAME,CUSTOMER-CITY (  CLIENT.CUSTOMER- NAME = CUSTOMER.CUSTOMER-NAME (  BANKER-NAME = “ JOHNSON ” (CLIENT*CUSTOMER)))