SlideShare a Scribd company logo
1 of 81
RELATIONAL MODEL
AND
RELATIONAL ALGEBRA
Module-2
Relational Model Concepts:
 Relational Model represents the database
as a collection of relations.
 In the RM terminology a row is called a
tuple.
 Column is called an attribute and the
table is called a relation.
 The value that can appear in each column
called a domain.
 Domain-set of atomic value
 Relation Schema-R-(A1,A2,A3….An)
 D –domain of Ai-dom(Ai).
 r®,r={t1,t2,t3…tn}
 T=<v1,v2,…vn> vi,1<=i<=n,
 Relational state:
 dom(A1),dom(A2)….dom(An)
 r®€(dom(A1)*dom(A2)*….dom(An))
Characteristics of relations:
1)Ordering of tuples in a relation.
2)Ordering of values with tuples .
Key Constraints and Constraints on
NULL
 Key Constraint:
 Candidate key:
1)Uniqueness
2)Irreduciblilty
Super key:t1[sk]<>t2[sk]
Relation r and a subset of attribute SK
{empno,empname}
Student(regno,name,add,pno,dob,gpa)
{regno,name}
Entity Integrity, Referential Integrity
and Foreign Key
 T1[fk]=T2[pk]
Update operations & Dealing with
Constraint Violations
 Three update operations
1)Insert
2)Delete
3)Modify
Constraint Violations on Insert operations
are
1)Domain Constraints
2)Key constraints
3)Entity Integrity
4)Referential Integrity
2)Delete Operation
 Referential Integrity
3)Update operation
 Referential Integrity
Introduction
 What is relational algebra?
Relational algebra is a collection of operations that
are used to manipulate the entire set of relations.
The output of any relational algebra operation is
always a relation.
 What are the Operations?
 Set operations like union, intersection, difference,
and Cartesian product.
 Special operations like: Selection, Projection
 Joins.
Relational Algebra (RA)
 Relational algebra is developed by E. F. Codd
in 1971.
 Relational algebra is a procedural language
i.e. An RA expression contains answers to
 i. WHAT is to be retrieved?
 ii. HOW it is to be retrieved? and
 iii. WHERE it is stored?
 RA is a paradigm in a relational domain.
 It is used as an intermediatory language to
transform the non-procedural queries into
procedural queries.
Relational Query Languages
 Query languages:
Allow manipulation and retrieval of data from a
database.
 Relational model supports simple, powerful
QLs:
 Strong formal foundation based on logic.
 Allows for much optimization.
 Query Languages != programming languages!
Operations of
Relational Algebra
Unary Operators Binary Operators
SELECTION SET Operations
PROJECTION JOINS
SSN Name BDate Addr Sex Salary SuperSSN DNo
1111 Deepak 5-Jan-62 Malleswaram M 22000 4444 1
2222 Nandagopal 10-Dec-60 Rajajinagar M 30000 4444 3
3333 Pooja 22-Jan-65 Indiranagar F 18000 2222 2
4444 Prasad 11-Jan-57 Rajajinagar M 32000 6666 3
5555 Reena 15-Jan-85 MG Road F 8000 4444 3
DNumber DName MgrSSN MgrStartDate
1 Admin 1111 23-Jan-2000
2 Research 2222 11-Aug-1995
3 Accounts 4444 7-Dec-1986
PNumber PName PLocation DNum
10 Library Management USA 2
20 ERP Chennai 1
30 Hospital Management Mumbai 3
40 Wireless Network London 2
Employee
Department
Project
Example Instances
Example Instances
sid bid
s2 b1
s1 b1
day
10/10/99
11/12/99
sid sname city
s1 Deepa bang
s2 Laxmi mang
s3 Roopa del
rating
7
8
10
Bid Bname city
Reservation
As R
Sailor
As S
Boat
As B
B1 star mang
B2 Water rat chennai
The SELECT operation ()
 It is an Unary operator
 The select operation retrieves a subset of tuples/
rows in a relation that satisfy a selection
condition.
Syntax: <selection-condition> (Relation)
 The rows are filtered based on the selection
condition.
 All COLUMNS of the relation will be displayed.
 Hence, SELECTION retrieves ALL COLUMNS
but SUB_SET of ROWS.
Selection
 One of the interesting properties of the selection
operation is that it is commutative. Therefore, all
the expressions shown below are equivalent,
condition-1 (condition-2 (R))
(condition-1 AND condition-2) (R)
condition-2 (condition-1 (R))
The SELECT operation ()
 Query-1: Find the employees whose salary is greater than 10,000 rupees.
Salary > 10000 (Employee)
 Query-2: Find the employees who work for department 3 and whose salary is
greater than 30,000 rupees.
Emp3  DNo = 3 (Employee)
Result  Salary > 30000 (Emp3)
Or,
Result  DNo = 3 and Salary > 30000 (Employee)
 Result  (DNo = 4 and Salary > 25000) OR
( DNo = 5 and Salary > 30000) (Employee)
EMP1<- (DNo = 4 and Salary > 25000)(Employee)
EMP2<- ( DNo = 5 and Salary > 30000) (Employee)
RESULT<-(EMP1) OR(EMP2)(EMPLOYEE)
Selection
sid sname age
28 Yamuna 35.0
rating
9
58 Roopa 35.0
10
rating > 8(SAILOR)
sname
Yamuna
rating
9
Roopa 10 sname, rating(S2) (rating > 8(S2))
List all sailors who have a rating more than 8
The Projection Operation ()
 The projection operation is used to select only few /all the
columns from a relation.
<attributes> (Relation)
 Query-5: List the name and salary of all the employees.
Name, Salary (Employee)
 Query-6: Print the project name and their locations.
PName, PLocation (Project)
 Query -7: Retrieve the Name and Salary of all employees working for
department 1.
Result  Name, Salary (DNo = 1 (Employee))
Projection
sname
Yamuna
Laxmi
Geeta
rating
9
8
5
Roopa 10
age
35.0
sname, rating(S2)
age(S2)
55.5
List the name and rating of sailors
List the age of sailors
 Query-8: Find the name, address, and salary
of the employees who earn more than 25000
rupees.
Name, Salary, Addr (Salary > 25000 (Employee))
 Query-9: List the name and the location of
the projects not controlled by department 2.
PName, PLocation (DNum ≠ 2) (Project))
SET OPERATIONS
 UNION
 INTERSECTION
 DIFFERENCE
 CARTESIAN PRODUCT
Union Compatibility
Union Compatibility:
Let R and S be two relations with attributes (A1, A2, …, An) and (B1, B2,
….., Bn) respectively.
If R and S are to be Unioned, Intersectioned, Minused then, they shall
satisfy the following two rules:
Rule 1: The relations R and S shall have the same
degree. That is, the number of attributes of R and S
must be same.
Rule 2: The domain of the ith attribute of R and the
domain of the ith attribute of S shall be same.
dom(Ai) = dom(Bi), where, 1  i  n.
Example
 Query-10: Retrieve the SSN of all employees who either work in
