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))
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}
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!
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))
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
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)
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}
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
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)
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)