dbms ii mca-ch5-ch6-relational algebra-2013
TRANSCRIPT
![Page 1: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/1.jpg)
Aruna Devi.C (DSCASC) 1
Data Base Management System[DBMS]
Chapter 5Unit III
Relational Algebra
![Page 2: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/2.jpg)
Relational Algebra
Historically, the relational algebra and calculus were developed before the SQL language.
In fact, in some ways, SQL is based on concepts from both the algebra and the calculus.
The algebra defines a set of operations for the relational model.
A relational calculus expression creates a new relation.
Aruna Devi.C (DSCASC)
![Page 3: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/3.jpg)
The relational algebra is often considered to be an integral part of the relational data model.
Its operations can be divided into two groups:
First group - set operations from mathematical set theory; these are applicable because each relation is defined to be a set of tuples in the formal relational model.
Set operations include UNION, INTERSECTION, SET DIFFERENCE, and CARTESIAN PRODUCT (also known as CROSS PRODUCT).
Second group - consists of operations developed specifically for relational databases—these include SELECT, PROJECT, and JOIN, among others.
Relational Algebra (cont.,)
Aruna Devi.C (DSCASC)
![Page 4: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/4.jpg)
Relational Algebra
Six basic operators:Select: - Selects tuples that satisfy a given
predicateProject: - Projects specified fields.Union: Intersection: Set difference: – Cartesian product: x Rename: Division: ÷ Joints
The operators take one or two relations as inputs and produce a new relation as a result.
4Aruna Devi.C (DSCASC)
![Page 5: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/5.jpg)
Unary Relational Operations SELECT and PROJECTThe SELECT Operation:
The SELECT operation is used to choose a subset of the tuples from a relation that satisfies a selection condition.
The SELECT operation is denoted by σ <selection condition>
(R) where the symbol σ (sigma) is used to denote the SELECT operator.
The selection condition is a Boolean expression (condition) specified on the attributes of relation R.
σ Dno=4(EMPLOYEE)
σ Salary>30000(EMPLOYEE)
Where Dno is Department No.
<comparison op> is normally one of the operators {=, <, ≤, >, ≥, ≠}. Clauses can be connected by the standard Boolean operators and, or, and
not to form a general selection condition.
![Page 6: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/6.jpg)
Select Operation
Notation: p(r) p is called the selection predicate Defined as:
P(r) = {t | t r and p(t)}
Where p is a formula connected by : (and), (or), (not)
Example of selection:
branch_name=“Perryridge”(account)
6Aruna Devi.C (DSCASC)
![Page 7: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/7.jpg)
Select Operation – Example
7
Relation rA B C D
1
5
12
23
7
7
3
10
A=B ^ D > 5 (r)A B C D
1
23
7
10
Aruna Devi.C (DSCASC)
![Page 8: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/8.jpg)
The PROJECT Operation:
The PROJECT operation, selects certain columns from the table and discards the other columns.
The SELECT operation chooses some of the rows from the table while discarding other rows.
The general form of the PROJECT operation is π <attribute list>
(R)
where π (pi) is the symbol used to represent the PROJECT operation, and <attribute list> is the desired sublist of attributes from the attributes of relation R.
For example, to list each employee’s first and last name and salary, we can use the PROJECT operation as follows:
π Lname, Fname, Salary(EMPLOYEE)
Unary Relational Operations SELECT and PROJECT
![Page 9: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/9.jpg)
Project Operation
Notation:
where A1, A2 are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained by erasing the columns that are not listed.
Duplicate rows removed from result, since relations are setsEg: To eliminate the branch_name attribute of account
account_number, balance (account)
9
)( ,,, 21r
kAAA
Aruna Devi.C (DSCASC)
![Page 10: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/10.jpg)
Project Operation – Example
Relation r:
10
A B C
10
20
30
40
1
1
1
2
A C
1
1
1
2
=
A C
1
1
2
A,C (r)
Aruna Devi.C (DSCASC)
![Page 11: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/11.jpg)
SELECT and PROJECT
Aruna Devi.C (DSCASC)
Company Database
![Page 12: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/12.jpg)
SELECT and PROJECT
In SQL, the SELECT condition is typically specified in the WHERE clause of a query.
For example, the following operation: σDno=4 AND Salary>25000 (EMPLOYEE)
would correspond to the following SQL query:
SELECT * FROM EMPLOYEE WHERE Dno=4 AND Salary>25000;
Aruna Devi.C (DSCASC)
Company Database
![Page 13: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/13.jpg)
Sequences of Operations
To retrieve the first name, last name, and salary of all employees who work in department number 5, we must apply a SELECT and a PROJECT operation.
We can write a single relational algebra expression, also known as an in-line expression, as follows:
π Fname, Lname, Salary(σDno=5(EMPLOYEE))
Next page shows the result of this in-line relational algebra expression.
Aruna Devi.C (DSCASC)
![Page 14: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/14.jpg)
Sequences of Operations
π Fname, Lname, Salary(σDno=5(EMPLOYEE))
Aruna Devi.C (DSCASC)
Company Database
![Page 15: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/15.jpg)
Sequences of Operations
To rename the attributes in a relation, we simply list the new attribute names in parentheses, as in the following example:
TEMP ← σDno=5(EMPLOYEE)
R(First_name, Last_name, Salary) ← πFname, Lname, Salary(TEMP)
If no renaming is applied, the names of the attributes in the resulting relation of a
SELECT operation are the same as those in the original relation and in the same
order.
For a PROJECT operation with no renaming, the resulting relation has the same attribute names as those in the projection list and in the same order in which they appear in the list.
Aruna Devi.C (DSCASC)
![Page 16: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/16.jpg)
Sequences of Operations
TEMP ← σDno=5(EMPLOYEE)
R(First_name, Last_name, Salary) ← πFname, Lname, Salary(TEMP)
Aruna Devi.C (DSCASC)
Company Database
![Page 17: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/17.jpg)
Rename Operation
Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
Allows us to refer to a relation by more than one name.
Example: x (E)
returns the expression E under the name X
If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A1 , A2 , …., An .
17
)(),...,,( 21E
nAAAx
Aruna Devi.C (DSCASC)
![Page 18: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/18.jpg)
Rename Operation
In SQL, a single query typically represents a complex relational algebra expression.
Renaming in SQL is accomplished by aliasing using AS, as in the following example:
SELECT E.Fname AS First_name, E.Lname AS Last_name, E.Salary AS Salary FROM EMPLOYEE AS E
WHERE E.Dno=5,
Aruna Devi.C (DSCASC)
Company Database
![Page 19: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/19.jpg)
Union Operation – Example
Relations r, s:
19
r s:
A B
1
2
1
A B
2
3
rs
A B
1
2
1
3
Aruna Devi.C (DSCASC)
![Page 20: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/20.jpg)
Relational Algebra Operations from Set Theory
The UNION, INTERSECTION, and MINUS Operations:
We can define the three operations UNION, INTERSECTION, and SET DIFFERENCE
on two union-compatible relations R and S as follows:
■ UNION: The result of this operation, denoted by R U S, is a relation that includes all tuples that are either in R or in S or in both R and S.
Duplicate tuples are eliminated.
■ INTERSECTION: The result of this operation, denoted by R ∩ S, is a relation that includes all tuples that are in both R and S.
■ SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S, is a relation that includes all tuples that are in R but not in S.
Aruna Devi.C (DSCASC)
![Page 21: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/21.jpg)
Union Operation
Notation: r s Defined as:
r s = {t | t r or t s} For r s to be valid.
1. r, s must have the same arity (same number of attributes)2. The attribute domains must be compatible (example: 2nd column of r deals with the same type of values as does the 2ndcolumn of s)
Example: to find all customers with either an account or a
loan
customer_name (depositor) customer_name (borrower)
21Aruna Devi.C (DSCASC)
![Page 22: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/22.jpg)
Set Difference Operation
Relations r, s:
22
r – s:
A B
1
2
1
A B
2
3
rs
A B
1
1
Aruna Devi.C (DSCASC)
![Page 23: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/23.jpg)
Set Difference Operation
Notation r – sDefined as:
r – s = {t | t r and t s}
Set differences must be taken between compatible relations.r and s must have the same arityattribute domains of r and s must be
compatible
23Aruna Devi.C (DSCASC)
![Page 24: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/24.jpg)
Set-Intersection Operation
Notation: r sDefined as:r s = { t | t r and t s }Assume:
r, s have the same arity attributes of r and s are compatible
Note: r s = r – (r – s)
24Aruna Devi.C (DSCASC)
![Page 25: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/25.jpg)
Set-Intersection Operation
Relation r, s:
r s
25
A B
121
A B
23
r s
A B
2
Aruna Devi.C (DSCASC)
![Page 26: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/26.jpg)
The CARTESIAN PRODUCT (CROSS PRODUCT) Operation
This set operation produces a new element by combining every member (tuple) from one relation (set) with every member (tuple) from the other
relation (set). In general, the result of
R(A1, A2, ..., An) X S(B1, B2, ..., Bm) is a relation Q
Aruna Devi.C (DSCASC)
![Page 27: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/27.jpg)
Cartesian-Product Operation
27
Relations r, s:
r x s:
A B
1
2
A B
11112222
C D
1010201010102010
E
aabbaabb
C D
10102010
E
aabbr
s
Aruna Devi.C (DSCASC)
![Page 28: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/28.jpg)
Composition of Operations
Can build expressions using multiple operations Example: A=C(r x s) r x s
A=C(r x s)
28
A B
11112222
C D
1010201010102010
E
aabbaabb
A B C D E
122
101020
aab
Aruna Devi.C (DSCASC)
![Page 29: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/29.jpg)
Binary Relational Operations: JOIN and DIVISION
The JOIN Operation:
The JOIN operation, denoted by , is used to combine related tuples from two relations into single “longer” tuples.
The join operator allows the combination of two relations to form a single new relation.
The JOIN operation can be specified as a CARTESIAN PRODUCT operation followed by a SELECT operation.
Aruna Devi.C (DSCASC)
![Page 30: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/30.jpg)
Natural-Join Operation
Example:R = (A, B, C, D)S = (E, B, D)
Result schema = (A, B, C, D, E)r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
30
Notation: r s
Aruna Devi.C (DSCASC)
![Page 31: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/31.jpg)
Natural Join Operation
Relations r, s:
31
A B
12412
C D
aabab
B
13123
D
aaabb
E
r
A B
11112
C D
aaaab
E
s
r s
Aruna Devi.C (DSCASC)
![Page 32: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/32.jpg)
Variations of JOIN: The EQUIJOIN and NATURAL JOIN
The only comparison operator used is =, is called an EQUIJOIN.
NATURAL JOIN requires that the two join attributes (or each pair of join attributes) have the same name in both relations.
The equality join condition specified on these two attributes requires the values to be identical in every tuple in the result.
NATURAL JOIN — denoted by *— was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition.
Aruna Devi.C (DSCASC)
![Page 33: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/33.jpg)
COMPANY Relational database schema
![Page 34: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/34.jpg)
Join Operation
Result of Join Operation:
DEPT_MGR DEPARTMENT Mgr_ssn= ssn EMPLOYEE
![Page 35: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/35.jpg)
NATURAL JOIN
Result of two Natural Join: a) PROJ_DEPT PROJECT * DEPT
b) DEPT_LOCS DEPARTMENT * DEPT _LOCATIONS
![Page 36: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/36.jpg)
A Complete Set of Relational Algebra Operations
Relational algebra operations {σ, π, U, ρ, –, x} is a complete set.
For example, the INTERSECTION operation can be expressed by using UNION and MINUS.
A JOIN operation can be specified as a CARTESIAN PRODUCT followed by a SELECT operation.
A NATURAL JOIN can be specified as a CARTESIAN PRODUCT preceded by RENAME and followed by SELECT and PROJECT operations.
Aruna Devi.C (DSCASC)
![Page 37: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/37.jpg)
A Complete Set of Relational Algebra Operations (Cont.,)
An example is Retrieve the names of employees who work on all the projects that ‘John Smith’ works on.
First, retrieve the list of project numbers that ‘John Smith’ works on in the intermediate relation SMITH_PNOS:
Aruna Devi.C (DSCASC)
![Page 38: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/38.jpg)
Division Operation
The DIVISION operation, denoted by ÷, is useful for a special kind of query that sometimes occurs in database applications.
An example is Retrieve the names of employees who work on all the projects that ‘John Smith’ works on.
1) First, retrieve the list of project numbers that ‘John Smith’ works on in the intermediate relation SMITH_PNOS:
SMITH ← σFname=‘John’ AND Lname=‘Smith’ (EMPLOYEE)
SMITH_PNOS ← πPno (WORKS_ON Essn=Ssn SMITH)
2) Next, create a relation that includes a tuple <Pno, Essn> whenever the employee whose Ssn is Essn works on the project whose number is Pno in the intermediate relation SSN_PNOS:
SSN_PNOS ← πEssn, Pno (WORKS_ON)
38Aruna Devi.C (DSCASC)
Company Database
![Page 39: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/39.jpg)
Division Operation
3) Finally, apply the DIVISION operation to the two relations, which gives the desired employees’ Social Security numbers:
SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
RESULT ← πFname, Lname (SSNS * EMPLOYEE)
Company Database
![Page 40: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/40.jpg)
Division Operation
In general, the DIVISION operation is applied to two relations R(Z) ÷ S(X), where the attributes of R are a subset of the attributes of S.
Example: 1) The table SSN_PNOS is from works on table.
2)The table Smith-Pnos is from which project smith work-on, that is from employee table and works-on table.
3) SSNS is the final table where check which all projects smith the same projects all the other employees should work on so only two employees work for both the projects.
Aruna Devi.C (DSCASC)
![Page 41: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/41.jpg)
Operations of Relational Algebra
![Page 42: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/42.jpg)
Additional Relational Operations
1. Generalized Projection:
The generalized projection operation extends the projection operation by allowing functions of attributes to be included in the projection list.
The generalized form can be expressed as: πF1, F2, ..., Fn (R)
where F1, F2, ..., Fn are functions over the attributes in relation R and may involve arithmetic operations and constant values.
This operation is helpful when developing reports where computed values have to be produced in the columns of a query result.
Aruna Devi.C (DSCASC)
![Page 43: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/43.jpg)
Additional Relational Operations (Cont.,)
As an example, consider the relation EMPLOYEE (Ssn, Salary, Deduction, Years_service)
A report may be required to show Net Salary = Salary – Deduction, Bonus = 2000 * Years_service, and Tax = 0.25 * Salary. Then a generalized projection combined with renaming may be used as
follows:
REPORT ← ρ(Ssn, Net_salary, Bonus, Tax)(πSsn, Salary – Deduction, 2000 * Years_service, 0.25 * Salary(EMPLOYEE))
Aruna Devi.C (DSCASC)
![Page 44: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/44.jpg)
2. Aggregate Functions and Grouping:
Another type of request that cannot be expressed in the basic relational algebra is to specify mathematical aggregate functions on collections of values from the database.
Examples: Retrieving the average or total salary of all employees or the total number of employee tuples.
Common functions applied to collections of numeric values include SUM, AVERAGE, MAXIMUM, MINIMUM and COUNT.
Another common type of request involves grouping the tuples in a relation.
Example: Group EMPLOYEE tuples by Dno.
Additional Relational Operations (Cont.,)
Aruna Devi.C (DSCASC)
![Page 45: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/45.jpg)
Additional Relational Operations (Cont.,)
We can define an AGGREGATE FUNCTION operation, using the symbol .ℑ
<grouping attributes> <function list> (ℑ R)
Example:
Aruna Devi.C (DSCASC)
![Page 46: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/46.jpg)
Additional Relational Operations (Cont.,)
3. Recursive Closure Operations:
This operation is applied to a recursive relationship between tuples of the same type, such as the relationship between an employee and a supervisor.
Aruna Devi.C (DSCASC)
![Page 47: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/47.jpg)
Additional Relational Operations (Cont.,)
4. OUTER JOIN Operations:
The JOIN operations described earlier match tuples that satisfy the join condition.
For example, for a NATURAL JOIN operation R * S, only tuples from R that have matching tuples in S—and vice versa—appear in the result.
Hence, tuples without a matching (or related) tuple are eliminated from the JOIN result.
Tuples with NULL values in the join attributes are also eliminated.
This type of join, where tuples with no match are eliminated, is known as an inner join.
There is an amount of loss in information.
Aruna Devi.C (DSCASC)
![Page 48: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/48.jpg)
Additional Relational Operations (Cont.,)
Different JOINs:
Join: Return rows when there is at least one match in both tables.
Left Outer Join: Return all rows from the first or left table, even if there are no matches in the right table. R S
Right Outer Join: Return all rows from the second or right table, even if there are no matches in the left table. R S
Full Outer Join: Return rows when there is a match in one of the tables.
R S
Aruna Devi.C (DSCASC)
![Page 49: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/49.jpg)
Examples of Queries in Relational Algebra
![Page 50: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/50.jpg)
Examples of Queries in Relational Algebra (Cont.,)
Query 1. Retrieve the name and address of all employees who work for the ‘Research’ department.
Query 2. For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date.
we first select the projects located in Stafford, then join them with their controlling departments, and then join the result with the department managers. Finally, we apply a project operation on the desired attributes.
![Page 51: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/51.jpg)
Query 3. Find the names of employees who work on all the projects controlled by department number 5.
In this query, we first create a table DEPT5_PROJS that contains the project numbers of all projects controlled by department 5.
Then we create a table EMP_PROJ that holds (Ssn, Pno) tuples, and apply the division operation.
Notice that we renamed the attributes so that they will be correctly used in the division operation.
Finally,we join the result of the division, which holds only Ssn values, with the EMPLOYEE table to retrieve the desired attributes from EMPLOYEE.
Examples of Queries in Relational Algebra (Cont.,)
Aruna Devi.C (DSCASC)
![Page 52: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/52.jpg)
Query 4. Make a list of project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project.
In this query, we retrieved the project numbers for projects that involve an employee named Smith as a worker in SMITH_WORKER_PROJS. Then we retrieved the project numbers for projects that involve an employee named Smith as manager of the department that controls the project in SMITH_MGR_PROJS. Finally, we applied the UNION operation on SMITH_WORKER_PROJS and SMITH_MGR_PROJS.
Examples of Queries in Relational Algebra (Cont.,)
Aruna Devi.C (DSCASC)
![Page 53: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/53.jpg)
Other Example Queries
Find all loans of over $1200
53
Find the loan number for each loan of an amount greater than $1200
amount > 1200 (loan)
loan_number (amount > 1200 (loan))
Find the names of all customers who have a loan, an account, or both, from the bank
Find the names of all customers who have a loan and an account at bank.
customer_name (borrower) customer_name (depositor)
customer_name (borrower) customer_name (depositor)
![Page 54: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/54.jpg)
Example Queries
1) Find the names of all customers who have a loan at the Perryridge branch.
54
2) Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.
customer_name (branch_name = “Perryridge”
(borrower.loan_number = loan.loan_number(borrower x loan))) –
customer_name(depositor)
customer_name (branch_name=“Perryridge”
(borrower.loan_number = loan.loan_number(borrower x loan)))
Aruna Devi.C (DSCASC)
![Page 55: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/55.jpg)
Example Queries
Find the names of all customers who have a loan at the Perryridge branch.
55
Query 2
customer_name(loan.loan_number = borrower.loan_number (
(branch_name = “Perryridge” (loan)) x borrower))
Query 1
customer_name (branch_name = “Perryridge” (
borrower.loan_number = loan.loan_number (borrower x loan)))
Aruna Devi.C (DSCASC)
![Page 56: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/56.jpg)
Relational Database Design UsingER-to-Relational Mapping
Chapter 6
![Page 57: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/57.jpg)
Step 1: Mapping of Regular Entity Types.
Step 2: Mapping of Weak Entity Types.
Step 3: Mapping of Binary 1:1 Relationship Types.
Step 4: Mapping of Binary 1:N Relationship Types.
Step 5: Mapping of Binary M:N Relationship Types.
Step 6: Mapping of Multi valued Attributes.
Step 7: Mapping of N-ary Relationship Types.
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
![Page 58: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/58.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
Step 1: Mapping of Regular Entity Types:
For each regular (strong) entity type E in the ER schema, create a relation R that includes all the simple attributes of E.
Include only the simple component attributes of a composite attribute.
Choose one of the key attributes of E as the primary key for R.
If the chosen key of E is a composite, then the set of simple attributes that form it will together form the primary key of R.
If multiple keys were identified for E during the conceptual design, the information describing the attributes that form each additional key is kept in order to specify secondary (unique) keys of relation R.
We create the relations EMPLOYEE, DEPARTMENT, and PROJECT in Figure 2 to correspond to the regular entity types EMPLOYEE, DEPARTMENT, and PROJECT in Figure 1.
![Page 59: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/59.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
The foreign key and relationship attributes, if any, are not included yet; they will be added during subsequent steps.
Example, we choose Ssn, Dnumber, and Pnumber as primary keys for the relations EMPLOYEE, DEPARTMENT, and PROJECT, respectively.
The relations that are created from the mapping of entity types are sometimes called entity relations because each tuple represents an entity instance.
![Page 60: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/60.jpg)
Relational Database Design Using ER-to-Relational MappingER-to-Relational Mapping Algorithm:
![Page 61: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/61.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
![Page 62: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/62.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
Step 2: Mapping of Weak Entity Types:
For each weak entity type W in the ER schema with owner entity type E, create a relation R and include all simple attributes (or simple components of composite attributes) of W as attributes of R.
In addition, include as foreign key attributes of R, the primary key attribute(s) of the relation(s) that correspond to the owner entity type(s); this takes care of mapping the identifying relationship type of W.
The primary key of R is the combination of the primary key(s) of the owner(s) and the partial key of the weak entity type W, if any.
Aruna Devi.C (DSCASC)
![Page 63: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/63.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
We create the relation DEPENDENT in this step to correspond to the weak entity type DEPENDENT.
The primary key of the DEPENDENT relation is the combination {Essn, Dependent_name}, because Dependent_name is the partial key of DEPENDENT.
Aruna Devi.C (DSCASC)
![Page 64: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/64.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
Step 3: Mapping of Binary 1:1 Relationship Types:
For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that correspond to the entity types participating in R.
There are three possible approaches: (a) the foreign key approach, (b) the merged relationship approach, and (c) the cross reference or relationship relation
approach.
a. Foreign key approach: Choose one of the relations—S, say—and include as a foreign key
in S the primary key of T.
We include the primary key of the EMPLOYEE relation as foreign key in the DEPARTMENT relation and rename it Mgr_ssn.
Aruna Devi.C (DSCASC)
![Page 65: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/65.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
b. Merged relation approach:
An alternative mapping of a 1:1 relationship type is to merge the two entity types and the relationship into a single relation.
c. Cross-reference or relationship relation approach:
The third option is to set up a third relation R for the purpose of cross-referencing the primary keys of the two relations S and T representing the entity types.
Aruna Devi.C (DSCASC)
![Page 66: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/66.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
Step 4: Mapping of Binary 1:N Relationship Types:
For each regular binary 1:N relationship type R, identify the relation S that represents the participating entity type at the N-side of the relationship type.
Include any simple attributes (or simple components of composite attributes) of the 1:N relationship type as attributes of S.
Example, we now map the 1:N relationship types WORKS_FOR, CONTROLS, and SUPERVISION from Figure 1.
For WORKS_FOR we include the primary key Dnumber of the DEPARTMENT relation as foreign key in the EMPLOYEE relation and call it Dno.
For SUPERVISION we include the primary key of the EMPLOYEE relation as foreign key in the EMPLOYEE relation itself—because the relationship is recursive— and call it Super_ssn.
![Page 67: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/67.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
Step 5: Mapping of Binary M:N Relationship Types:
For each binary M:N relationship type R, create a new relation S to represent R.
Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types; their combination will form the primary key of S.
Example, we map the M:N relationship type WORKS_ON from Figure 1 by creating the relation WORKS_ON in Figure 2.
We include the primary keys of the PROJECT and EMPLOYEE relations as foreign keys in WORKS_ON and rename them Pno and Essn, respectively.
![Page 68: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/68.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
Step 6: Mapping of Multi valued Attributes:
For each multi valued attribute A, create a new relation R.
This relation R will include an attribute corresponding to A, plus the primary key attribute K—as a foreign key in R—of the relation that represents the entity type or relationship type that has A as a multi valued attribute.
The primary key of R is the combination of A and K.
Example, we create a relation DEPT_LOCATIONS.
Aruna Devi.C (DSCASC)
![Page 69: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/69.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
The attribute Dlocation represents the multivalued attribute LOCATIONS of DEPARTMENT, while Dnumber—as foreign key—represents the primary key of the DEPARTMENT relation.
The primary key of DEPT_LOCATIONS is the combination of {Dnumber, Dlocation}.
Aruna Devi.C (DSCASC)
![Page 70: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/70.jpg)
Relational Database Design Using ER-to-Relational Mapping (Cont.,)
Step 7: Mapping of N-ary Relationship Types:
For each n-ary relationship type R, where n > 2, create a new relation S to represent R.
Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types.
The primary key of S is usually a combination of all the foreign keys that reference the relations representing the participating entity types.
Aruna Devi.C (DSCASC)
![Page 71: Dbms ii mca-ch5-ch6-relational algebra-2013](https://reader036.vdocuments.net/reader036/viewer/2022062407/55d1dfe5bb61ebbc1e8b466e/html5/thumbnails/71.jpg)
Summary of Mapping constructs and constraints
Table : Correspondence between ER and Relational Models
ER Model Relational ModelEntity type “Entity” relation1:1 or 1:N relationship type Foreign key (or “relationship” relation)M:N relationship type “Relationship” relation and two foreign keysn-ary relationship type “Relationship” relation and n foreign keysSimple attribute AttributeComposite attribute Set of simple component attributesMultivalued attribute Relation and foreign keyValue set DomainKey attribute Primary (or secondary) key
Aruna Devi.C (DSCASC)