Presentation is loading. Please wait.

Presentation is loading. Please wait.

LECTURE 4 RELATIONAL ALGEBRA. Introduction Relational algebra defines the theoretical way of manipulating table contents through a number of relational.

Similar presentations


Presentation on theme: "LECTURE 4 RELATIONAL ALGEBRA. Introduction Relational algebra defines the theoretical way of manipulating table contents through a number of relational."— Presentation transcript:

1 LECTURE 4 RELATIONAL ALGEBRA

2 Introduction Relational algebra defines the theoretical way of manipulating table contents through a number of relational operators These relational operators include; ▫ SELECT (or RESTRICT) ▫ PROJECT ▫ JOIN ▫ PRODUCT ▫ INTERSECT ▫ UNION ▫ DIFFERENCE ▫ DIVIDE

3 Introduction (cont’d) The relational operators have the property of closure, i.e., relational algebra operators are used on existing tables to produce new tables The relational operators are classed as being unary or binary Unary operators such as SELECT and PROJECT, can be applied to one relation Binary operators such as JOIN are applied on two relations

4 SELECTion

5 SELECT (cont’d) OrderNumPartNum 21608AT94 21610DR93 21610DW11 21613KL62 21614KT03 21617BV06 21617CD52 21619DR93 21623KV29 OrderLine OrderNumPartNum 21608AT94 21610DR93 21610DW11 21613KL62 21614KT03 21617BV06 21617CD52 21619DR93 21623KV29 OrderNumPartNum 21608AT94

6 PROJECTion

7 PROJECT (cont’d) OrderNumPartNum 21608AT94 21610DR93 21610DW11 21613KL62 21614KT03 21617BV06 21617CD52 21619DR93 21623KV29 OrderLine OrderNum 21608 21610 21613 21614 21617 21619 21623

8 UNION The UNION set operator combines all tuples from two relations, excluding duplicate tuples. The relations must have the same attribute characteristics (the columns and domains must be identical) to be used in the UNION. When two or more tables share the same number of columns, i.e., have the same degree, and when the share the same (or compatible) domains, they are said to be union- compatible

9 UNION(cont’d)

10 OrderNumPartNum 21608AT94 21610DR93 21610DW11 21613KL62 OrderLine1 OrderNumPartNum 21614KT03 21617BV06 21617CD52 21619DR93 21623KV29 OrderLine2 OrderNumPartNum 21608AT94 21610DR93 21610DW11 21613KL62 21614KT03 21617BV06 21617CD52 21619DR93 21623KV29

11 INTERSECT

12 INTERSECT(cont’d) PartNum DR93 DW11 BV06 CD52 PartNum AT94 DR93 DW11 KL62 KT03 KV29 Part1 Part2 PartNum DR93 DW11

13 DIFFERENCE

14 DIFFERENCE (cont’d) PartNum DR93 DW11 BV06 CD52 PartNum AT94 DR93 DW11 KL62 KT03 DR93 KV29 Part1 Part2 PartNum AT94 KL62 KT03 KV29

15 CARTESIAN PRODUCT

16 CARTESIAN PRODUCT (cont’d) OrderNumOrderDate 2160810/20/2010 2161010/20/2010 2161310/21/2010 PartNumDescription DR93Gas Range DW11Washer OrderNumOrderDatePartNumDescription 2160810/20/2010DR93Gas Range 2161010/20/2010DR93Gas Range 2161310/21/2010DR93Gas Range 2160810/20/2010DW11Washer 2161010/20/2010DW11Washer 2161310/21/2010DW11Washer Part Orders Product of Orders and Part

17 DIVISION

18 Division (cont’d) OrderNum 21610 PartNum DR93 DW11 OrderNumPartNum 21608AT94 21610DR93 21610DW11 21613KL62 21614KT03 21617BV06 21617CD52 21619DR93 21623KV29 OrderLine Part

19 JOIN The JOIN operation is one of the essential operations of relational algebra. It is said to be the real power behind the relational database, allowing the use of independent tables linked by common attributes The JOIN of two relations R 1 and R 2 is a restriction on their Cartesian product R 1 XR 2 to meet a specified criterion. The join itself is defined on an attribute a of R 1 and b of R 2 where the attributes a and b share the same domain.

20 JOIN(cont’d)

21 THETA JOIN AND EQUIJOIN EQUIJOIN is on of the most commonly used joins which links tables on the basis of an equality condition that compares specified columns of each table The outcome of the equijoin does not eliminate duplicate columns, and the condition or criterion used must be explicitly defined. The equijoin takes its name from the equality comparison operator (=) used in the condition

22 THETA JOIN AND EQUIJOIN(cont’d)

23

24 Equijoin Example DEPT_CODEDEPT_NAME ACCTAccounting BIOLBiology CISComputer Info. Systems ENGLEnglish STU_NUMSTU_LNAMESTU_FNAMESTU_DOBDEPT_CODE 321452BowserWilliam12 February 1972BIOL 324257SmithsonAnne15 November 1977CIS 324258BrewerJuliette23 August 1966ACCT 324269OblonskiWalter16 September 1973CIS 324273smithJohn30 December 1955ENGL STUDENT DEPARTMENT

