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:
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
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
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
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
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.
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
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
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)
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
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
Natural Join Example (cont’d) CUSTOMER X AGENT CUS_CODECUS_LNAMECUS_POSTCODEAGENT_CODEAGENT_PHONE 1132445WalkerM1 5RT23101812431124 1217782AdaresNW6 4RT12501812439887 1312243Rakowski67895416701813426778 1321242RodriguezNW6 2WS12501812439887 1657399Vanloo67543W23101812431124
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
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.
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
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
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.
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
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.
Car Maintenance Example (cont’d) Car inspection ERD
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
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.