+ All Categories
Home > Documents > Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

Date post: 11-Jan-2016
Category:
Upload: rumor
View: 65 times
Download: 0 times
Share this document with a friend
Description:
Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra). Database Systems: Design, Implementation, and Management 7th Edition Peter Rob & Carlos Coronel. What is Relational Algebra?. Part of Relational DB Theory - PowerPoint PPT Presentation
27
3 Chapter 3 Section 3.4 Relational Database Operator (Relational Algebra) Database Systems: Design, Implementation, and Managem 7th Edition Peter Rob & Carlos Coronel
Transcript
Page 1: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33Chapter 3 Section 3.4Relational Database Operators(Relational Algebra)

Database Systems: Design, Implementation, and Management 7th Edition

Peter Rob & Carlos Coronel

Page 2: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

What is Relational Algebra?What is Relational Algebra?

Part of Relational DB Theory Operations that any RDBMS should provide for data

manipulation

NOT directly included in products; capabilities generally provided via QBE or SQL

Page 3: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

What is included in RA?What is included in RA?

Set Operations Operations specific to RDBs Recent Advanced Add-Ons All RA operations work on one or more relations, and

produce a relation as a result

Page 4: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

UNIONUNION

Produces a resulting relation that contains a tuple for every tuple in either or both of two input relations (duplicates only occur once)

The Relations being combined must be union-compatible (type-compatible)

e.g. CurrentEnrollments U HistoricalEnrollments; MailListFromSierraClub U MailListFromAudabonSoc;

Page 5: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

INTERSECTIONINTERSECTION

Produces a resulting relation that contains a tuple for every tuple in BOTH of two input relations

The Relations being combined must be union-compatible (type-compatible)

MailListFromSierraClub INTERSECT MailListFromNewBabyMag;

Page 6: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

SET DIFFERENCE (MINUS)SET DIFFERENCE (MINUS)

Produces a resulting relation that contains a tuple for every tuple in FIRST of two input relations AND NOT IN the second.

The Relations being combined must be union-compatible (type-compatible)

MailListFromMarketingCompany - CurrentCustomerList;

Page 7: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

CARTESIAN PRODUCT(TIMES)

CARTESIAN PRODUCT(TIMES)

Produces a resulting relation that contains all attributes in either input relation and a tuple for every possible combination of tuples in two input relations.

The Relations being combined must be product-compatible

BY ITSELF, not usually useful in the real world

Page 8: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Relational Database OperatorsRelational Database Operators PRODUCT produces a list of all possible

pairs of rows from two tables.

(old) Figure 2.8 PRODUCT

Page 9: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

RESTRICT/SELECTRESTRICT/SELECT

Produces a resulting relation, containing only the tuples that meet some condition (hence a “horizontal” subset of the original relation)

e.g. employees in department #4, students majoring in CS, students with a GPA < 2.0

Page 10: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

PROJECTionPROJECTion

Produces a resulting relation, containing only the attributes that are requested (hence a “vertical” subset of the original relation)

e.g. last name, first name and salary of employees; last name, major, year of students; dept, class of sections

Page 11: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Relational Database OperatorsRelational Database Operators

JOIN allows us to combine information from two or more tables.

JOIN is the real power behind the relational database, allowing the use of independent tables linked by common attributes.

Page 12: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

JOINSJOINS

Produces a resulting relation that contains all attributes in either input relation and a tuple for “every possible combination of tuples in two input relations that meet some condition”.

Page 13: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Relational Database OperatorsRelational Database Operators Natural JOIN links tables by selecting only

the rows with common values in their common attribute(s). It is the result of a three-stage process:

A PRODUCT of the tables is created. (Figure 3.12)

A SELECT is performed on the output of the first step to yield only the rows for which the common attribute values match. (Figure 3.13)

A PROJECT is performed to yield a single copy of each attribute, thereby eliminating the duplicate column. (Figure 3.14)

Page 14: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Natural Join, Step 1: PRODUCTNatural Join, Step 1: PRODUCT

Page 15: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Natural Join, Step 2: SELECTNatural Join, Step 2: SELECT

Page 16: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Natural Join, Step 3: PROJECTNatural Join, Step 3: PROJECT

Page 17: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Natural Join (continued)Natural Join (continued)

Final outcome yields table that

Does not include unmatched pairs

Provides only copies of matches

If no match is made between the table rows,

the new table does not include the unmatched row

Page 18: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Relational Database OperatorsRelational Database Operators EquiJOIN links tables based on an equality condition

that compares specified columns of each table. The outcome of the EquiJOIN does not eliminate duplicate columns and the condition or criteria to join the tables must be explicitly defined.

Theta JOIN is an equiJOIN that compares specified columns of each table using a comparison operator other than the equality comparison operator.

In an Outer JOIN, the unmatched pairs would be retained and the values for the unmatched other tables would be left blank or null.

Page 19: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Outer JoinOuter Join

Matched pairs are retained and any unmatched values in other table are left null

In outer join for tables CUSTOMER and AGENT, three scenarios are possible: Left outer join

Yields all rows in CUSTOMER table, including those that do not have a matching value in the AGENT table

Right outer join Yields all rows in AGENT table, including those that do not have

matching values in the CUSTOMER table FULL OUTER JOIN - keeps all tuples in first or second relation

even if no matching tuples

Page 20: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Left Outer JoinLeft Outer Join

Page 21: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Right Outer JoinRight Outer Join

Page 22: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

DIVISIONDIVISION

Useful for finding all X who are doing something with all Y

e.g. find all students who are taking all three of these sections: 66416, 66417,66419

book e.g. find all locations that are associated with both codes A & B

Page 23: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

Relational Database OperatorsRelational Database Operators DIVIDE typically involves the use of one single-column

table and one two-column table.

Page 24: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

A Minimally Complete Set of RA OperationsA Minimally Complete Set of RA Operations

RESTRICT, PROJECT, UNION, SET DIFFERENCE, CARTESIAN PRODUCT

Others can be derived R INTERSECT S is equivalent to (R U S) - ((R -S) U (S - R)) Theta JOIN is equivalent to cartesian product followed by restrict NATURAL JOIN is equivalent to Cartesian product preceded by

rename and followed by project and restrict

Page 25: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

EnhancementsEnhancements

Aggregate Functions - SUM, AVERAGE, MAXIMUM, MINIMUM

Aggregates within Group e.g. GROUPING BY: Dept#; COUNT SSN, AVERAGE

SALARY (Employee) - for each department give the count of # employees and the average salary

Page 26: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33While RA is not seen commercially, it is a foundation on what is available commercially. It is a commonly understood basis for comparison and for communication.

A Final Word

Page 27: Chapter 3 Section 3.4 Relational Database Operators (Relational Algebra)

33

End Relational AlgebraEnd Relational Algebra


Recommended