Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Lecture No 16 Asma Ahmad Relational Algebra I March 10, 2011 Database Systems."— Presentation transcript:

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

2 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 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 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.

5 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 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 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 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 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 10 Example of Cross Product Students  Awards Students Awards

11 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 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 13 Examples of Set Operations TAs RAs TAs  RAs TAs  RAs TAs  RAs

14 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 15 Examples of Joins Theta Join. Students Students.Age<=Profs.Age Profs Students Profs Result

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

17 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 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 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 20 Examples of Outer Joins Left Outer Join. Students Awards Students Awards Result

21 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 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 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 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 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 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 27 An Example of Division Takes  CS_Req Takes Result CS_Req  What is the meaning of this expression?

28 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 29 Example - Division Identify all clients who have viewed all properties with three rooms. (  clientNo, propertyNo (Viewing))  (  propertyNo (  rooms = 3 (PropertyForRent)))

30 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 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

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

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

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

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

36 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

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

38 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).

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

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


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

Similar presentations


Ads by Google