A l Ain University Of Science and Technology 6 Handout(6) Database Management Principles and Applications Relational algebra http://alainauh.webs.com/ 1
Handout Outines 6 Basic operations in relational algebra: Selection, Projection, Cartesian product, Union, Difference. 2
Introduction Relational algebra is formal language associated with the relational model. Informally, relational algebra is a (high-level) procedural language It defines the theoretical way of manipulating table contents using the eight relational operators: SELECT, JOIN, INTERSECT, UNION and DIVIDE 3
Relational Algebra Relational algebra operations work on one or more relations to define another relation without changing the original relations. Both operands and results are relations, so output from one operation can become input to another operation. 4
Relational Algebra Five basic operations in relational algebra: Selection, Projection, Cartesian product, Union, and Set Difference. These perform most of the data retrieval operations needed. Also have Join, Intersection, and Division operations, which can be expressed in terms of 5 basic operations. 5
UNION Denoted by Union Combines all rows from two tables (relations) excluding duplicate rows. Example R S Includes all tuples that are either in R or in S or in both R and S Duplicate tuples eliminated 6
UNION The tables must have the same attributed characteristics (the column and domain must be identical) to be used in the UNION. When two or more tables share the same column and domains, they are said to be unioncompatible The effect of UNION is shown in next slide 7
UNION 6 8
INTERSECT Denoted by Yields only the rows that appear in both tables. Example R S Includes all tuples that are in both R and S The tables must be union-compatible to yield valid result. For example You can not use INTERSECT if one of the attributes is numaric and one is character-based. The effect of INTERSECT is shown in next slide. 9
INTERSECT 6 10
DIFFERENCE Denoted by DIFFERENCE (or MINUS) yields all rows in one table that are not found in the other table. It subtract one table from the other. Example R S Includes all tuples that are in R but not in S The tables must be union-compatible to yield valid results. The effect of DIFFERENCE is shown in next slide 11
DIFFERENCE 6 12
PRODUCT Denoted by PRODUCT yields all possible pairs of rows from two tables also known as the Cartesian product. Relations do not have to be union compatible If one table has 6 rows and the other table has 3 rows, the PRODUCT yields a list composed of 6 X 3=18 rows. The effect of PRODUCT is shown in next slide 13
PRODUCT 6 14
SELECT SELECT yields values for all rows found in a table. The select operation also subset of the tuples from a relation that satisfies a selection condition: SELECT can be used to list either all of row values, or it can yield only those row values that match a specified criterion. The effect of SELECT is shown in next slide 15
SELECT 6 16
SELECT Example: <selection condition> applied independently to each individual tuple t in R If condition evaluates to TRUE, tuple selected 17
PROJECT PROJECT yields a vertical subset of a table. Selects columns from table and discards the other columns: The effect of PROJECT shown in the next slide 18
PROJECT 19
JOIN 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. 20
Natural JOIN Denoted by * Natural JOIN linked tables by selecting only the rows with common values in their common attribute (s). Natural join is the result of three-stage process. We will use the tables in the following figure to illustrate JOIN 21
Natural JOIN 22
Step A, Natural JOIN a product of the tables is created as shown in the following figure. 23
Step B Natural JOIN A SELECT is performed on the output of step A to yield only the rows for which the AGENT- CODE values are equal. The common column(s) are referred to as join column(s). Step B yields the results shown in the next slide. 24
Natural JOIN 25
Natural JOIN Step C A project is performed on the results of step B to yield a single copy of each attribute, thereby eliminating duplicate columns. Step C yields the results shown in the next slide. 26
Natural JOIN 27
DIVIDE Denoted by Requires the use of one single-column table and one two column table. Using the example shown next slide note that: 1. Table 1 is divide by table 2, table 1 and 2 both contain column CODE. But do not share LOC. 2. To be included in the resulting table 3, a value in the unshared column LOC must be associated in the dividing table 2 with every value in table 1. 3. The only value associated with both A and B is 5. 28
DIVIDE 29
Notations Summary 6 30
References Text Book: Database Systems: Design, Implementation, & Management, Authors : Peter Rob, Carlos Coronel, Text Book: Course Technology Cengage Learning, Sixth Edition, 2004. ISBN 0-619-21323-x Text Book: Fundamentals of Database System, Sixth Edition, Authors: Elmasri, Navathe 31