25 Equijoin Example (cont’d) STU_NUMSTU_LNAMESTU_FNAMESTU_DOBS.DEPT_CODED.DEPT_CODEDEPT_NAME 321452BowserWilliam12 February 1972BIOLACCTAccounting 321452BowserWilliam12 February 1972BIOL Biology 321452BowserWilliam12 February 1972BIOLCISComputer Info. Systems 321452BowserWilliam12 February 1972BIOLENGLEnglish 324257SmithsonAnne15 November 1977CISACCTAccounting 324257SmithsonAnne15 November 1977CISBIOLBiology 324257SmithsonAnne15 November 1977CIS Computer Info. Systems 324257SmithsonAnne15 November 1977CISENGLEnglish 324258BrewerJuliette23 August 1966ACCT Accounting 324258BrewerJuliette23 August 1966ACCTBIOLBiology 324258BrewerJuliette23 August 1966ACCTCISComputer Info. Systems 324258BrewerJuliette23 August 1966ACCTENGLEnglish 324269OblonskiWalter16 September 1973CISACCTAccounting 324269OblonskiWalter16 September 1973CISBIOLBiology 324269OblonskiWalter16 September 1973CIS Computer Info. Systems 324269OblonskiWalter16 September 1973CISENGLEnglish 324273smithJohn30 December 1955ENGLACCTAccounting 324273smithJohn30 December 1955ENGLBIOLBiology 324273smithJohn30 December 1955ENGLCISComputer Info. Systems 324273smithJohn30 December 1955ENGL English Cartesian product (STUDENT X DEPARTMENT)

26 Equijoin Example (cont’d) STU_NUMSTU_LNAMESTU_FNAMESTU_DOBS.DEPT_CODED.DEPT_CODEDEPT_NAME 321452BowserWilliam12 February 1972BIOL Biology 324257SmithsonAnne15 November 1977CIS Computer Info. Systems 324258BrewerJuliette23 August 1966ACCT Accounting 324269OblonskiWalter16 September 1973CIS Computer Info. Systems 324273smithJohn30 December 1955ENGL English

27 NATURAL JOIN The natural join operation is the most common variant of the joins and requires that the two operant relations must have at least one common attribute, i.e., attributes that share the same domain. The common column(s) is (are) referred to as the join column(s) The natural join is in fact an equijoin, however, in addition, the duplicate attributes are dropped with the resulting relation containing one less column than that of the equijoin

28 NATURAL JOIN(cont’d)

29

30 Natural Join Example CUSTOMER AGENT CUS_CODECUS_LNAMECUS_POSTCODEAGENT_CODE 1132445WalkerM1 5RT231 1217782AdaresNW6 4RT125 1312243Rakowski678954167 1321242RodriguezNW6 2WS125 1542311SmithsonN4 3YP421 1657399Vanloo67543W231 AGENT_CODEAGENT_PHONE 12501812439887 16701813426778 23101812431124 33301131234445

31 Natural Join Example (cont’d) Cartesian product (CUSTOMER X AGENT) C.CUS_CODEC.CUS_LNAMEC.CUS_POSTCODEC.AGENT_CODEA.AGENT_CODEA.AGENT_PHONE 1132445WalkerM1 5RT23112501812439887 1132445WalkerM1 5RT23116701813426778 1132445WalkerM1 5RT231 01812431124 1132445WalkerM1 5RT23133301131234445 1217782AdaresNW6 4RT125 01812439887 1217782AdaresNW6 4RT12516701813426778 1217782AdaresNW6 4RT12523101812431124 1217782AdaresNW6 4RT12533301131234445 1312243Rakowski67895416712501812439887 1312243Rakowski678954167 01813426778 1312243Rakowski67895416723101812431124 1312243Rakowski67895416733301131234445 1321242RodriguezNW6 2WS125 01812439887 1321242RodriguezNW6 2WS12516701813426778 1321242RodriguezNW6 2WS12523101812431124 1321242RodriguezNW6 2WS12533301131234445 1542311SmithsonN4 3YP42112501812439887 1542311SmithsonN4 3YP42116701813426778 1542311SmithsonN4 3YP42123101812431124 1542311SmithsonN4 3YP42133301131234445 1657399Vanloo67543W23112501812439887 1657399Vanloo67543W23116701813426778 1657399Vanloo67543W231 01812431124 1657399Vanloo67543W23133301131234445

32 Natural Join Example (cont’d) CUSTOMER X AGENT CUS_CODECUS_LNAMECUS_POSTCODEAGENT_CODEAGENT_PHONE 1132445WalkerM1 5RT23101812431124 1217782AdaresNW6 4RT12501812439887 1312243Rakowski67895416701813426778 1321242RodriguezNW6 2WS12501812439887 1657399Vanloo67543W23101812431124

33 OUTER JOIN When using the theta and natural join, it is possible that some tuples in the joined relations do not have identical values for the common attributes. As a result these tuples will be ‘lost’. If it is required that all tuples from the original tables be shown in the resulting relation, then it is necessary to have a join which keeps all the tuples in R 1 which have no corresponding values in R 2. The tuples in the R 2 will have null values. This type of join is known as the outer join