department 3 or directly supervise an employee who works in 3.
Emp_Dept3  SSN (DNo = 3 (Employee))
Supervisors  SuperSSN (DNo = 3 (Employee))
Result  Emp_Dept3  Supervisors
Emp_Dept3 Supervisors
2222 4444
4444 6666
5555
Result
2222
4444
5555
6666
Intersection (∩)
The expression R ∩ S returns all common tuples that appear
in both the relations R and S.
Query-12: Find the first name and the last name of people
who are teachers as well as students.
FName LName Age FName LName Age
Susan Yao 18 Jennifer Amy 30
Ramesh Arvind 20 Nanda Sham 40
Joseph Antony 19 Ramesh Arvind 20
Jennifer Amy 30
Andy Perumal 21
Student Teacher
Difference
 This operation, written as R – S (set difference) returns all
tuples that are in R but not in S.
 Query-13: Find the students who are not teachers.
Result  Student – Teacher
{Susan, Joseph, Andy}
 Query-14: Find the teachers who are not students.
Result  Teacher – Student
{Nandagopal}
Cartesian Product (×)
 The Cartesian product or cross-product is a binary
operation that is used to combine two relations. Assuming
R and S as relations with n and m attributes respectively, the
Cartesian product, R × S can be written as,
R (A1, A2, …, An) × S (B1, B2, …, Bm)
The result of the above set operation is,
Q (A1, A2, …, An, B1, B2, …, Bm)
Where,
Degree (Q) = n + m
count(Q) = Number of tuples in R * Number of tuples in S.
 Query-15: Find for each female employee, all
the names of her dependents.
Dependent
ESSN DependentName Sex BDate Relationship
1111 Pradeep M 05-Jul-63 Brother
3333 Sonali F 15-Aug-85 Daughter
3333 Rahul M 01-Jan-80 Son
4444 Miruthula F 02-Aug-83 Spouse
FemaleEmp  SSN, Name (Sex = ‘F’ (Employee))
FemaleDeps  FemaleEmp × Dependent
Result  Name, DependentName (SSN = ESSN (FemaleDeps))
Cartesian Product
 This concatenates
all tuples of second
operand relation to
each tuple of first
operand relation.
 It is useful if the
tuples are to be
selected based on
the other tuple
values
E-no Ename Salary
001
003
004
Nupur
Poonam
Shiva
40000
41000
45000
E-no Ename Salary
005
006
010
Shanta
Indu
Arif
40000
42000
45000
Bemployee
Memployee
Cartesian Product(X)
Employee := {Bemployee} X {Memployee}
E-no Ename Salary E-no Ename Salary
001
001
001
Nupur
Nupur
Nupur
40000
40000
40000
005
006
010
Shanta
Indu
Arif
40000
42000
45000
003
003
003
Poonam
Poonam
Poonam
41000
41000
41000
005
006
010
Shanta
Indu
Arif
40000
42000
45000
004
004
004
Shiva
Shiva
Shiva
45000
45000
45000
005
006
010
Shanta
Indu
Arif
40000
42000
45000
Employee
Join Operations
 Natural join (*)
 Theta join ()
 Equijoin ( = )
 Non-Equi join
 Outer join ( )
Natural Join (*)
 When we omit the condition during joining of two relations,
then it is called as natural join (*).
 When we use the word join, it invariably means natural join.
We shall formally define natural join as:
 Let R and S be the two relations with the attributes as shown
below:
R(X1, X2, .., Xm, Y1, Y2, …, Yn) and
S(Y1, Y2, …, Yn , Z1, Z2, …, ZP)
Example:
Employee (SSN, Name, Addr, DNo)
Department (DNo, DName)
Natural Join
 Employee*Department
SSN Name Addr Dno Dname
Natural Join
 For the natural Join, both operand relations
need to contain at least one attribute-
domain-pair common. The resultant relation
contains the concatenation of all the tuples of
first operand relation with second operand
relation, based on the common attribute
values.
 The resultant relation has the union of
attributes of both operand relations.
Natural Join
 Consider the Schema:
 EMP(eno, ename, age)
 WORKS-IN(eno, dno)
 DEPT(dno, dname, address)
