Download presentation
Presentation is loading. Please wait.
Published bySharon Campbell Modified over 9 years ago
1
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Jianping Fan
2
Database Management Systems 2 Raghu Ramakrishnan Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports simple, powerful QLs: –Strong formal foundation based on logic. –Allows for much optimization. v Query Languages != programming languages! –QLs not expected to be “Turing complete”. –QLs not intended to be used for complex calculations. –QLs support easy, efficient access to large data sets.
3
Database Management Systems 3 Raghu Ramakrishnan Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: ¶ Relational Algebra : More operational, very useful for representing execution plans. · Relational Calculus : Lets users describe what they want, rather than how to compute it. * Understanding Algebra is key to understanding * SQL, query processing!
4
Database Management Systems 4 Raghu Ramakrishnan Preliminaries v A query is applied to relation instances, and the result of a query is also a relation instance. – Schemas of input relations for a query are fixed (but query will run regardless of instance!) –The schema for the result of a given query is also fixed! Determined by definition of query language constructs. v Positional vs. named-field notation: –Positional notation easier for formal definitions, named-field notation more readable. –Both used in SQL
5
Database Management Systems 5 Raghu Ramakrishnan Example Instances R1 S1 S2 v “Sailors” and “Reserves” relations for our examples. v We’ll use positional or named field notation, assume that names of fields in query results are `inherited’ from names of fields in query input relations.
6
Database Management Systems 6 Raghu Ramakrishnan Relational Algebra v Basic operations: – Selection ( ) Selects a subset of rows from relation. – Projection ( ) Deletes unwanted columns from relation. – Cross-product ( ) Allows us to combine two relations. – Set-difference ( ) Tuples in reln. 1, but not in reln. 2. – Union ( ) Tuples in reln. 1 and in reln. 2. v Additional operations: –Intersection, join, division, renaming: Not essential, but (very!) useful.
7
Database Management Systems 7 Raghu Ramakrishnan Projection v Deletes attributes that are not in projection list. v Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. v Projection operator has to eliminate duplicates ! –Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it.
8
Database Management Systems 8 Raghu Ramakrishnan Projection v How to implement projection ? If you are the first person to design SQL, how to implement projection?
9
Database Management Systems 9 Raghu Ramakrishnan Selection v Selects rows that satisfy selection condition. v No duplicates in result! v Schema of result identical to schema of (only) input relation. v Result relation can be the input for another relational algebra operation! ( Operator composition. )
10
Database Management Systems 10 Raghu Ramakrishnan Selection v How to implement selection ? If you are the first person for SQL design, how to implement selection?
11
Database Management Systems 11 Raghu Ramakrishnan Testing Example 1 S
12
Database Management Systems 12 Raghu Ramakrishnan Testing Example 2 S
13
Database Management Systems 13 Raghu Ramakrishnan Testing Example 3 S
14
Database Management Systems 14 Raghu Ramakrishnan Testing Example 3 S
15
Database Management Systems 15 Raghu Ramakrishnan Testing Example 4 S sidsnameratingage 23sandy721.0 32windy819.5 45jib525.0 68topsail1025.0
16
Database Management Systems 16 Raghu Ramakrishnan Union, Intersection, Set-Difference B AUBAUB AnBAnBA-B A B-A A U B = A + B - AnB
17
Database Management Systems 17 Raghu Ramakrishnan Union, Intersection, Set-Difference v All of these operations take two input relations, which must be union-compatible : –Same number of fields. –`Corresponding’ fields have the same type. v What is the schema of result? Occur in either S1 or S2 or both Occur in both S1 and S2 Occur in S1 but not in S2
18
Database Management Systems 18 Raghu Ramakrishnan Testing Example 1 sidsnameratingage 23sandy721.0 32windy819.5 59rope1018.0 sidsnameratingage 23sandy721.0 32windy819.5 45jib525.0 68topsail1025.0 S3 S4 v Work out on board SS34 SS34 SS34
19
Database Management Systems 19 Raghu Ramakrishnan Union, Intersection, Set-Difference (continued) v Answers sidsnameratingage 23sandy721.0 32windy819.5 59rope1018.0 45jib525.0 68topsail1025.0 sidsnameratingage 23sandy721.5 32windy819.5 SS34 SS34 sidsnameratingage 59rope1018.0 SS34
20
Database Management Systems 20 Raghu Ramakrishnan Testing Example 2 v Work out on board S1 S2
21
Database Management Systems 21 Raghu Ramakrishnan Testing Example 3 v Work out on board S1 S2 sidsnameratingage 23sandy721.0 32windy819.5 59rope1018.0 45jib525.0 68topsail1025.0
22
Database Management Systems 22 Raghu Ramakrishnan Cross-Product v Each row of S1 is paired with each row of R1. v Result schema has one field per field of S1 and R1, with field names `inherited’ if possible. – Conflict : Both S1 and R1 have a field called sid. * Renaming operator :
23
Database Management Systems 23 Raghu Ramakrishnan Cross-Product: all possible combinations
24
Database Management Systems 24 Cross-Product v How to implement cross-product X? If you are the first person for SQL design, how to implement cross-product?
25
Database Management Systems 25 Raghu Ramakrishnan Testing Example 1
26
Database Management Systems 26 Raghu Ramakrishnan Testing Example 2
27
Database Management Systems 27 Raghu Ramakrishnan Joins v Condition Join : v Result schema same as that of cross-product. v Fewer tuples than cross-product, might be able to compute more efficiently v Sometimes called a theta-join.
28
Database Management Systems 28 Raghu Ramakrishnan Testing Example 1: Condition Join
29
Database Management Systems 29 Raghu Ramakrishnan Testing Example 3
30
Database Management Systems 30 Raghu Ramakrishnan Testing Example 2: Condition Join
31
Database Management Systems 31 Raghu Ramakrishnan Testing Example 3: Condition Join
32
Database Management Systems 32 Raghu Ramakrishnan Testing Example 4: Condition Join
33
Database Management Systems 33 Raghu Ramakrishnan Joins v Equi-Join : A special case of condition join where the condition c contains only equalities. v Result schema similar to cross-product, but only one copy of fields for which equality is specified. v Natural Join : Equijoin on all common fields.
34
Database Management Systems 34 Raghu Ramakrishnan Joins v How to implement Join, Equal Join, Natural Join? If you are the first person for SQL design, how to implement join operators?
35
Database Management Systems 35 Raghu Ramakrishnan Testing Example 1: Equal Join
36
Database Management Systems 36 Raghu Ramakrishnan Testing Example 2: Equal Join
37
Database Management Systems 37 Raghu Ramakrishnan Testing Example 3: Equal Join
38
Database Management Systems 38 Raghu Ramakrishnan Division v Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. v Let A have 2 fields, x and y ; B have only field y : – A/B contains all x tuples (sailors) such that for every y tuple (boat) in B, there is an xy tuple in A. – Or : If the set of y values (boats) associated with an x value (sailor) in A contains all y values in B, the x value is in A/B. v In general, x and y can be any lists of fields; y is the list of fields in B, and x y is the list of fields of A.
39
Database Management Systems 39 Raghu Ramakrishnan Examples of Division A/B A B1 B2 B3 A/B1 A/B2A/B3
40
Database Management Systems 40 Raghu Ramakrishnan Expressing A/B Using Basic Operators v Division is not essential op; just a useful shorthand. –(Also true of joins, but joins are so common that systems implement joins specially.) v Idea : For A/B, compute all x values that are not `disqualified’ by some y value in B. – x value is disqualified if by attaching y value from B, we obtain an xy tuple that is not in A. Disqualified x values: A/B: all disqualified tuples
41
Database Management Systems 41 Raghu Ramakrishnan Expressing A/B Using Basic Operators (continued) v A/B2 sno s1 s2 s3 s4 sno A pno s1p2 s1p4 s2p2 s2p4 s3p2 s3p4 s4p2 s4p4 sno A X B2 snopno s2p4 s3p4 sno A X B2 ) - A ( sno s2 s3 sno A X B2 ) - A ( sno ( ) A X B2 ) - A ( sno ( )) = sno A - ( sno s1 s4
42
Database Management Systems 42 Raghu Ramakrishnan v Work A/B3 on board Testing Example 1: A/B
43
Database Management Systems 43 Raghu Ramakrishnan Testing Example 1: A/B v A/B3 sno s1 s2 s3 s4 sno A pno s1p1 s1p2 s1p4 s2p1 s2p2 s2p4 s3p1 s4p4 sno A X B3 snopno s2p4 s3p1 sno A X B3 ) - A ( sno s2 s3 sno A X B3 ) - A ( sno ( ) A X B3 ) - A ( sno ( )) = sno A - ( sno s1 s4 s3p4 s4p1 s3p2 s3p4 s4p1 s4p2
44
Database Management Systems 44 Raghu Ramakrishnan v Work A/B4 on board Testing Example 2: A/B pno p1 p2
45
Database Management Systems 45 Raghu Ramakrishnan v Work A/B4 on board Testing Example 2: A/B pno p1 p2
46
Database Management Systems 46 Raghu Ramakrishnan v Work A/B1 on board Testing Example 3: A/B
47
Database Management Systems 47 Raghu Ramakrishnan Example Instances R1 S1 S2 bidbnamebcolor 101Gippy red 103Fullsail green B1
48
Database Management Systems 48 Raghu Ramakrishnan Find names of sailors who’ve reserved boat #103 v Solution 1: v Solution 2 : v Solution 3 :
49
Database Management Systems 49 Raghu Ramakrishnan Find names of sailors who’ve reserved a red boat v Information about boat color only available in Boats; so need an extra join: v A more efficient solution: * A query optimizer can find this given the first solution!
50
Database Management Systems 50 Raghu Ramakrishnan Find sailors who’ve reserved a red or a green boat v Can identify all red or green boats, then find sailors who’ve reserved one of these boats: v Can also define Tempboats using union! v Select boats color=red and union with color=green v What happens if is replaced by in this query? v Nothing selected because color can’t be red & green
51
Database Management Systems 51 Raghu Ramakrishnan Find sailors names who’ve reserved a red and a green boat v Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors):
52
Database Management Systems 52 Raghu Ramakrishnan v Uses division; schemas of the input relations to / must be carefully chosen: Find the names of sailors who’ve reserved all boats v To find sailors who’ve reserved all ‘Interlake’ boats:.....
53
Database Management Systems 53 Testing Example 1 Students Name Login Id AgeGPA Courses Id NameCredit Enrolled_In Grade Find students’ names who get ``A” from ITCS6160 at Spring 2015 semester
54
Database Management Systems 54 Testing Example 2 Patients Name AddrPhoneAge Drug Name ManufExp Prescribed Dosage #days Find Drug’s name which John Smith takes two pieces per day
55
Database Management Systems 55 name Employees ssn lot Works_In2 from to dname budget did Departments Testing Example 3 Find Department’s name John Smith work from 2001 to 2015
56
Database Management Systems 56 dname budget did name Departments ssn lot Employees Works_In3 Duration from to Testing Example 4 Find ssn who work in ``cs” department from 2001 to 2015
57
Database Management Systems 57 Raghu Ramakrishnan lot dname budget did since name Works_In Departments Employees ssn Locations address capacity Find the address for the employees who work in ``cs’ department since 2001 Testing Example 5
58
Database Management Systems 58 lot name dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In Testing Example 6 Find the name who is managing ``cs” department now Find the starting ``work-in” day who is managing ``cs” department now
59
Database Management Systems 59 Raghu Ramakrishnan lot name dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In age pname DependentsPolicy cost Find the employees’names who is now working in ``cs” department and his family insurance cost is above $10,000 Testing Example 7
60
Database Management Systems 60 Raghu Ramakrishnan lot name dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In age pname DependentsPolicy cost Find the employees’names who is now managing ``cs” department and his family insurance cost is above $20,000 Testing Example 8
61
Database Management Systems 61 Raghu Ramakrishnan Summary v The relational model has rigorously defined query languages that are simple and powerful. v Relational algebra is more operational; useful as internal representation for query evaluation plans. v Several ways of expressing a given query; a query optimizer should choose the most efficient version.
62
Database Management Systems 62 Raghu Ramakrishnan lot name dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In age pname DependentsPolicy cost Find the employees’ssn who is now managing ``cs” department and his/her dependents’ age is above 50 Homework 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.