Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2003-2012 Curt Hill The Relational Algebra What operations can be done?

Similar presentations


Presentation on theme: "Copyright © 2003-2012 Curt Hill The Relational Algebra What operations can be done?"— Presentation transcript:

1 Copyright © 2003-2012 Curt Hill The Relational Algebra What operations can be done?

2 Algebra There exists a relational algebra What is an algebra? What most of us know as Algebra is actually the Algebra of Real Numbers There is also Boolean Algebra among others Copyright © 2003-2012 Curt Hill

3 Algebra of Real Numbers What does this Algebra consist of? A set of objects to work on –This is the infinite set of real numbers A set of operators –These include , , ,  –The  may be subtraction or negation –Each operator takes one or two real numbers and computes another real –There are rules on how they must be applied Copyright © 2003-2012 Curt Hill

4 Relational Algebra Mostly the same The object that it operates on are not real numbers but tables –Sets of relations It also possesses operators –Each takes one or two tables and produces another table These are considered next Copyright © 2003-2012 Curt Hill

5 Relational Operations Selection Projection Cartesian product Set union Set intersection Set difference First two take one table, the rest two The above are primitive operations –There are also composite operations Copyright © 2003-2012 Curt Hill

6 Operations Relational operations are like arithmetic operations They are unary or binary They take operands and produce a result The operands and results are tables

7 Copyright © 2003-2012 Curt Hill Selection Choose or eliminate tuples based on a comparison Unary operation There is a boolean test which determines if a row is eliminated or not The symbol is the sigma ( s)

8 Copyright © 2003-2012 Curt Hill Boolean test of selection The test may include –Fields compared with constants –Fields compared with other fields in same record The test may be ANDed, ORed or NOTted together

9 Copyright © 2003-2012 Curt Hill Selection Example s cnt > 2 (T1) IDCntSrcUseFrq A1X185 B5X45 C2W164 E3Z129 IDCntSrcUseFrq B5X45 E3Z129

10 Copyright © 2003-2012 Curt Hill Projection Choose or eliminate columns Unary operation We may also rearrange the rows that are left The symbol is pi ( p)

11 Copyright © 2003-2012 Curt Hill Projection Example p Src, Cnt, ID (T1) IDCntSrcUseFrq A1X185 B5X45 C2W164 E3Z129 SrcCntID X1A X5B W2C Z3E

12 Copyright © 2003-2012 Curt Hill More Projection Notes Usually projection is used to change degree of relation A relation is a set –It must have unique entries before a projection –This may not be so when an attribute is removed Projection may eliminate duplicates –Not all systems actually do this –Why not?

13 Copyright © 2003-2012 Curt Hill Second Projection Example p Src, Cnt, ID (T1) IDCntSrcUseFrq A1X185 A1X45 C2W164 C2W129 SrcCntID X1A W2C

14 Copyright © 2003-2012 Curt Hill Cartesian Product AKA Cross product Binary operation Append to each row in first each row in the second The number of tuples in the result is the product of the number of rows in the operands –This could be large and expensive –Seldom done without optimization

15 Copyright © 2003-2012 Curt Hill Cartesian Product Example T1  T2 IDCnt A1 B5 IDCntF1F2F3 A1A61 B5A61 A1X42 B5X42 A1B39 B5B39 F1F2F3 A61 X42 B39

16 Copyright © 2003-2012 Curt Hill Cartesian Addendum Only primitive operator that deals with two different schemas If the two tables have a common field, one of the fields must be renamed –A tuple must be a set with unique field names

17 Copyright © 2003-2012 Curt Hill Cartesian Product Example T1  T2 IDCnt A1 B5 IDCntID2Cnt2Src A1A61 B5A61 A1X42 B5X42 A1B39 B5B39 IDCntSrc A61 X42 B39

18 Copyright © 2003-2012 Curt Hill Set Union Binary operation Two relations must be union compatible –They must have same schema, that is the same attributes New relation has all the tuples of both tables with duplicates removed

19 Copyright © 2003-2012 Curt Hill Union Example T1  T2 IDCnt A1 D8 B5 IDCnt B5 E2 A1 IDCnt A1 D8 E2 B5

20 Copyright © 2003-2012 Curt Hill Set Intersection Binary operation Two relations must be union compatible –They must have same schema, that is the same attributes New relation has only the tuples in both tables

21 Copyright © 2003-2012 Curt Hill Intersection Example T1  T2 IDCnt A1 D8 B5 IDCnt B5 E2 A1 IDCnt A1 B5

22 Copyright © 2003-2012 Curt Hill Set Difference Binary operation Two relations must be union compatible –They must have same schema, that is the same attributes New relation has only the tuples in both tables removed from first table Not symmetrical or commutative

23 Copyright © 2003-2012 Curt Hill Set Difference Example T1 - T2 IDCnt A1 D8 B5 IDCnt B5 E2 A1 IDCnt D8 T2 – T1 IDCnt E2

24 Copyright © 2003-2012 Curt Hill Relational Algebra The algebra only uses these operations –All of our queries translate into these Each operation produces a relation –Starts with one or two relations The algebra is closed –Maps from the set of relations back to the set of relations There are also composite operations

25 Copyright © 2003-2012 Curt Hill Join Binary composite operation It is the composite of three operations –Cartesian product –Selection –Projection (optional) Often the only way cartesian products are done –Thus the DBMS may optimize it

