Download presentation

Presentation is loading. Please wait.

Published byMacie Luffman Modified about 1 year ago

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 21608AT DR DW KL KT BV CD DR KV29 OrderLine OrderNumPartNum 21608AT DR DW KL KT BV CD DR KV29 OrderNumPartNum 21608AT94

6
PROJECTion

7
PROJECT (cont’d) OrderNumPartNum 21608AT DR DW KL KT BV CD DR KV29 OrderLine OrderNum

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 21608AT DR DW KL62 OrderLine1 OrderNumPartNum 21614KT BV CD DR KV29 OrderLine2 OrderNumPartNum 21608AT DR DW KL KT BV CD DR KV29

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 /20/ /20/ /21/2010 PartNumDescription DR93Gas Range DW11Washer OrderNumOrderDatePartNumDescription /20/2010DR93Gas Range /20/2010DR93Gas Range /21/2010DR93Gas Range /20/2010DW11Washer /20/2010DW11Washer /21/2010DW11Washer Part Orders Product of Orders and Part

17
DIVISION

18
Division (cont’d) OrderNum PartNum DR93 DW11 OrderNumPartNum 21608AT DR DW KL KT BV CD DR KV29 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 BowserWilliam12 February 1972BIOL SmithsonAnne15 November 1977CIS BrewerJuliette23 August 1966ACCT OblonskiWalter16 September 1973CIS smithJohn30 December 1955ENGL STUDENT DEPARTMENT

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

26
Equijoin Example (cont’d) STU_NUMSTU_LNAMESTU_FNAMESTU_DOBS.DEPT_CODED.DEPT_CODEDEPT_NAME BowserWilliam12 February 1972BIOL Biology SmithsonAnne15 November 1977CIS Computer Info. Systems BrewerJuliette23 August 1966ACCT Accounting OblonskiWalter16 September 1973CIS Computer Info. Systems smithJohn30 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 WalkerM1 5RT AdaresNW6 4RT Rakowski RodriguezNW6 2WS SmithsonN4 3YP Vanloo67543W231 AGENT_CODEAGENT_PHONE

31
Natural Join Example (cont’d) Cartesian product (CUSTOMER X AGENT) C.CUS_CODEC.CUS_LNAMEC.CUS_POSTCODEC.AGENT_CODEA.AGENT_CODEA.AGENT_PHONE WalkerM1 5RT WalkerM1 5RT WalkerM1 5RT WalkerM1 5RT AdaresNW6 4RT AdaresNW6 4RT AdaresNW6 4RT AdaresNW6 4RT Rakowski Rakowski Rakowski Rakowski RodriguezNW6 2WS RodriguezNW6 2WS RodriguezNW6 2WS RodriguezNW6 2WS SmithsonN4 3YP SmithsonN4 3YP SmithsonN4 3YP SmithsonN4 3YP Vanloo67543W Vanloo67543W Vanloo67543W Vanloo67543W

32
Natural Join Example (cont’d) CUSTOMER X AGENT CUS_CODECUS_LNAMECUS_POSTCODEAGENT_CODEAGENT_PHONE WalkerM1 5RT AdaresNW6 4RT Rakowski RodriguezNW6 2WS Vanloo67543W

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 WalkerM1 5RT AdaresNW6 4RT Rakowski RodriguezNW6 2WS Vanloo67543W SmithsonN4 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 WalkerM1 5RT AdaresNW6 4RT Rakowski RodriguezNW6 2WS Vanloo67543W NULL

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 3679MR82ToyotaCorollaBlue fr89768 E-TS865NissanMicroRed Smith121 PE57UVPPeugeot407Blue byt3212 PISE567VolkswagenEosLime2006DF-678-WV ROMA482VolkswagenGolf GTBlack2007AQ-123-AV Z-BA975Peugeot207Black vrt7312 PART_NOPART_NAMEPART_COST 12390Paint sealants Gh Wiper Gh Brake pads Gh Brake Discs Gh Spark Plugs Gh Airbag Gh TyresGh25.00 CAR PART

43
Car Maintenance Example (cont’d) INSPECTION_CODEREGISTRATIONINSPECTION_DATEEVALUATION PE57UVP10/05/2008FAIL ROMA48201/09/ E-TS86501/03/2006PASS Z-BA97503/10/2008FAIL PISE56730/09/2007PASS E-TS86521/02/2005PASS E-TS86501/04/2007FAIL MAINTENANCE_RECORD INSPECTION_CODEPART_NO REPAIR

44
Car Maintenance Example (cont’d)

45
REGISTRATIONCAR_MAKECAR_MODELCAR_COLOURMODEL_YEARLINCENCE_NO PE57UVPPeugeot407Blue byt321 2 ROMA482 Volkswage n Golf GTBlack2007AQ-123-AV Z-BA975Peugeot207Black vrt7312

46
Car Maintenance Assignment 1.Display all the part names and their prices where the cost of the part is greater than Gh 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.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google