1. Find names of employees who work in department #2.
ename (σdno=2 (E) ⋈ W )
W.eno = E.eno
2. Find names of employees who work in a department in
Bombay
ename(σaddress=Bombay (E ) ⋈ W.eno = E.eno W ⋈ W.eno = E.eno D)
3. Find the names of cities for departments in which Ravi
works:
city(σename = Ravi (E) W ⋈ D)
4. Find the names of employees who work in at least one
department
ename(E) ⋈ W)
5. Find the names of employees who work in all departments
ename ((eno,dno(W) / dno((D)) ⋈ E)
6. Find the names of employees who work in all departments
in Bombay
T1:= eno,dno(E)/ dno(σcity=Bombay(W ⋈ D)
 ename(T1 ⋈ E)
7. Find names of employees who work in a department which
is in Bombay or Delhi
T1:= σcity=Bombay (D) U σcity = Delhi (D)
ename(T1 ⋈ W ⋈ E)
8 .Find names of employees who work in a department
in Bombay and a department in Delhi:
T1:= ename(σcity=Bombay (D) ⋈ W ⋈ E)
T2:= ename(σcity=Delhi (D) ⋈ W ⋈ E)
T1 ∩ T2
9. Find employee number of employees who do not work in
dno #2
eno(E) -- eno((σdno=2 W))
10. Find eno’s for employees who work in two different
departments in Bombay:
eno(σW1.dno≠W2.dno σW1.city=W2.city(ρw1(W ⋈ D) X ρw2 (W
⋈ D))
Theta Join ()
Let R and S be two relations. Consider an
attribute x in R, and an attribute y in S. The
theta join of these two relations can be
written as,
R x  y S
where  indicates a valid relational operator.
Equijoin ( = )
 The most widely used join operation is equijoin. As
discussed in the previous subsection, when  is =,
this type of -join (a special case) is called
equijoin. We shall use the notation to denote
equijoin. The general form for this kind of join is,
R <Join_condition> S
<Join_condition> must always have = operator
 Query-16: Retrieve the name and salary
of the manager of each department.
Managers  Department MgrSSN = SSN Employee
Gnames  Name, Salary (Managers)
 Query-17: Find which project(s) and
location(s) Pooja is working on.
EmpPooja  Name = 'Pooja' (Employee)
PoojaPrj  EmpPooja SSN = ESSN WorksOn
Result   Name, PName, PLocation (PoojaPrj WrksOn.PNo = Project.PNumber Project)
Outer Joins
 Dangling tuples in Join
 Usually, only a subset of tuples of each relation will actually
participate in a join, i.e. only tuples that match with the
joining attributes.
 Tuples of a relation not participating in a join are called
dangling tuples.
 How do we keep dangling tuples in the result of a join?
Use null values to indicate a no-join situation. There are three
types of outer joins
- left outer join
- right outer join and
- outer join (full outer join).
 Left outer-join
R1 R2 is similar to a natural join but keep all dangling
tuples of R1.
 Right Outer-Join
R1 R2 is similar to a natural join but keep all dangling
tuples of R2.
 Outer Join (full outer-join)
R1 R2 is similar to a natural join but keep all dangling
tuples of both R1 and R2.
The advantages of outer join is to take the union of tuples
from two relations that are not union compatible.
Example
RegNo Name GPA Branch RegNo Amount Year
1BI01CS056 Pooja 5.6 CSE 1BI01CS056 10000 2002
1BI01ME067 Pallavi 8.9 MECH 1BI01IS001 20000 2003
1BI01CS045 Reena 9.5 CSE
1BI01IS001 Kumar 6.0 ISE
RegNo Name GPA Branch Amount Year
1BI01CS056 Pooja 5.6 CSE 10000 2002
1BI01ME067 Pallavi 8.9 MECH null null
1BI01CS045 Reena 9.5 CSE null null
1BI01IS001 Kumar 6.0 ISE 20000 2003
Students Awards
Students Awards
Dangling
tuples
Division
 Consider two relations R and S. Assume that R has only two attributes
r1 and r2 and S has only one attribute r2 with the same domain as in
R. This is to ensure that the degree of the numerator is more than the
degree of the denominator. Now we shall define R / S as,
 For each r1 value in R, consider the set of r2 values that appear in
tuples of R with that r2 value. If this set contains S, the r1 is in the
result of R / S.
 One possible restriction in this definition is that every attribute in S
should be in R.
Alternatively, we can define R / S as,
For, R(a1, a2, …., an, b1, b2, …., bn) / S(b1, b2, …., bn) and
T = a1, a2, …., an (R), return the subset of T, say W, such that every
tuple in W × S is in R. W is the largest subset of T, such that,
(W × S)  R
Example
Options
RegNo SubId
412 CS175
412 CS272
412 CS351
413 CS175
413 CS272
532 CS175
676 CS272
SubId
CS175
CS272
SubId
CS175
SubId
CS175
CS272
CS351
RegNo
412
413
RegNo
412
413
532
RegNo
412
Electives1 Electives2 Electives3
Options/Electives1 Options/Electives2 Options/Electives3
GROUPING AND AGGREGATE
FUNCTIONS
 We shall define Function formally using the
symbol  (pronounced as script F ) as,
grouping_attributes  aggregate_functions (Relation)
 The steps involved in the evaluation of this
function is,
 Partition the relation into groups.
 Apply aggregate function to each group.
 Output group and aggregate values, one tuple per
group.
 Query-18: Retrieve department number,
number of employees and their average
salary.
Result (DNo, N, AvgSal)   DNo  Count(SSN), Avg(Salary) (Employee)
 R1(SSN,NO_OF_DEPENDENTS)ESSN 
COUNT DEPENDENTNAME (DEPENDENT)
 R2<- σ NO_OF_DEPENDENTS>=2(R1)
 R3<-  LNAME,FNAME(R2*EMPLOYEE)
Relational Database Design by
ER-to-Relational Mapping
 ER-to-Relational Mapping Algorithm
 Step 1: Mapping of Regular Entity Types
 Step 2: Mapping of Weak Entity Types
 Step 3: Mapping of Binary 1:1 Relation Types
 Step 4: Mapping of Binary 1:N Relationship Types.
 Step 5: Mapping of Binary M:N Relationship Types.
 Step 6: Mapping of Multivalued attributes.
 Step 7: Mapping of N-ary Relationship Types.
ER-to-Relational Mapping
Algorithm
 Step 1: Mapping of Regular Entity Types.
 For each regular (strong) entity type E in the ER diagram,
create a relation R that includes all the simple attributes of E.
 Choose one of the key attributes of E as the primary key for
R.
 If the chosen key of E is composite, the set of simple
attributes that form it will together form the primary key of
R.
 Example: We create the relations EMPLOYEE,
DEPARTMENT, and PROJECT in the relational schema
corresponding to the regular entities in the ER diagram.
 SSN, DNUMBER, and PNUMBER are the primary keys for
the relations EMPLOYEE, DEPARTMENT, and PROJECT
as shown.
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 & include all simple
attributes (or simple components of composite attributes) of
W as attributes of R.
 Also, include as foreign key attributes of R the primary key
attribute(s) of the relation(s) that correspond to the owner
entity type(s).
 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.
 Example: Create the relation DEPENDENT in this step to
correspond to the weak entity type DEPENDENT.
 Include the primary key SSN of the EMPLOYEE relation
as a foreign key attribute of DEPENDENT (renamed to
ESSN).
 The primary key of the DEPENDENT relation is the
combination {ESSN, DEPENDENT_NAME} because
DEPENDENT_NAME is the partial key of DEPENDENT
Step 3: Mapping of Binary 1:1 Relation 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:
1. Foreign Key approach: Choose one of the relations-say S-
and include a foreign key in S the primary key of T. It is better
to choose an entity type with total participation in R in the role
of S.
 Example: 1:1 relation MANAGES is mapped by choosing
the participating entity type DEPARTMENT to serve in the
role of S, because its participation in the MANAGES
relationship type is total.
2.Merged relation option: An alternate mapping of a 1:1
relationship type is possible by merging the two entity types
and the relationship into a single relation. This may be
appropriate when both participations are total.
3.Cross-reference or relationship relation option: The third
alternative 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.
Step 4: Mapping of Binary 1:N Relationship Types.
 For each regular binary 1:N relationship type R, identify
the relation S that represent the participating entity type at
the N-side of the relationship type.
 Include as foreign key in S the primary key of the relation
T that represents the other entity type participating in R.
 Include any simple attributes of the 1:N relation type as
attributes of S.
 Example: 1:N relationship types WORKS_FOR, CONTROLS,
and SUPERVISION in the figure.
 For WORKS_FOR we include the primary key
DNUMBER of the DEPARTMENT relation as foreign key
in the EMPLOYEE relation and call it DNO.
 Step 5: Mapping of Binary M:N Relationship Types.
 For each regular 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.
 Also include any simple attributes of the M:N relationship
type (or simple components of composite attributes) as
attributes of S.
 Example: The M:N relationship type WORKS_ON from the
ER diagram is mapped by creating a relation WORKS_ON in
the relational database schema.
 The primary keys of the PROJECT and EMPLOYEE
relations are included as foreign keys in WORKS_ON and
renamed PNO and ESSN, respectively.
 Attribute HOURS in WORKS_ON represents the HOURS
attribute of the relation type. The primary key of the
WORKS_ON relation is the combination of the foreign key
attributes {ESSN, PNO}.
Step 6: Mapping of Multivalued attributes.
 For each multivalued 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 of relationship type that
has A as an attribute.
 The primary key of R is the combination of A and K. If the
multivalued attribute is composite, we include its simple
components.
 Example: The relation DEPT_LOCATIONS is created.
 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 R is the combination of {DNUMBER,
DLOCATION}.
Step 7: Mapping of N-ary Relationship Types.
 For each n-ary relationship type R, where n>2, create a new
relationship S to represent R.
 Include as foreign key attributes in S the primary keys of the
relations that represent the participating entity types.
 Also include any simple attributes of the n-ary relationship
type (or simple components of composite attributes) as
attributes of S.
 Example: The relationship type SUPPY in the ER on the next
slide.
 This can be mapped to the relation SUPPLY shown in the
relational schema, whose primary key is the combination of
the three foreign keys {SNAME, PARTNO, PROJNAME}
Ternary relationship types. (a) The SUPPLY relationship.
Mapping the n-ary relationship type SUPPLY
Summary of Mapping constructs and constraints
Correspondence between ER and Relational Models
ER Model Relational Model
Entity type “Entity” relation
1:1 or 1:N relationship type Foreign key (or “relationship” relation)
M:N relationship type “Relationship” relation and two foreign keys
n-ary relationship type “Relationship” relation and n foreign keys
Simple attribute Attribute
Composite attribute Set of simple component attributes
Multivalued attribute Relation and foreign key
Value set Domain
Key attribute Primary (or secondary) key
Mapping Exercise
FIGURE 7.7
An ER schema for a
SHIP_TRACKING
database.
The ER conceptual schema diagram for the COMPANY
database.
Result of mapping the COMPANY ER schema into a relational
schema.
Chapter Summary
 ER-to-Relational Mapping Algorithm
 Step 1: Mapping of Regular Entity Types
 Step 2: Mapping of Weak Entity Types
 Step 3: Mapping of Binary 1:1 Relation Types
 Step 4: Mapping of Binary 1:N Relationship Types.
 Step 5: Mapping of Binary M:N Relationship Types.
 Step 6: Mapping of Multivalued attributes.
 Step 7: Mapping of N-ary Relationship Types.
One possible database state for the COMPANY
relational database schema (Fig. 5.6)
One possible database state for the COMPANY
relational database schema – continued (Fig. 5.6)

 ⋈
 
 R1(PNO)<- PNUMBER(DNUM=5(PROJECT))
 R2(SSN,PNO)<-ESSN,PNO(WORKSON)
 R3<-R2÷R1
 RESULT<-NAME(R3*EMPLOYEE)
Retrieve the name of employess who
have no dependents
 R1<-SSN(EMPLOYEE)
 R2<-ESSN(DEPENDENT)
 R3<-R1-R2
 RESULT<-FNAME,LNAME(R3*EMPLOYEE)
List the names of managers who
have atleast one dependent
 R1<-MGRSSN(DEPARTMENT)
 R2<-ESSN(DEPENDENT)
 R3<-R1∩R2
 RESULT<-LNAME,FNAME(R3*EMPLOYEE)
 Make a list of project numbers for project
that involve an employee who last name is
‘Smith’ ,either as a worker or as a manager
of the departments that controls the project.
 R1<- SSN(LNAME=‘SMITH’(EMPLOYEE)
 R2<- PNO(WORKS*R1)
R3<- DNO(R1⋈R1.SSN=DEPT.MGRSSN (DEPT))
R4<- DNO(LNAME=‘SMITH’(R3)
R5<- PNO(R3*PROJECT)
RESULT<-R2ᶸR5
 For every project located in ‘Stafford’, list
the project number, the controlling dept
no and the dept manager’s last name,
address and birth date.
 R1<- Ploc=‘Stafford’(Project)
 R2<-(R1 ⋈DNUM=DNUMBER DEPARTMENT)
 R3<-(R2 ⋈MGRSNN=SSN EMPLOYEE)
RESULT<-
PNO,DNUM,LNAME,ADD,BDATE(R3)

 ⋈
 R1(PNO)<- PNUMBER(DNUM=5(PROJECT))
 R2(SSN,PNO)<-ESSN,PNO(WORKSON)
 R3<-R2÷R1
 RESULT<-NAME(R3*EMPLOYEE)
SAILORS BOAT
SID SNAME RATING AG
E
401 RAJ 10 25
402 RAVI 20 30
403 MAHESH 30 35
404 SATISH 40 40
BID BNAM
E
COLO
R
100 INTER
LAKE
RED
101 INTER
LAKE
GREEN
103 MARIN
E
RED
104 CLIPPE
R
GREEN
105 CLIPPE
R
BLACK
102 clipper brown
SID BID DAY
401 100 MONDAY
401 101 TUESDAY
402 102 WEDNES
DAY
403 103 MONDAY
RESERVE
1.Find the names of sailors who
have reserved boat 103.
 R1<- bid=103(Reserve)
 R2<- sname(R1 ⋈sid=sid Sailors)
2.Find the names of sailors who have reserved a
red boat.
 R1<- BID(COLOR=RED(BOAT))
 R2<-(R1 ⋈BID=BID RESERVE)
 R3<- NAME(R2 ⋈SID=SID (SAILORS))
3.Find the names of sailors who
have reserved a red or green boat
 R1<- COLOR=RED(BOAT)
 R2<- SID(R1 ⋈Bid=Bid RESREVE)
 R3<- COLOR=GREEN(BOAT)
 R4<- SID(R1 ⋈Bid=Bid RESREVE)
 R5<-R2ᶸR4
 RESULT<- SNAME(R5*SAILORS)
R1<- SID(COLOR=RED(BOAT))⋈Bid=Bid
RESREVE)
R2<-  SID(COLOR=GREEN(BOAT))⋈Bid=Bid
RESREVE)
R3<-R1ᶸR2
RESULT<- SNAME(R3*SAILORS)
4.Find the colors of boats reserved
by mahesh.
Result<-color((sname=mahesh(Sailors)
⋈Reserve ⋈Boats)
Or

More Related Content

Similar to Module 2_Relational Algebra.pptx

1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf
Kavinilaa
 
Relational model
Relational modelRelational model
Relational model
Jihin Raju
 
Relational model
Relational modelRelational model
Relational model
Jihin Raju
 
Relational model
Relational modelRelational model
Relational model
Jihin Raju
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2
Mukund Trivedi
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)
Mukund Trivedi
 

Similar to Module 2_Relational Algebra.pptx (20)

1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
 
DBMS CS3
DBMS CS3DBMS CS3
DBMS CS3
 
Module 2-2.ppt
Module 2-2.pptModule 2-2.ppt
Module 2-2.ppt
 
3 relational model
3 relational model3 relational model
3 relational model
 
Relational model
Relational modelRelational model
Relational model
 
Ra Revision
Ra RevisionRa Revision
Ra Revision
 
Unit_3.pdf
Unit_3.pdfUnit_3.pdf
Unit_3.pdf
 
Relational model
Relational modelRelational model
Relational model
 
Relational model
Relational modelRelational model
Relational model
 
Relational model
Relational modelRelational model
Relational model
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
 
3.2 SQL to -Relational Algebra.pdf
3.2  SQL to -Relational Algebra.pdf3.2  SQL to -Relational Algebra.pdf
3.2 SQL to -Relational Algebra.pdf
 
4 the sql_standard
4 the  sql_standard4 the  sql_standard
4 the sql_standard
 
Relational model for Databases
Relational model for DatabasesRelational model for Databases
Relational model for Databases
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
Relational Algebra.ppt
Relational Algebra.pptRelational Algebra.ppt
Relational Algebra.ppt
 
Database systems-Formal relational query languages
Database systems-Formal relational query languagesDatabase systems-Formal relational query languages
Database systems-Formal relational query languages
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)
 

Recently uploaded

Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 

Module 2_Relational Algebra.pptx

  • 2. Relational Model Concepts:  Relational Model represents the database as a collection of relations.  In the RM terminology a row is called a tuple.  Column is called an attribute and the table is called a relation.  The value that can appear in each column called a domain.
  • 3.  Domain-set of atomic value  Relation Schema-R-(A1,A2,A3….An)  D –domain of Ai-dom(Ai).  r®,r={t1,t2,t3…tn}  T=<v1,v2,…vn> vi,1<=i<=n,  Relational state:  dom(A1),dom(A2)….dom(An)  r®€(dom(A1)*dom(A2)*….dom(An))
  • 4. Characteristics of relations: 1)Ordering of tuples in a relation. 2)Ordering of values with tuples .
  • 5. Key Constraints and Constraints on NULL  Key Constraint:  Candidate key: 1)Uniqueness 2)Irreduciblilty Super key:t1[sk]<>t2[sk] Relation r and a subset of attribute SK {empno,empname} Student(regno,name,add,pno,dob,gpa) {regno,name}
  • 6. Entity Integrity, Referential Integrity and Foreign Key  T1[fk]=T2[pk]
  • 7. Update operations & Dealing with Constraint Violations  Three update operations 1)Insert 2)Delete 3)Modify Constraint Violations on Insert operations are 1)Domain Constraints 2)Key constraints 3)Entity Integrity 4)Referential Integrity
  • 8. 2)Delete Operation  Referential Integrity 3)Update operation  Referential Integrity
  • 9. Introduction  What is relational algebra? Relational algebra is a collection of operations that are used to manipulate the entire set of relations. The output of any relational algebra operation is always a relation.  What are the Operations?  Set operations like union, intersection, difference, and Cartesian product.  Special operations like: Selection, Projection  Joins.
  • 10. Relational Algebra (RA)  Relational algebra is developed by E. F. Codd in 1971.  Relational algebra is a procedural language i.e. An RA expression contains answers to  i. WHAT is to be retrieved?  ii. HOW it is to be retrieved? and  iii. WHERE it is stored?  RA is a paradigm in a relational domain.  It is used as an intermediatory language to transform the non-procedural queries into procedural queries.
  • 11. Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational model supports simple, powerful QLs:  Strong formal foundation based on logic.  Allows for much optimization.  Query Languages != programming languages!
  • 12. Operations of Relational Algebra Unary Operators Binary Operators SELECTION SET Operations PROJECTION JOINS
  • 13. SSN Name BDate Addr Sex Salary SuperSSN DNo 1111 Deepak 5-Jan-62 Malleswaram M 22000 4444 1 2222 Nandagopal 10-Dec-60 Rajajinagar M 30000 4444 3 3333 Pooja 22-Jan-65 Indiranagar F 18000 2222 2 4444 Prasad 11-Jan-57 Rajajinagar M 32000 6666 3 5555 Reena 15-Jan-85 MG Road F 8000 4444 3 DNumber DName MgrSSN MgrStartDate 1 Admin 1111 23-Jan-2000 2 Research 2222 11-Aug-1995 3 Accounts 4444 7-Dec-1986 PNumber PName PLocation DNum 10 Library Management USA 2 20 ERP Chennai 1 30 Hospital Management Mumbai 3 40 Wireless Network London 2 Employee Department Project Example Instances
  • 14. Example Instances sid bid s2 b1 s1 b1 day 10/10/99 11/12/99 sid sname city s1 Deepa bang s2 Laxmi mang s3 Roopa del rating 7 8 10 Bid Bname city Reservation As R Sailor As S Boat As B B1 star mang B2 Water rat chennai
  • 15. The SELECT operation ()  It is an Unary operator  The select operation retrieves a subset of tuples/ rows in a relation that satisfy a selection condition. Syntax: <selection-condition> (Relation)  The rows are filtered based on the selection condition.  All COLUMNS of the relation will be displayed.  Hence, SELECTION retrieves ALL COLUMNS but SUB_SET of ROWS.
  • 16. Selection  One of the interesting properties of the selection operation is that it is commutative. Therefore, all the expressions shown below are equivalent, condition-1 (condition-2 (R)) (condition-1 AND condition-2) (R) condition-2 (condition-1 (R))
  • 17. The SELECT operation ()  Query-1: Find the employees whose salary is greater than 10,000 rupees. Salary > 10000 (Employee)  Query-2: Find the employees who work for department 3 and whose salary is greater than 30,000 rupees. Emp3  DNo = 3 (Employee) Result  Salary > 30000 (Emp3) Or, Result  DNo = 3 and Salary > 30000 (Employee)
  • 18.  Result  (DNo = 4 and Salary > 25000) OR ( DNo = 5 and Salary > 30000) (Employee) EMP1<- (DNo = 4 and Salary > 25000)(Employee) EMP2<- ( DNo = 5 and Salary > 30000) (Employee) RESULT<-(EMP1) OR(EMP2)(EMPLOYEE)
  • 19. Selection sid sname age 28 Yamuna 35.0 rating 9 58 Roopa 35.0 10 rating > 8(SAILOR) sname Yamuna rating 9 Roopa 10 sname, rating(S2) (rating > 8(S2)) List all sailors who have a rating more than 8
  • 20. The Projection Operation ()  The projection operation is used to select only few /all the columns from a relation. <attributes> (Relation)  Query-5: List the name and salary of all the employees. Name, Salary (Employee)  Query-6: Print the project name and their locations. PName, PLocation (Project)  Query -7: Retrieve the Name and Salary of all employees working for department 1. Result  Name, Salary (DNo = 1 (Employee))
  • 22.  Query-8: Find the name, address, and salary of the employees who earn more than 25000 rupees. Name, Salary, Addr (Salary > 25000 (Employee))  Query-9: List the name and the location of the projects not controlled by department 2. PName, PLocation (DNum ≠ 2) (Project))
  • 23. SET OPERATIONS  UNION  INTERSECTION  DIFFERENCE  CARTESIAN PRODUCT
  • 24. Union Compatibility Union Compatibility: Let R and S be two relations with attributes (A1, A2, …, An) and (B1, B2, ….., Bn) respectively. If R and S are to be Unioned, Intersectioned, Minused then, they shall satisfy the following two rules: Rule 1: The relations R and S shall have the same degree. That is, the number of attributes of R and S must be same. Rule 2: The domain of the ith attribute of R and the domain of the ith attribute of S shall be same. dom(Ai) = dom(Bi), where, 1  i  n.
  • 25. Example  Query-10: Retrieve the SSN of all employees who either work in department 3 or directly supervise an employee who works in 3. Emp_Dept3  SSN (DNo = 3 (Employee)) Supervisors  SuperSSN (DNo = 3 (Employee)) Result  Emp_Dept3  Supervisors Emp_Dept3 Supervisors 2222 4444 4444 6666 5555 Result 2222 4444 5555 6666
  • 26. Intersection (∩) The expression R ∩ S returns all common tuples that appear in both the relations R and S. Query-12: Find the first name and the last name of people who are teachers as well as students. FName LName Age FName LName Age Susan Yao 18 Jennifer Amy 30 Ramesh Arvind 20 Nanda Sham 40 Joseph Antony 19 Ramesh Arvind 20 Jennifer Amy 30 Andy Perumal 21 Student Teacher
  • 27. Difference  This operation, written as R – S (set difference) returns all tuples that are in R but not in S.  Query-13: Find the students who are not teachers. Result  Student – Teacher {Susan, Joseph, Andy}  Query-14: Find the teachers who are not students. Result  Teacher – Student {Nandagopal}
  • 28. Cartesian Product (×)  The Cartesian product or cross-product is a binary operation that is used to combine two relations. Assuming R and S as relations with n and m attributes respectively, the Cartesian product, R × S can be written as, R (A1, A2, …, An) × S (B1, B2, …, Bm) The result of the above set operation is, Q (A1, A2, …, An, B1, B2, …, Bm) Where, Degree (Q) = n + m count(Q) = Number of tuples in R * Number of tuples in S.
  • 29.  Query-15: Find for each female employee, all the names of her dependents. Dependent ESSN DependentName Sex BDate Relationship 1111 Pradeep M 05-Jul-63 Brother 3333 Sonali F 15-Aug-85 Daughter 3333 Rahul M 01-Jan-80 Son 4444 Miruthula F 02-Aug-83 Spouse FemaleEmp  SSN, Name (Sex = ‘F’ (Employee)) FemaleDeps  FemaleEmp × Dependent Result  Name, DependentName (SSN = ESSN (FemaleDeps))
  • 30. Cartesian Product  This concatenates all tuples of second operand relation to each tuple of first operand relation.  It is useful if the tuples are to be selected based on the other tuple values E-no Ename Salary 001 003 004 Nupur Poonam Shiva 40000 41000 45000 E-no Ename Salary 005 006 010 Shanta Indu Arif 40000 42000 45000 Bemployee Memployee
  • 31. Cartesian Product(X) Employee := {Bemployee} X {Memployee} E-no Ename Salary E-no Ename Salary 001 001 001 Nupur Nupur Nupur 40000 40000 40000 005 006 010 Shanta Indu Arif 40000 42000 45000 003 003 003 Poonam Poonam Poonam 41000 41000 41000 005 006 010 Shanta Indu Arif 40000 42000 45000 004 004 004 Shiva Shiva Shiva 45000 45000 45000 005 006 010 Shanta Indu Arif 40000 42000 45000 Employee
  • 32. Join Operations  Natural join (*)  Theta join ()  Equijoin ( = )  Non-Equi join  Outer join ( )
  • 33. Natural Join (*)  When we omit the condition during joining of two relations, then it is called as natural join (*).  When we use the word join, it invariably means natural join. We shall formally define natural join as:  Let R and S be the two relations with the attributes as shown below: R(X1, X2, .., Xm, Y1, Y2, …, Yn) and S(Y1, Y2, …, Yn , Z1, Z2, …, ZP) Example: Employee (SSN, Name, Addr, DNo) Department (DNo, DName)
  • 35. Natural Join  For the natural Join, both operand relations need to contain at least one attribute- domain-pair common. The resultant relation contains the concatenation of all the tuples of first operand relation with second operand relation, based on the common attribute values.  The resultant relation has the union of attributes of both operand relations.
  • 36. Natural Join  Consider the Schema:  EMP(eno, ename, age)  WORKS-IN(eno, dno)  DEPT(dno, dname, address) 1. Find names of employees who work in department #2. ename (σdno=2 (E) ⋈ W ) W.eno = E.eno 2. Find names of employees who work in a department in Bombay ename(σaddress=Bombay (E ) ⋈ W.eno = E.eno W ⋈ W.eno = E.eno D)
  • 37. 3. Find the names of cities for departments in which Ravi works: city(σename = Ravi (E) W ⋈ D) 4. Find the names of employees who work in at least one department ename(E) ⋈ W) 5. Find the names of employees who work in all departments ename ((eno,dno(W) / dno((D)) ⋈ E)
  • 38. 6. Find the names of employees who work in all departments in Bombay T1:= eno,dno(E)/ dno(σcity=Bombay(W ⋈ D)  ename(T1 ⋈ E) 7. Find names of employees who work in a department which is in Bombay or Delhi T1:= σcity=Bombay (D) U σcity = Delhi (D) ename(T1 ⋈ W ⋈ E)
  • 39. 8 .Find names of employees who work in a department in Bombay and a department in Delhi: T1:= ename(σcity=Bombay (D) ⋈ W ⋈ E) T2:= ename(σcity=Delhi (D) ⋈ W ⋈ E) T1 ∩ T2 9. Find employee number of employees who do not work in dno #2 eno(E) -- eno((σdno=2 W)) 10. Find eno’s for employees who work in two different departments in Bombay: eno(σW1.dno≠W2.dno σW1.city=W2.city(ρw1(W ⋈ D) X ρw2 (W ⋈ D))
  • 40. Theta Join () Let R and S be two relations. Consider an attribute x in R, and an attribute y in S. The theta join of these two relations can be written as, R x  y S where  indicates a valid relational operator.
  • 41. Equijoin ( = )  The most widely used join operation is equijoin. As discussed in the previous subsection, when  is =, this type of -join (a special case) is called equijoin. We shall use the notation to denote equijoin. The general form for this kind of join is, R <Join_condition> S <Join_condition> must always have = operator
  • 42.  Query-16: Retrieve the name and salary of the manager of each department. Managers  Department MgrSSN = SSN Employee Gnames  Name, Salary (Managers)  Query-17: Find which project(s) and location(s) Pooja is working on. EmpPooja  Name = 'Pooja' (Employee) PoojaPrj  EmpPooja SSN = ESSN WorksOn Result   Name, PName, PLocation (PoojaPrj WrksOn.PNo = Project.PNumber Project)
  • 43. Outer Joins  Dangling tuples in Join  Usually, only a subset of tuples of each relation will actually participate in a join, i.e. only tuples that match with the joining attributes.  Tuples of a relation not participating in a join are called dangling tuples.  How do we keep dangling tuples in the result of a join? Use null values to indicate a no-join situation. There are three types of outer joins - left outer join - right outer join and - outer join (full outer join).
  • 44.  Left outer-join R1 R2 is similar to a natural join but keep all dangling tuples of R1.  Right Outer-Join R1 R2 is similar to a natural join but keep all dangling tuples of R2.  Outer Join (full outer-join) R1 R2 is similar to a natural join but keep all dangling tuples of both R1 and R2. The advantages of outer join is to take the union of tuples from two relations that are not union compatible.
  • 45. Example RegNo Name GPA Branch RegNo Amount Year 1BI01CS056 Pooja 5.6 CSE 1BI01CS056 10000 2002 1BI01ME067 Pallavi 8.9 MECH 1BI01IS001 20000 2003 1BI01CS045 Reena 9.5 CSE 1BI01IS001 Kumar 6.0 ISE RegNo Name GPA Branch Amount Year 1BI01CS056 Pooja 5.6 CSE 10000 2002 1BI01ME067 Pallavi 8.9 MECH null null 1BI01CS045 Reena 9.5 CSE null null 1BI01IS001 Kumar 6.0 ISE 20000 2003 Students Awards Students Awards Dangling tuples
  • 46. Division  Consider two relations R and S. Assume that R has only two attributes r1 and r2 and S has only one attribute r2 with the same domain as in R. This is to ensure that the degree of the numerator is more than the degree of the denominator. Now we shall define R / S as,  For each r1 value in R, consider the set of r2 values that appear in tuples of R with that r2 value. If this set contains S, the r1 is in the result of R / S.  One possible restriction in this definition is that every attribute in S should be in R. Alternatively, we can define R / S as, For, R(a1, a2, …., an, b1, b2, …., bn) / S(b1, b2, …., bn) and T = a1, a2, …., an (R), return the subset of T, say W, such that every tuple in W × S is in R. W is the largest subset of T, such that, (W × S)  R
  • 47. Example Options RegNo SubId 412 CS175 412 CS272 412 CS351 413 CS175 413 CS272 532 CS175 676 CS272 SubId CS175 CS272 SubId CS175 SubId CS175 CS272 CS351 RegNo 412 413 RegNo 412 413 532 RegNo 412 Electives1 Electives2 Electives3 Options/Electives1 Options/Electives2 Options/Electives3
  • 48. GROUPING AND AGGREGATE FUNCTIONS  We shall define Function formally using the symbol  (pronounced as script F ) as, grouping_attributes  aggregate_functions (Relation)  The steps involved in the evaluation of this function is,  Partition the relation into groups.  Apply aggregate function to each group.  Output group and aggregate values, one tuple per group.
  • 49.  Query-18: Retrieve department number, number of employees and their average salary. Result (DNo, N, AvgSal)   DNo  Count(SSN), Avg(Salary) (Employee)  R1(SSN,NO_OF_DEPENDENTS)ESSN  COUNT DEPENDENTNAME (DEPENDENT)  R2<- σ NO_OF_DEPENDENTS>=2(R1)  R3<-  LNAME,FNAME(R2*EMPLOYEE)
  • 50. Relational Database Design by ER-to-Relational Mapping
  • 51.  ER-to-Relational Mapping Algorithm  Step 1: Mapping of Regular Entity Types  Step 2: Mapping of Weak Entity Types  Step 3: Mapping of Binary 1:1 Relation Types  Step 4: Mapping of Binary 1:N Relationship Types.  Step 5: Mapping of Binary M:N Relationship Types.  Step 6: Mapping of Multivalued attributes.  Step 7: Mapping of N-ary Relationship Types.
  • 52. ER-to-Relational Mapping Algorithm  Step 1: Mapping of Regular Entity Types.  For each regular (strong) entity type E in the ER diagram, create a relation R that includes all the simple attributes of E.  Choose one of the key attributes of E as the primary key for R.  If the chosen key of E is composite, the set of simple attributes that form it will together form the primary key of R.  Example: We create the relations EMPLOYEE, DEPARTMENT, and PROJECT in the relational schema corresponding to the regular entities in the ER diagram.  SSN, DNUMBER, and PNUMBER are the primary keys for the relations EMPLOYEE, DEPARTMENT, and PROJECT as shown.
  • 53. 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 & include all simple attributes (or simple components of composite attributes) of W as attributes of R.  Also, include as foreign key attributes of R the primary key attribute(s) of the relation(s) that correspond to the owner entity type(s).  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.
  • 54.  Example: Create the relation DEPENDENT in this step to correspond to the weak entity type DEPENDENT.  Include the primary key SSN of the EMPLOYEE relation as a foreign key attribute of DEPENDENT (renamed to ESSN).  The primary key of the DEPENDENT relation is the combination {ESSN, DEPENDENT_NAME} because DEPENDENT_NAME is the partial key of DEPENDENT
  • 55. Step 3: Mapping of Binary 1:1 Relation 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: 1. Foreign Key approach: Choose one of the relations-say S- and include a foreign key in S the primary key of T. It is better to choose an entity type with total participation in R in the role of S.  Example: 1:1 relation MANAGES is mapped by choosing the participating entity type DEPARTMENT to serve in the role of S, because its participation in the MANAGES relationship type is total.
  • 56. 2.Merged relation option: An alternate mapping of a 1:1 relationship type is possible by merging the two entity types and the relationship into a single relation. This may be appropriate when both participations are total. 3.Cross-reference or relationship relation option: The third alternative 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.
  • 57. Step 4: Mapping of Binary 1:N Relationship Types.  For each regular binary 1:N relationship type R, identify the relation S that represent the participating entity type at the N-side of the relationship type.  Include as foreign key in S the primary key of the relation T that represents the other entity type participating in R.  Include any simple attributes of the 1:N relation type as attributes of S.  Example: 1:N relationship types WORKS_FOR, CONTROLS, and SUPERVISION in the figure.  For WORKS_FOR we include the primary key DNUMBER of the DEPARTMENT relation as foreign key in the EMPLOYEE relation and call it DNO.
  • 58.  Step 5: Mapping of Binary M:N Relationship Types.  For each regular 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.  Also include any simple attributes of the M:N relationship type (or simple components of composite attributes) as attributes of S.
  • 59.  Example: The M:N relationship type WORKS_ON from the ER diagram is mapped by creating a relation WORKS_ON in the relational database schema.  The primary keys of the PROJECT and EMPLOYEE relations are included as foreign keys in WORKS_ON and renamed PNO and ESSN, respectively.  Attribute HOURS in WORKS_ON represents the HOURS attribute of the relation type. The primary key of the WORKS_ON relation is the combination of the foreign key attributes {ESSN, PNO}.
  • 60. Step 6: Mapping of Multivalued attributes.  For each multivalued 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 of relationship type that has A as an attribute.  The primary key of R is the combination of A and K. If the multivalued attribute is composite, we include its simple components.  Example: The relation DEPT_LOCATIONS is created.  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 R is the combination of {DNUMBER, DLOCATION}.
  • 61. Step 7: Mapping of N-ary Relationship Types.  For each n-ary relationship type R, where n>2, create a new relationship S to represent R.  Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types.  Also include any simple attributes of the n-ary relationship type (or simple components of composite attributes) as attributes of S.  Example: The relationship type SUPPY in the ER on the next slide.  This can be mapped to the relation SUPPLY shown in the relational schema, whose primary key is the combination of the three foreign keys {SNAME, PARTNO, PROJNAME}
  • 62. Ternary relationship types. (a) The SUPPLY relationship.
  • 63. Mapping the n-ary relationship type SUPPLY
  • 64. Summary of Mapping constructs and constraints Correspondence between ER and Relational Models ER Model Relational Model Entity type “Entity” relation 1:1 or 1:N relationship type Foreign key (or “relationship” relation) M:N relationship type “Relationship” relation and two foreign keys n-ary relationship type “Relationship” relation and n foreign keys Simple attribute Attribute Composite attribute Set of simple component attributes Multivalued attribute Relation and foreign key Value set Domain Key attribute Primary (or secondary) key
  • 65. Mapping Exercise FIGURE 7.7 An ER schema for a SHIP_TRACKING database.
  • 66. The ER conceptual schema diagram for the COMPANY database.
  • 67. Result of mapping the COMPANY ER schema into a relational schema.
  • 68. Chapter Summary  ER-to-Relational Mapping Algorithm  Step 1: Mapping of Regular Entity Types  Step 2: Mapping of Weak Entity Types  Step 3: Mapping of Binary 1:1 Relation Types  Step 4: Mapping of Binary 1:N Relationship Types.  Step 5: Mapping of Binary M:N Relationship Types.  Step 6: Mapping of Multivalued attributes.  Step 7: Mapping of N-ary Relationship Types.
  • 69. One possible database state for the COMPANY relational database schema (Fig. 5.6)
  • 70. One possible database state for the COMPANY relational database schema – continued (Fig. 5.6)
  • 71.   ⋈    R1(PNO)<- PNUMBER(DNUM=5(PROJECT))  R2(SSN,PNO)<-ESSN,PNO(WORKSON)  R3<-R2÷R1  RESULT<-NAME(R3*EMPLOYEE)
  • 72. Retrieve the name of employess who have no dependents  R1<-SSN(EMPLOYEE)  R2<-ESSN(DEPENDENT)  R3<-R1-R2  RESULT<-FNAME,LNAME(R3*EMPLOYEE)
  • 73. List the names of managers who have atleast one dependent  R1<-MGRSSN(DEPARTMENT)  R2<-ESSN(DEPENDENT)  R3<-R1∩R2  RESULT<-LNAME,FNAME(R3*EMPLOYEE)
  • 74.  Make a list of project numbers for project that involve an employee who last name is ‘Smith’ ,either as a worker or as a manager of the departments that controls the project.  R1<- SSN(LNAME=‘SMITH’(EMPLOYEE)  R2<- PNO(WORKS*R1) R3<- DNO(R1⋈R1.SSN=DEPT.MGRSSN (DEPT)) R4<- DNO(LNAME=‘SMITH’(R3) R5<- PNO(R3*PROJECT) RESULT<-R2ᶸR5
  • 75.  For every project located in ‘Stafford’, list the project number, the controlling dept no and the dept manager’s last name, address and birth date.  R1<- Ploc=‘Stafford’(Project)  R2<-(R1 ⋈DNUM=DNUMBER DEPARTMENT)  R3<-(R2 ⋈MGRSNN=SSN EMPLOYEE) RESULT<- PNO,DNUM,LNAME,ADD,BDATE(R3)
  • 76.   ⋈  R1(PNO)<- PNUMBER(DNUM=5(PROJECT))  R2(SSN,PNO)<-ESSN,PNO(WORKSON)  R3<-R2÷R1  RESULT<-NAME(R3*EMPLOYEE)
  • 77. SAILORS BOAT SID SNAME RATING AG E 401 RAJ 10 25 402 RAVI 20 30 403 MAHESH 30 35 404 SATISH 40 40 BID BNAM E COLO R 100 INTER LAKE RED 101 INTER LAKE GREEN 103 MARIN E RED 104 CLIPPE R GREEN 105 CLIPPE R BLACK 102 clipper brown SID BID DAY 401 100 MONDAY 401 101 TUESDAY 402 102 WEDNES DAY 403 103 MONDAY RESERVE
  • 78. 1.Find the names of sailors who have reserved boat 103.  R1<- bid=103(Reserve)  R2<- sname(R1 ⋈sid=sid Sailors) 2.Find the names of sailors who have reserved a red boat.  R1<- BID(COLOR=RED(BOAT))  R2<-(R1 ⋈BID=BID RESERVE)  R3<- NAME(R2 ⋈SID=SID (SAILORS))
  • 79. 3.Find the names of sailors who have reserved a red or green boat  R1<- COLOR=RED(BOAT)  R2<- SID(R1 ⋈Bid=Bid RESREVE)  R3<- COLOR=GREEN(BOAT)  R4<- SID(R1 ⋈Bid=Bid RESREVE)  R5<-R2ᶸR4  RESULT<- SNAME(R5*SAILORS)
  • 80. R1<- SID(COLOR=RED(BOAT))⋈Bid=Bid RESREVE) R2<-  SID(COLOR=GREEN(BOAT))⋈Bid=Bid RESREVE) R3<-R1ᶸR2 RESULT<- SNAME(R3*SAILORS)
  • 81. 4.Find the colors of boats reserved by mahesh. Result<-color((sname=mahesh(Sailors) ⋈Reserve ⋈Boats) Or