Your SlideShare is downloading. ×
My slide  relational algebra
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

My slide relational algebra

95
views

Published on

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
95
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Rushdi Shams, Dept of CSE, KUET 1 Database SystemsDatabase Systems Relational AlgebraRelational Algebra Version 1.0Version 1.0
  • 2. 2Rushdi Shams, Dept of CSE, KUET What is Relational AlgebraWhat is Relational Algebra  It is a collection of operations on relationsIt is a collection of operations on relations  If you apply relational algebra on some relations,If you apply relational algebra on some relations, the result will be another relationthe result will be another relation  You can say a relation- a table.You can say a relation- a table.  So, operation on tables=another tableSo, operation on tables=another table  That is what relational algebra does!That is what relational algebra does!
  • 3. 3Rushdi Shams, Dept of CSE, KUET Memory RefreshmentMemory Refreshment  A relation is a set of attributes with values for eachA relation is a set of attributes with values for each attribute such that:attribute such that:  Each attribute value must be a single value onlyEach attribute value must be a single value only (atomic).(atomic).  All values for a given attribute must be of the same typeAll values for a given attribute must be of the same type (or domain).(or domain).  Each attribute name must be unique.Each attribute name must be unique.  The order of attributes is insignificantThe order of attributes is insignificant  No two rows (tuples) in a relation can be identical.No two rows (tuples) in a relation can be identical.  The order of the rows (tuples) is insignificant.The order of the rows (tuples) is insignificant.
  • 4. 4Rushdi Shams, Dept of CSE, KUET 2 types of operation2 types of operation 1.1. Set theory operations:Set theory operations: Union, Intersection, Difference and CartesianUnion, Intersection, Difference and Cartesian product.product. 2.2. Specific Relational Operations:Specific Relational Operations: Selection, Projection, Join, DivisionSelection, Projection, Join, Division We will take a look into set theory operations,We will take a look into set theory operations, and then we will dig into specific relationaland then we will dig into specific relational operationsoperations
  • 5. 5Rushdi Shams, Dept of CSE, KUET Our example tablesOur example tables  There are 2 tables- R andThere are 2 tables- R and S, we will play withS, we will play with them!!them!!
  • 6. 6Rushdi Shams, Dept of CSE, KUET UnionUnion  RUS means, a unionRUS means, a union operation on R and Soperation on R and S that will produce anotherthat will produce another table which will have alltable which will have all the rows of R and Sthe rows of R and S except the duplicatesexcept the duplicates  There are 2 Sally in RThere are 2 Sally in R and S, so, just take 1and S, so, just take 1 from themfrom them
  • 7. 7Rushdi Shams, Dept of CSE, KUET DifferenceDifference  R-S means, a differenceR-S means, a difference operation on R and Soperation on R and S that will produce anotherthat will produce another table which will have alltable which will have all the rows which is in Rthe rows which is in R but not in S.but not in S.  As Sally is in R and in S,As Sally is in R and in S, so, she has been omitted.so, she has been omitted.
  • 8. 8Rushdi Shams, Dept of CSE, KUET IntersectionIntersection  R∩S means, anR∩S means, an intersection operation onintersection operation on R and S that will produceR and S that will produce another table which willanother table which will have all the rows whichhave all the rows which are in both R and S.are in both R and S.  As Sally is in R and in S,As Sally is in R and in S, so, she has been selected.so, she has been selected.
  • 9. 9Rushdi Shams, Dept of CSE, KUET More UnionMore Union
  • 10. 10Rushdi Shams, Dept of CSE, KUET More IntersectionMore Intersection
  • 11. 11Rushdi Shams, Dept of CSE, KUET More DifferenceMore Difference
  • 12. 12Rushdi Shams, Dept of CSE, KUET ConstraintsConstraints  You see tables, and start performing set theoryYou see tables, and start performing set theory operations on them- MISTAKE!! You shouldoperations on them- MISTAKE!! You should remember the following constraints before youremember the following constraints before you perform set theory operations on tables.perform set theory operations on tables. 1.1. Attributes of relations needAttributes of relations need notnot be identical tobe identical to perform union, intersection and differenceperform union, intersection and difference operations.operations. 2.2. However, theyHowever, they must havemust have the same number ofthe same number of attributes and the domains for correspondingattributes and the domains for corresponding attributes must be identical.attributes must be identical.
  • 13. 13Rushdi Shams, Dept of CSE, KUET Other things to rememberOther things to remember  Union, Intersection and difference operatorsUnion, Intersection and difference operators may only be applied to Union Compatiblemay only be applied to Union Compatible relations.relations.  Union and Intersection are commutativeUnion and Intersection are commutative operationsoperations RUS= SURRUS= SUR R∩S = S∩RR∩S = S∩R  Difference operation isDifference operation is NOTNOT commutative.commutative. R - S not equal S - RR - S not equal S - R
  • 14. 14Rushdi Shams, Dept of CSE, KUET Class PerformanceClass Performance  Find out RUTFind out RUT  Find out R∩TFind out R∩T  Find out R-TFind out R-T  Find out T-RFind out T-R
  • 15. 15Rushdi Shams, Dept of CSE, KUET Cartesian ProductCartesian Product  It provides all the combinations of rows from two tables.It provides all the combinations of rows from two tables.  If A and B are 2 tables and A has 3 rows and B has 4 rows, then AIf A and B are 2 tables and A has 3 rows and B has 4 rows, then A×B is-×B is- A1-B1A1-B1 A1-B2A1-B2 A1-B3A1-B3 A1-B4A1-B4 A2-B1A2-B1 A2-B2A2-B2 A2-B3A2-B3 A2-B4A2-B4 A3-B1A3-B1 A3-B2A3-B2 A3-B3A3-B3 A3-B4A3-B4
  • 16. 16Rushdi Shams, Dept of CSE, KUET Cartesian Product (continued)Cartesian Product (continued)
  • 17. 17Rushdi Shams, Dept of CSE, KUET More ProductMore Product
  • 18. 18Rushdi Shams, Dept of CSE, KUET SelectionSelection  Selection and Projection areSelection and Projection are unaryunary operators.operators.  The selection operator isThe selection operator is sigma (sigma (σσ))  The selection operation acts like a filter on aThe selection operation acts like a filter on a relation byrelation by returning only a certain number ofreturning only a certain number of rows.rows.  TheThe resulting relation will have the sameresulting relation will have the same number of attributesnumber of attributes as the original relation.as the original relation.
  • 19. 19Rushdi Shams, Dept of CSE, KUET Selection (continued)Selection (continued)  The resulting relationThe resulting relation maymay have fewer rows thanhave fewer rows than the original relation.the original relation.  The rows to be returned are dependent on aThe rows to be returned are dependent on a condition that is part of the selection operator.condition that is part of the selection operator.  σσCC (R) Returns only those rows in R that satisfy(R) Returns only those rows in R that satisfy conditioncondition CC  A condition C can be made up of anyA condition C can be made up of any combination of comparison or logical operatorscombination of comparison or logical operators that operate on the attributes of R.that operate on the attributes of R.
  • 20. 20Rushdi Shams, Dept of CSE, KUET Selection ExampleSelection Example  We have a table named EMP.We have a table named EMP.  We will play with this table named EMP for a while!We will play with this table named EMP for a while!
  • 21. 21Rushdi Shams, Dept of CSE, KUET Selection Example (continued)Selection Example (continued)  Select only those Employees in the CS departmentSelect only those Employees in the CS department σσ Dept=‘CS’Dept=‘CS’ (EMP)(EMP)
  • 22. 22Rushdi Shams, Dept of CSE, KUET Selection Example (continued)Selection Example (continued)  Select only those Employees with last name Smith whoSelect only those Employees with last name Smith who are assistant professorsare assistant professors σσ Name=‘Smith’Name=‘Smith’ ΛΛ Rank=‘Assistant’Rank=‘Assistant’ (EMP)(EMP)
  • 23. 23Rushdi Shams, Dept of CSE, KUET Selection Example (continued)Selection Example (continued)  Select only those Employees who are either AssistantSelect only those Employees who are either Assistant Professors or in the Economics departmentProfessors or in the Economics department σσ Dept =‘Econ’V Rank=‘Assistant’Dept =‘Econ’V Rank=‘Assistant’ (EMP)(EMP)
  • 24. 24Rushdi Shams, Dept of CSE, KUET Selection Example (continued)Selection Example (continued)  Select only those Employees who are not in the CSSelect only those Employees who are not in the CS department or Adjunctsdepartment or Adjuncts σσ ¬¬(Dept=‘CS’ V Rank=‘Adjunct’)(Dept=‘CS’ V Rank=‘Adjunct’) (EMP)(EMP)
  • 25. 25Rushdi Shams, Dept of CSE, KUET More SelectionMore Selection
  • 26. 26Rushdi Shams, Dept of CSE, KUET Class PerformanceClass Performance  σσ(Rank='Adjunct'(Rank='Adjunct' ΛΛ Dept ='CS')Dept ='CS') (EMP)(EMP)  σσ Rank= 'Associate'Rank= 'Associate' ΛΛ Dept ='CS'Dept ='CS' EMP )EMP )  σσ Dept= 'CS'Dept= 'CS' VV Rank='Associate'Rank='Associate' EMP )EMP )  σσ Age>26Age>26 (R U S)(R U S)
  • 27. 27Rushdi Shams, Dept of CSE, KUET ProjectionProjection  Projection is also aProjection is also a UnaryUnary operator.operator.  The Projection operator isThe Projection operator is pi (pi (ππ))  Projection limits the columns that will be returned from theProjection limits the columns that will be returned from the original relation.original relation.  The general syntax is:The general syntax is: ππ attributesattributes RR Where attributes is the list of attributes to be displayed and R isWhere attributes is the list of attributes to be displayed and R is the relation.the relation.  The resulting table will have the same number of rows as theThe resulting table will have the same number of rows as the original relation (unless there are duplicate rows produced).original relation (unless there are duplicate rows produced).  The number of columns of the resulting relation may be equal toThe number of columns of the resulting relation may be equal to or less than that of the original relation.or less than that of the original relation.
  • 28. 28Rushdi Shams, Dept of CSE, KUET Projection ExampleProjection Example  Project only the names and departments of theProject only the names and departments of the employeesemployees ππ name,deptname,dept (EMP)(EMP)
  • 29. 29Rushdi Shams, Dept of CSE, KUET Combining selection &Combining selection & projectionprojection  Show the names of all employees working in the CSShow the names of all employees working in the CS departmentdepartment ππ namename ((σσ Dept ='CS'Dept ='CS' (EMP) )(EMP) )
  • 30. 30Rushdi Shams, Dept of CSE, KUET Combining selection & projectionCombining selection & projection (continued)(continued)  Show the name and rank of those Employees who areShow the name and rank of those Employees who are not in the CS department or Adjunctsnot in the CS department or Adjuncts ππ name, rankname, rank (( σσ ¬¬(Rank='Adjunct' V Dept='CS')(Rank='Adjunct' V Dept='CS') (EMP) )(EMP) )
  • 31. 31Rushdi Shams, Dept of CSE, KUET More ProjectionMore Projection
  • 32. 32Rushdi Shams, Dept of CSE, KUET Class PerformanceClass Performance  ππ name, rankname,rank (( σσ ¬¬(Rank='Adjunct'(Rank='Adjunct' ΛΛ Dept ='CS')Dept ='CS') (EMP) )(EMP) )  ππ fname,agefname,age ((σσ Age>22Age>22 (R U S) )(R U S) )  σσ office>300office>300 (( ππ name,rankname,rank (EMP))(EMP))
  • 33. 33Rushdi Shams, Dept of CSE, KUET Aggregate FunctionsAggregate Functions  We can also apply Aggregate functions to columnsWe can also apply Aggregate functions to columns and rowsand rows 1.1. SUMSUM 2.2. MINIMUMMINIMUM 3.3. MAXIMUMMAXIMUM 4.4. AVERAGE, MEAN, MEDIANAVERAGE, MEAN, MEDIAN 5.5. COUNTCOUNT  Aggregate functions are sometimes written using theAggregate functions are sometimes written using the Projection operator or theProjection operator or the Tau character (Tau character (ττ))
  • 34. 34Rushdi Shams, Dept of CSE, KUET Aggregate Functions (continued)Aggregate Functions (continued)  Find the minimum SalaryFind the minimum Salary ττ MIN(salary)MIN(salary) (EMP)(EMP)  Find the average SalaryFind the average Salary ττ AVG(salaryAVG(salary) (EMP)) (EMP)  Count the number of employees in the CS departmentCount the number of employees in the CS department ττ COUNT(name)COUNT(name) (( σσ Dept='CS'Dept='CS' (EMP) )(EMP) )  Find the total payroll for the Economics departmentFind the total payroll for the Economics department ττ SUM(salary)SUM(salary) ((σσ Dept='Econ'Dept='Econ' (EMP) )(EMP) )
  • 35. 35Rushdi Shams, Dept of CSE, KUET JoinJoin  Join operations bring together two tables and combineJoin operations bring together two tables and combine their rows and columns in a specific fashion.their rows and columns in a specific fashion.  The generic join operator called theThe generic join operator called the Theta JoinTheta Join isis  It takes as arguments the columns from the two tablesIt takes as arguments the columns from the two tables that are to be joined.that are to be joined.  The join condition can beThe join condition can be  When the join condition operator is  When the join condition operator is   ==   then we call  then we call this an Equijointhis an Equijoin  Note that the columns in common are repeated.Note that the columns in common are repeated.
  • 36. 36Rushdi Shams, Dept of CSE, KUET Join (continued)Join (continued)  Now, we have two tables Emp and depart.Now, we have two tables Emp and depart.  We will play with these 2 tables for a while!We will play with these 2 tables for a while!
  • 37. 37Rushdi Shams, Dept of CSE, KUET Join (continued)Join (continued)  Find all information on every employee including theirFind all information on every employee including their department infodepartment info EMPEMP emp.Dept=depart.Deptemp.Dept=depart.Dept DEPARTDEPART
  • 38. 38Rushdi Shams, Dept of CSE, KUET Join (continued)Join (continued)  Take a look, one table has 5 rows, the other has 4 rows,Take a look, one table has 5 rows, the other has 4 rows, the resultant has 5 rows!the resultant has 5 rows!
  • 39. 39Rushdi Shams, Dept of CSE, KUET Join (continued)Join (continued)  Find all information on every employee including departmentFind all information on every employee including department info where the employee works in an office numbered less thaninfo where the employee works in an office numbered less than the department main officethe department main office EMPEMP (emp.office<depart.mainoffice)(emp.office<depart.mainoffice) ΛΛ (emp.dept =depart.dept)(emp.dept =depart.dept) DEPARTDEPART
  • 40. 40Rushdi Shams, Dept of CSE, KUET Natural JoinNatural Join  Notice in the generic (Theta) join operation, anyNotice in the generic (Theta) join operation, any attributes in common (such as dept above) areattributes in common (such as dept above) are repeated.repeated.  The Natural Join operation removes theseThe Natural Join operation removes these duplicate attributes.duplicate attributes.  The natural join operator is:The natural join operator is: **  We can also assume usingWe can also assume using ** that the jointhat the join condition will becondition will be == on the two attributes inon the two attributes in common.common.
  • 41. 41Rushdi Shams, Dept of CSE, KUET Natural Join (continued)Natural Join (continued)  Emp * departEmp * depart  There are 2 Dept columns, one is omitted!There are 2 Dept columns, one is omitted!
  • 42. 42Rushdi Shams, Dept of CSE, KUET Outer JoinOuter Join  In the Join operations so far, only those tuples from bothIn the Join operations so far, only those tuples from both relations that satisfy the join condition are included in therelations that satisfy the join condition are included in the output relation.output relation.  The Outer join includes other rows as well according to a fewThe Outer join includes other rows as well according to a few rules.rules.  Three types of outer joins:Three types of outer joins: 1.1. Left Outer Join includes all rows in the left hand relation andLeft Outer Join includes all rows in the left hand relation and includes only those matching rows from the right hand relation.includes only those matching rows from the right hand relation. 2.2. Right Outer Join includes all rows in the right hand relationRight Outer Join includes all rows in the right hand relation and includes only those matching rows from the left handand includes only those matching rows from the left hand relation.relation. 3.3. Full Outer Join includes all rows in the left hand relation andFull Outer Join includes all rows in the left hand relation and from the right hand relation.from the right hand relation.
  • 43. 43Rushdi Shams, Dept of CSE, KUET Outer Join (continued)Outer Join (continued)  Now we have another 2 tables.Now we have another 2 tables.  We will play with them for next few slides!We will play with them for next few slides!
  • 44. 44Rushdi Shams, Dept of CSE, KUET Left Outer JoinLeft Outer Join PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
  • 45. 45Rushdi Shams, Dept of CSE, KUET Right Outer JoinRight Outer Join PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
  • 46. 46Rushdi Shams, Dept of CSE, KUET Full Outer JoinFull Outer Join PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
  • 47. 47Rushdi Shams, Dept of CSE, KUET Outer UnionOuter Union  In this case, just go from row of table 1 to row of table 2.In this case, just go from row of table 1 to row of table 2.  Take common columns just once.Take common columns just once.  if matching criteria don’t match, then put NULL, else include inif matching criteria don’t match, then put NULL, else include in the tablethe table  Repeat the same but this time go from row of table 2 to row ofRepeat the same but this time go from row of table 2 to row of table 1table 1
  • 48. 48Rushdi Shams, Dept of CSE, KUET ReferencesReferences  Database Management Systems by Prof.Database Management Systems by Prof. Holowczak, Zicklin School of Business,Holowczak, Zicklin School of Business, Baruch College, City University of NewBaruch College, City University of New YorkYork  Database Systems: Design, ImplementationDatabase Systems: Design, Implementation & Management by Rob & Coronel, 6& Management by Rob & Coronel, 6thth EditionEdition