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

Slides:



Advertisements
Similar presentations
Relational Database Operators
Advertisements

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Concepts of Database Management Seventh Edition
The Relational Database Model
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Car Maintenance Example Solution. Tables REGISTRATIONCAR_MAKECAR_MODELCAR_COLOURMODEL_YEARLINCENCE_NO 3679MR82ToyotaCorollaBlue fr89768 E-TS865NissanMicroRed Smith121.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Chapter 2 The Relational Database Model
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
The Relational Database Model
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
The Relational Database Model
3 The Relational Model MIS 304 Winter Class Objectives That the relational database model takes a logical view of data That the relational model’s.
The Relational Database Model
RELATIONAL ALGEBRA Objectives
Chapter 3 Section 3.4 Relational Database Operators
ITEC 3220M Using and Designing Database Systems Instructor: Prof. Z.Yang Course Website: 3220m.htm
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
The Relational Database Model
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Relational Algebra – Part 2
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 3 The Relational Database Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Advanced Relational Algebra & SQL (Part1 )
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
Department of Mathematics Computer and Information Science1 CS 351: Database Systems Christopher I. G. Lanclos Chapter 3: The Relational Database Model.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
3 1 Database Systems The Relational Database Model.
3 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Relational Algebra Operators (continued) Difference –Yields all.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
Week 2 Lecture The Relational Database Model Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Chapter (6) The Relational Algebra and Relational Calculus Objectives
COMP3017 Advanced Databases
The Relational Database Model
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 4 Relational Model Characteristics
Lecture 2 The Relational Model
Theory behind the relational engine
Theory behind the relational engine
Chapter 3 The Relational Database Model
The Relational Algebra and Relational Calculus
More Relational Algebra
DATABASE SYSTEM.
The Relational Database Model
DCT 2053 DATABASE CONCEPT Chapter 2.2 CONTINUE
Database Systems: Design, Implementation, and Management
Chapter 4 Relational Algebra
Relational Database Operators
Presentation transcript:

LECTURE 4 RELATIONAL ALGEBRA

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

SELECTion

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

PROJECTion

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

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

UNION(cont’d)

OrderNumPartNum 21608AT DR DW KL62 OrderLine1 OrderNumPartNum 21614KT BV CD DR KV29 OrderLine2 OrderNumPartNum 21608AT DR DW KL KT BV CD DR KV29

INTERSECT

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

DIFFERENCE

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

CARTESIAN PRODUCT

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

DIVISION

Division (cont’d) OrderNum PartNum DR93 DW11 OrderNumPartNum 21608AT DR DW KL KT BV CD DR KV29 OrderLine 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.

JOIN(cont’d)

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

THETA JOIN AND EQUIJOIN(cont’d)

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

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)

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

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(cont’d)

Natural Join Example CUSTOMER AGENT CUS_CODECUS_LNAMECUS_POSTCODEAGENT_CODE WalkerM1 5RT AdaresNW6 4RT Rakowski RodriguezNW6 2WS SmithsonN4 3YP Vanloo67543W231 AGENT_CODEAGENT_PHONE

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

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

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.

OUTER JOIN (cont’d)

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

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

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 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

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

Car Maintenance Example (cont’d)

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

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.