Chap6-Relational Algebra
Chap6-Relational Algebra
Chap6-Relational Algebra
Relational Algebra
Chapter 6
Relational Algebra
• Definition of relational Algebra
• Purpose of Relational Algebra
• Relational algebra Operations
• Join
• Types of join
• Aggregate function
• ER Diagram to Relational Model
• More Example
Relational Algebra
• Definition:
– Relational algebra is a procedural query language.
Procedural:
The user instructs the system to perform a sequence of operations on the
database to compute the desired result.
PL/SQL is Procedural.
Non-Procedural:
In contrast the non-procedural languages user specify what they want and do
not specify how to do.
SQL is Non-Procedural.
Role of Relational algebra in DBMS
• S1 ᴜ S2 S1 ∩ S2
Sid Sname Rating Age
Sid Sname Rating Age
11 Ramesh 9 25
11 Ramesh 9 25
13 Asha 7 30
12 Mamatha 8 27
13 Asha »7 30 S1 – S2
14 Akash 9 28
Sid Sname Rating Age
19 Kavitha 10 27
12 Mamatha 8 27
16 Bharath 9 31
14 Akash 9 28
Set Difference
sid sname rating age
sid sname rating age
22 dustin 7 45.0
22 dustin 7 45.0
31 lubber 8 55.5 S1 S2
58 rusty 10 35.0
S1
ρ Ship (Boats)
• R S RxS
a 1 a 1
b 2 a 2
3 a 3
b 1
b 2
b 3
R1 Cross Product Example
S1
sid sname rating age
22 xxx 7 45.0 sid bid day
31 yyy 8 55.5 22 101 10/10/96
58 zzz 10 35.0 58 103 11/12/96
• Eg of Division : A/B
A B1 B2 B3
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
s1 p3 p4
s1 p4
s2 p1 A/B1 A/B2 A/B3
s2 p2
sno sno sno
s3 p2
s1 s1 s1
s4 p2
s2 s4
s4 p4 s3
s4
B2 = {S1,S2,S3,S4} ∩{S1,S4}
Relational
Model:
3
4
ER diagram to Relational mapping
7-Step Process:
1. Map Regular Entity Types
2. Map Weak Entity Types
3. Map Binary 1:1 Relation Types
4. Map Binary 1:N Relationship Types.
5. Map Binary M:N Relationship Types.
6. Map Multivalued attributes.
7. Map N-ary Relationship Types.
Mapping Entity Types to Relations
• For every entity type create a relation
• Every atomic attribute of the entity type becomes a relation attribute
• For Composite attributes: include all the atomic attributes
• Derived attributes are not included
• Attributes of the entity key make up the primary key of the relation
3
6
STUDENT (studno, givenname, familyname)
Mapping Weak Entity Types to Relations
• Create a Relation S for weak entity set.
• Add all its attributes to relation S as field.
• Add the primary key of identifying entity set R to
Relation S and Declare all foreign key constraints.
» Or
Name
Personid
Persons( personid , name, lastname, email,Phone )
Countries ( countryid , name, Address)
HasRelat ( hasrelatid , personid , countryid)
Mapping Multivalued attribute
• For each multivalued attribute create a new relation T.
• Add primary key of each entity set as foreign key of Relation T.
• The foreign key attribute and multivalued attribute become composite
key.
Mapping N-ary relationship
• For each N-ary relationship, create a new relation.
• New relation will contain the primary key of all the entity sets forming the
N- ary relationship
Some more example Queries
Consider the schema depositor (Cust_name, acc_number)
Borrower (cust_name, loan_number) loan(branch_name, loan_number, amount)
• Find all loans of over $1200
• Find the names of all customers who have a loan at the Perryridge
branch.
customer_name (branch_name=“Perryridge”
(borrower.loan_no = loan.loan_no(borrower x loan)))
• lives(person-name,street,city)
• works(person-name, company-name,salary)
• located-in(company-name,city)
• manages(person-name,manager-name)
Find the name of all employees (i.e., persons) who work for
the City Bank company (which is a specific company in the
database).
• lives(person-name, street,city)
• works(person-name, company-name, salary)
• located-in(company-name, city)
• manages(person-name,manager-name)
Find the name and city of all employees who work for City Bank.
For this query we need to access the lives table to get city of the employee and
the located-in table to get city of the company; plus the works table to associate
employee with their company. The selection condition is then that the two cities are
the same.
Questions
• Explain select and Project operations
• Explain union, intersect and minus operations.
• Explain division operator.
• Explain join and types of join
• Write short note on Natural join with example
• Explain Types of Outer join
• List the steps to convert ER diagram to
Relational model