34 OUTER JOIN (cont’d) There are 3 common types of the outer join ▫ Left outer join – keeps data from the left-hand relation ▫ Right outer join – keeps data from the right-hand relation ▫ Full outer join – keeps data from both relations The steps for determining an outer join are very similar to those for computing a natural join, except that data from the left or right side of the relation, depending on whether one is performing a left or right outer join is included.

35 OUTER JOIN (cont’d)

36 Left Outer Join Example (cont’d) A left outer join of CUSTOMER and AGENT, will return all the tuples in the CUSTOMER relation, including those that do not have a matching value in the AGENT relation. CUS_CODECUS_LNAMECUS_POSTCODEAGENT_CODEAGENT_PHONE 1132445WalkerM1 5RT23101812431124 1217782AdaresNW6 4RT12501812439887 1312243Rakowski67895416701813426778 1321242RodriguezNW6 2WS12501812439887 1657399Vanloo67543W23101812431124 1542311SmithsonN4 3YP421NULL

37 Right Outer Join Example (cont’d) A right outer join of CUSTOMER and AGENT, will return all the tuples in the AGENT relation, including those that do not have a matching value in the CUSTOMER relation. CUS_CODECUS_LNAMECUS_POSTCODEAGENT_CODEAGENT_PHONE 1132445WalkerM1 5RT23101812431124 1217782AdaresNW6 4RT12501812439887 1312243Rakowski67895416701813426778 1321242RodriguezNW6 2WS12501812439887 1657399Vanloo67543W23101812431124 NULL 33301131234445

38 CONSTRUCTING QUERIES USING RELATIONAL ALGEBRAIC EXPRESSIONS The main purpose of relational algebra is to provide a way to create and manipulate relations (tables) in a database. During the lifetime of a database, users will ask many different kinds of queries. The task of building a query involves breaking the query down into a number of smaller steps, where each step generates a set of intermediate results which are then used in the steps of the query.

39 CONSTRUCTING QUERIES USING RELATIONAL ALGEBRAIC EXPRESSIONS The following steps should be followed when building a query using relational algebraic expressions 1.List all the attributes needed to give the answer 2.Select all the relations needed based on the list of attributes. 3.Specify the relational operators and the intermediate results that are needed

40 Building Queries Example - Car Maintenance Database In a small database that stores information about the maintenance of cars, each car is required to undergo an inspection each year to test to see if it is roadworthy. After each inspection a maintenance record is created and any repairs that are needed are recorded. A repair can require new parts to be purchased and fitted. If a car needs a repair then the EVALUATION is set to FAIL until all the repairs are completed and then it is set to PASS. The Entity Relationship Diagram (ERD) and tables are shown below.

41 Car Maintenance Example (cont’d) Car inspection ERD

42 Car Maintenance Example (cont’d) REGISTRATIONCAR_MAKECAR_MODELCAR_COLOURMODEL_YEARLINCENCE_NO 3679MR82ToyotaCorollaBlue20061967fr89768 E-TS865NissanMicroRed20041973Smith121 PE57UVPPeugeot407Blue20071990byt3212 PISE567VolkswagenEosLime2006DF-678-WV ROMA482VolkswagenGolf GTBlack2007AQ-123-AV Z-BA975Peugeot207Black20071980vrt7312 PART_NOPART_NAMEPART_COST 12390Paint sealants Gh14.95 12391Wiper Gh19.95 12392Brake pads Gh24.99 12393Brake Discs Gh49.54 12395Spark Plugs Gh0.99 12396Airbag Gh24.95 12397TyresGh25.00 CAR PART

43 Car Maintenance Example (cont’d) INSPECTION_CODEREGISTRATIONINSPECTION_DATEEVALUATION 100036PE57UVP10/05/2008FAIL 100390ROMA48201/09/2008 106750E-TS86501/03/2006PASS 122456Z-BA97503/10/2008FAIL 145678PISE56730/09/2007PASS 200450E-TS86521/02/2005PASS 200456E-TS86501/04/2007FAIL MAINTENANCE_RECORD INSPECTION_CODEPART_NO 10675012396 10675012397 10003612393 20045012391 10003612397 20045012392 20045612397 REPAIR

44 Car Maintenance Example (cont’d)

45 REGISTRATIONCAR_MAKECAR_MODELCAR_COLOURMODEL_YEARLINCENCE_NO PE57UVPPeugeot407Blue2007 1990byt321 2 ROMA482 Volkswage n Golf GTBlack2007AQ-123-AV Z-BA975Peugeot207Black20071980vrt7312

46 Car Maintenance Assignment 1.Display all the part names and their prices where the cost of the part is greater than Gh20.00 2.List the car registration and model details and part numbers for all cars where the model year is 2007, where an inspection was carried out after 01/03/2008, which resulted in a part being required for the repair.


Download ppt "LECTURE 4 RELATIONAL ALGEBRA. Introduction Relational algebra defines the theoretical way of manipulating table contents through a number of relational."

Similar presentations


Ads by Google