26 Copyright © 2003-2012 Curt Hill Join A join always operates on joining the two tables through a common field (or fields) in each Thus we join on one or more fields that are in common between the two tables –The fields must have the same format, often have same name

27 Copyright © 2003-2012 Curt Hill Join Process Take the product of the two tables Use select to eliminate all records where the two fields are not equal Eliminate one of the redundant fields Resulting table as the sum of the two tables field minus one The number of rows is dependent on data

28 Copyright © 2003-2012 Curt Hill Natural Join Example T1  ID T2 IDCnt A1 B5 IDCntSrcDst A161 A132 B539 IDSrcDst A61 X42 A32 B39

29 Copyright © 2003-2012 Curt Hill Types of Joins The relationship between the two joined fields may be anything –We specify the fields and comparison –Called a Condition Join –Same schema as product When comparison is equality the join is called an Equijoin –Project on equijoin to eliminate redundant column If the join is equijoin on all common fields then it is called a Natural Join

30 Copyright © 2003-2012 Curt Hill Condition Join Example T1  T1.Cnt<T2.Cnt T2 IDCnt A5 B3 IDCntID2Cnt2Dst A5A61 B3A61 B3X42 IDCntDst A61 X42 B39

31 Copyright © 2003-2012 Curt Hill Join Importance The cartesian product is only primitive that may take two different relation types Cartesian products are usually inside a join Usually only one table in a database has a particular schema Almost every multiple table queries will use a join

32 Copyright © 2003-2012 Curt Hill Division Division a composite not primitive operation Deals with three relations of different degree –First table degree m+n –Second of degree n –Result of degree m Columns in the second table are eliminated from the first

33 Copyright © 2003-2012 Curt Hill Division Process The columns in the second table correspond to those in the first If the values in the first table match any corresponding values in the second the row is copied to result The common columns are eliminated in the result Duplicates are then eliminated

34 Copyright © 2003-2012 Curt Hill Division Example T1 /T2 IDSrcCntDst A24X B24X A34Y B34Y B35Y B27Y B39Y B25X B38X B29X C38Z IDCnt A4 B4 B5 B9 SrcDst2X 3Y

35 Copyright © 2003-2012 Curt Hill Division Implementation Do an equijoin on the two tables common columns Project away the remaining common columns

36 Copyright © 2003-2012 Curt Hill Algebra and Calculus The algebra is procedural –You specify how to do what needs to be done –Must use the operations The calculus is declarative –You say what you want without saying how to obtain this SQL has elements of both Calculus must be translated into the algebra before execution

37 Copyright © 2003-2012 Curt Hill Algebra Shortcomings Algebra is a theoretical support for database implementation It lacks most of the niceties needed for an actual implementation –Reports –Formatting –Counts –Averages All it does is deliver the data as a table

38 Copyright © 2003-2012 Curt Hill Queries using relational algebra Consider the college schema tables: –Course –Students –Grade –Faculty –Faculty_teach –Department –Division

39 Example Suppose we want to produce a grade report for students This should include information from two or three relations: –Students –Grade –Courses (depending how much information is needed) Copyright © 2003-2012 Curt Hill

40 Find student grades: Tables naidnameaddress 2156Betty Reynoldson315 4 th Ave deptnumbernaidscore CS160206786 CIS385215694

41 Find student grades: Operations Equi join on ID –Students  NAID Grades Project away unwanted fields –p name, dept, course, score –Include address if it is to be mailed Outside of the algebra –Format score as grade –Sort by zip code –Format into pages Copyright © 2003-2012 Curt Hill

42 Find all the courses taught by faculty members Equi join on ID –Faculty  NAID Faculty_teach Project away unwanted fields –p name, dept, course

43 Copyright © 2003-2012 Curt Hill Find the departmental chairs for each faculty member Equi join on ID –Faculty  NAID Departments This connects departments and chairs –p name, dept Equi join on ID –Faculty  Dept Temp Project out what is not needed Two more joins needed to include divisional chairs

44 Copyright © 2003-2012 Curt Hill Find all the students who got a B or better in any CS class Use selection to trim the grades relation to just CS acronym, ID and score greater than or equal to 80 Join this with students based on student ID equality Eliminate those columns that you do not want

45 Copyright © 2003-2012 Curt Hill Find all the students that each faculty member has Join faculty with faculty_teach on NAID Join this with grades file based on equality with both dept acronym and course number Project out what you don’t want

46 Copyright © 2003-2012 Curt Hill Find all the students who got an A in Calculus and an A in CIS 385 Select out all the grades table leaving only Calc As Join this with students on NAID Call this T1 Select out all the grades table leaving only CIS 385 As Join this with students on NAID and call it T2 Intersect T2

47 Copyright © 2003-2012 Curt Hill Find this semesters GPA of all Math students What is a Math student? –A Math major or –Any student taking any math course Is this the average score of math courses? –If so do a selection on grades table Is this the average score of all students taking any math course? –Select grades to just find Math –Join this with student on NAID –Join this with original grade file –Use report program to sort by NAID and then compute and summarize the GPA


Download ppt "Copyright © 2003-2012 Curt Hill The Relational Algebra What operations can be done?"

Similar presentations


Ads by Google