3 Relational Model
3 Relational Model
Introduction
Proposed by Edgar. F. Codd (1923-2003) in the
early seventies. [ Turing Award – 1981 ]
Most of the modern DBMS are relational.
Simple and elegant model with a mathematical basis.
Led to the development of a theory of data
dependencies and database design.
Relational algebra operations –
crucial role in query optimization and execution.
Laid the foundation for the development of
Tuple relational calculus and then
Database standard SQL
1
Relation Scheme
Consists of relation name, and a set of attributes or
field names or column names. Each attribute has an
associated domain.
Example:
student : string,
( studentName : string,
rollNumber : integer,
Relation phoneNumber
yearOfAdmission : integer,
name branchOfStudy :
string )
Attribute domains
names
Domain – set of atomic (or indivisible ) values – data type
2
Relation Instance
A finite set of tuples constitute a relation instance.
A tuple of relation with scheme R = (A1, A2, … ,
Am) is an ordered sequence of values
(v1,v2, ... ,vm) such that vi ∈ domain (Ai), 1≤ i ≤
m
yearOf phoneNumber branch
student
studentName rollNumber
Admission Of Study
Ravi Teja CS05B015 2005 9840110489 CS
Rajesh CS04B125 2004 9840110490 EC
…
enrollment
studentName rollNumber courseNo sectionNo
4
Keys for a Relation (1/2)
• Key: A set of attributes K, whose values uniquely identify a
tuple in any instance. And none of the proper subsets of K
has this property
Example: {rollNumber} is a key for student relation.
{rollNumber, name} – values can uniquely identify a tuple
• but the set is not minimal
• not a Key
• A key can not be determined from any particular instance
data
it is an intrinsic property of a scheme
it can only be determined from the meaning of
attributes
5
Keys for a Relation (2/2)
A relation can have more than one key.
Each of the keys is called a candidate key
Example: book (isbnNo, authorName, title, publisher, year)
(Assumption : books have only one author )
Keys: {isbnNo}, {authorName, title}
A relation has at least one key
- the set of all attributes, in case no proper subset is a key.
Superkey: A set of attributes that contains any key as a subset.
A key can also be defined as a minimal superkey
Primary Key: One of the candidate keys chosen for indexing
purposes ( More details later…)
6
Relational Database Scheme and Instance
Relational database scheme: D consist of a finite no. of
relation schemes and a set I of integrity constraints.
Integrity constraints: Necessary conditions to be satisfied by
the data values in the relational instances so that the set
of data values constitute a meaningful database
• domain constraints
• key constraints
• referential integrity constraints
Database instance: Collection of relational instances satisfying
the integrity constraints.
7
Domain and Key Constraints
• Domain Constraints: Attributes have associated domains
Domain – set of atomic data values of a specific type.
Constraint – stipulates that the actual values of an attribute in
any tuple must belong to the declared domain.
8
Foreign Keys
• Tuples in one relation, say r1(R1), often need to refer to tuples
in another relation, say r2(R2)
• to capture relationships between entities
• Primary Key of R2 : K = {B1, B2, …, Bj}
• A set of attributes F = {A1, A2, …, Aj} of R1 such that
dom(Ai) = dom(Bi), 1≤ i ≤ j and
whose values are used to refer to tuples in r2
is called a foreign key in R1
referring to R2.
Course Department
courseId name credits deptNo deptId name hod phone
10
Foreign Key – Examples(2/2)
It is possible for a foreign key in a relation
to refer to the primary key of the relation itself
An Example:
11
Referential Integrity Constraint (RIC)
• Let F be a foreign key in scheme R1 referring to scheme R2
and let K be the primary key of R2.
• RIC: any relational instance r1on R1, r2 on R2 must be s.t
for any tuple t in r1, either its F-attribute values are null
or they are identical to the K-attribute values of some
tuple in r2.
• RIC ensures that references to tuples in r2 are for currently
existing tuples.
• That is, there are no dangling references.
12
Referential Integrity Constraint (RIC) - Example
COURSE DEPARTMENT
CE751 MASS 3 4
TRANSFER
13
Example Relational Scheme
student (rollNo, name, degree, year, sex, deptNo, advisor)
Here, degree is the program ( B Tech, M Tech, M S, Ph D
etc) for which the student has joined. Year is the year of
admission and advisor is the EmpId of a faculty member
identified as the student’s advisor.
14
Example Relational Scheme
course (courseId, cname, credits, deptNo)
Here, deptNo indicates the department that offers the course.
16
Example Relational Scheme with RIC’s shown
student (rollNo, name, degree, year, sex, deptNo, advisor)
17
Relational Algebra
A set of operators (unary and binary) that take relation
instances as arguments and return new relations.
Gives a procedural method of specifying a retrieval
query.
Forms the core component of a relational query
engine.
SQL queries are internally translated into RA
expressions.
Provides a framework for query optimization.
19
Selection Condition
• Select condition:
Basic condition or Composite condition
• Basic condition:
Either Ai <compOp> Aj or Ai <compOp> c
• Composite condition:
Basic conditions combined with logical operators
AND, OR and NOT appropriately.
• Notation:
<compOp> : one of < , ≤ , > , , = ,
Ai, Aj : attributes in the
scheme R of r
c: constant of 20
appropriate data type
Examples of select expressions
1. Obtain information about a professor with name
“giridhar”
21
The project Operator
Unary operator.
Can be used to keep only the required attributes of a
relation instance and throw away others.
Notation: A ,A , … ,A (r ) where A1,A2, … ,Ak is a list
L of
1 2 k
r.
Result = { (v1,v2, … ,vk) | vi ∈ dom(Ai) , 1≤ i
≤ k and
there is some tuple t in r
s.t
22
t.A1 = v1, t.A2 = v2, … ,
Examples of project expressions
student
rollNo name degree year sex deptNo advisor
CS04S001 Mahesh M.S 2004 M 1 CS01
CS03S001 Rajesh M.S 2003 M 1 CS02
CS04M002 Piyush M.E 2004 M 1 CS01
ES04M001 Deepak M.E 2004 M 2 ES01
ME04M001 Lalitha M.E 2004 F 3 ME01
ME03M002 Mahesh M.S 2003 M 3 ME01
ES04M001 Deepak
ME04M001 Lalitha
Note: Mahesh is displayed only once because
ME03M002 Mahesh
project operation results in a set.
23
Size of project expression result
If r1 = L(r2) then scheme of r1 is L
What about the number of tuples in r1?
Two cases arise:
Projection List L contains some key of r2
Then |r1| = |r2|
Projection List L does not contain any key of r2
Then |r1| ≤ |r2|
24
Set Operators on Relations
• As relations are sets of tuples, set operations are applicable
to them; but not in all cases.
• Union Compatibility : Consider two schemes R1, R2 where
R1 = (A1, A2, …, Ak) ; R2 = (B1, B2, …, Bm)
• R1 and R2 are called union-compatible if
• k = m and
• dom(Ai) = dom(Bi) for 1 ≤ i ≤ k
• Set operations – union, intersection, difference
• Applicable to two relations if their schemes are
union-compatible
• If r3 = r1 ⋃ r2 , scheme of r3 is R1 (as a
convention)
25
Set Operations
r1 - relation with scheme R1
r2 - relation with scheme R2 - union compatible with R1
r1 ⋃ r2 = {t | t ∈ r1 or t ∈ r2};
r1 ∩ r2 = {t | t ∈ r1 and t ∈ r2}
r1 − r2 = {t | t ∈ r1 and t ∉ r2};
26
Cross product Operation
r1 r2
r1 A 1 ... Am
A2 ... a1m A1 A2 ... Am B1 B 2 ...
... a1 m Bn ... b1 n
... a2m
a21
11 a22 a 11 a 12 b11 b12
a12
... asm a 11 a 12 ... a1 m b 21 b 22 ... b 2 n
as1 as . .
. .
r21 : s tuples a11 a12 ... a 1 bt bt 2 ... btn
m 1
r2 B1 B2 ... Bn a 21 a 22 ... a 2 m b11 b12 ... b1
b12 ... b1n a 21 a 22 ... a 2 m b 21 b22 ... b 2 n
n
b11
. .
b b ... b2n . .
21
22 ... btn ... a 2 m ... btn
a 21 a bt 1 bt 2
brt1: t btuples 22 .
2
t .
2
.
r1 r2 : s t tuples
27
Example Query using cross product
Obtain the list of professors along with the name of their
departments
28
Join Operation
• Cross product : produces all combinations of tuples
• often only certain combinations are meaningful
• cross product is usually followed by selection
29
Theta join
• Let r1 - relation with scheme R1 = (A1,A2,…,Am)
r2 - relation with scheme R2 = (B1,B2,…,Bn)
and R1 ∩ R2 =
• Notation for join expression : r1 ⋈ r2 , - join
condition
is of the form : C1 ^ C2 ^ … ^ Cs
Ci is of the form : Aj <CompOp> Bk
<CompOp> : = , ≠, < , ≤ , > , ≥
• Scheme of the result relation
Q = (A1,A2,…,Am,B1,B2,…,Bn)
• r = {(a1,a2,…,am,b1,b2,…,bn) (a1,a2,…,am) r1,
(b1,b2,…,bn) r2 and (a1,a2,…,am , b1,b2,…,bn) satisfies }
30
Professor
empId name sex startYear deptNo phone
CS01 GIRIDHAR M 1984 1 22576345
Courses
courseId cname credits deptNo
Department
CS635 Algorithms 3 1
deptId name hod phone
CS636 A.I 4 1
1 Computer Science CS01 22576235
ES456 D.S.P 3 2
2 Electrical Engg. ES01 22576234 ME650 Aero 3 3
Dynamics
3 Mechanical Engg. ME01 22576233
31
Examples
For each department, find its name and the name, sex and phone number
of the head of the department.
Prof (empId, p-name, sex, deptNo, prof-phone)
← empId, name, sex, deptNo, phone
(professor)
Result ←
deptId name (Department
hod ⋈
p-name sex prof-phone
Prof) 1 Computer CS01 Giridher (empIdM 22576235
DeptId, name, hod, p-name, sex, prof-phone = hod) ^ (deptNo = deptId)
Science
2 Electrical EE01 Rajiv M 22576234
Engg. Guptha
3 Mechanical ME01 Tahir M 22576233
Engg. Nayyar
32
Equi-join and Natural join
• Equi-join : Equality is the only comparison operator used in the
join condition
• Natural join : R1, R2 - have common attributes, say X1,X2,X3
• Join condition:
(R1.X1 = R2.X1) ^ (R1.X2 = R2.X2) ^ (R1.X3 = R2.X3)
• values of common attributes should be equal
• Schema for the result Q = R1 ⋃ (R2- {X1, X2, X3 })
•Only one copy of the common attributes is kept
33
Examples – Equi-join
Find courses offered by each department
34
Teaching
empId courseId sem year classRoom
CS01 CS635 1 2005 BSB361
CS02 CS636 1 2005 BSB632
ES01 ES456 2 2004 ESB650
ME650 ME01 1 2004 MSB331
Murthy
ES01 Rajiv M 1989 2 22576244 ES456 2 2004 ESB650
Guptha
ME01 Tahir M 1999 3 22576243 ME650 1 2004 MSB331
Nayyar
35
Division operator
The necessary condition to determine r s
on instances r(R) ands(S) is S ⊆ R
The relation r s is a relation on schema R –
S. A tuple t is in r s if and only if
1) t is in R-S
2)
(r) For every tuple ts in s, there is tr in r satisfying both
a) tr [S] = ts
b) tr [R – S] = t
• Another Definition r = r1 r2
Division operator produces a relation R(X) that includes all
tuples t[X] in R1(Z) that appear in R1 in combination with
every tuple from R2(Y) where Z = X ⋃ Y
36
R = (A, B, C, D), S = (A, B), X = (C, D)
x=r s
r A B C D
s A B
a1 b1 c1 1
a 1 b1
d2 a b 2 c1 1
a 2 b2 a bd c2
1 1 2
a1 db1 c3 d 3
x C D a 2 b2 c3 d 3
c1 d1
c3 d3
(c2, d2) is not present in the result of division as it does not appear
in combination with all the tuples of s in r
37
Query using division operation
Find those students who have registered for all courses offered
in dept of Computer Science.
deptNodept))
38
Suppose result of step 1 is result of step 2
studEnroll csCourse
name courseId courseId
Mahesh CS635 CS635
Mahesh CS636 CS636
Rajesh CS635
Piyush CS636
Piyush CS635
Deepak ES456
Lalitha ME650
Mahesh ME650
39
Complete Set of Operators
• Are all Relational Algebra operators essential ?
Some operators can be realized through other operators
40
Example Queries
Schema
Retrieve the list of female PhD students
41
More Example Queries
Obtain the department Ids for departments with no lady
professor
42
Another Example Query Schema
Obtain the names, roll numbers of students who have got S grade in
the CS370 course offered in 2006 odd semester along with his/her
advisor name.
reqStudsRollNo ←
rollNo(courseId =‘CS370’& year =‘2006’& semester = ‘odd’&grade=‘S’(enrollment))
43
Transitive Closure Queries Schema
levelTwoPrereq(cId2) ←
preReqCourse (preRequisite ⋈ courseId = cId1 levelOnePrereq))
The set of operations called outer joins are used when all
tuples in relation r or relation s or both in r and s have
to be in result.
45
Outer Join Operation (2/2)
46
Instance Data for Examples
Student
rollNo name degree year sex deptNo advisor
CS04S001 Mahesh M.S 2004 M 1 CS01
CS05S001 Amrish M.S 2003 M 1 null
CS04M002 Piyush M.E 2004 M 1 CS01
ES04M001 Deepak M.E 2004 M 2 null
ME04M001 Lalitha M.E 2004 F 3 ME01
ME03M002 Mahesh M.S 2003 M 3 ME01
Professor
empId name sex startYear deptNo phone
CS01 GIRIDHAR M 1984 1 22576345
47
Left outer join
temp ← (student
advisor = empId professor)
( (temp))
rollNo, name, advisor rollNo, student.name, professor.name
48
Right outer join
temp ←
professor)
(student advisor = empId
( (temp))
rollNo, name, advisor rollNo, student.name, professor.name
49
Full outer join
temp ← (student
advisor = empId professor)
( (temp))
roll no, name, advisor roll No, student.name, professor.name
Result
rollNo name advisor
CS04S001 Mahesh Giridhar
CS04M002 Piyush Giridhar
CS05S001 Amrish Null
null null Keshav Murthy
ES04M001 Deepak Null
null null Rajiv Guptha
ME04M001 Lalitha Tahir Nayyer
ME03M002 Mahesh Tahir Nayyer
50
E/R diagrams to Relational Schema
E/R model and the relational model are logical representations
of real world enterprises
For each entity set and relationship set in E/R diagram we can
have a corresponding relational table with the same name as
entity set / relationship set
Each table will have multiple columns whose names are obtained
from the attributes of entity types/relationship types
51
Relational representation of strong entity sets
Create a table Ti for each strong entity set Ei.
52
Relational representation of weak entity sets
Let E' be a weak entity owned by a strong/weak entity E
E' is converted to a table, say R'
Attributes of R' will be
Attributes of the weak entity set E' and
Primary key attributes of the identifying strong entity E
(Or, partial key of E + primary key of the
owner of E, if E is itself a weak entity)
• These attributes will also be a foreign key in
R' referring to the table corresponding to E
54
Relational representation of multi-valued attributes
One table for each multi-valued attribute
Student
55
Handling Binary 1:1 Relationship
Let S and T be entity sets in relationship R and S', T' be the
tables corresponding to these entity sets
Student Hostel
RollNo Name homeAddress RoomId RoomNo HostelName address
57
Handling 1:N Relationship
Let S be the participating entity on the N-side and T the other
entity. Let S' and T' be the corresponding tables.
58
Example
Name
Phone ProfID RollNo
Name
Student Professor
RollNo Name guide ProfId Name phone
59
Handling M:N relationship
Make a separate table T for this relationship R between entity
sets E1 and E2.
Let R1 and R2 be the tables corresponding to E1 and E2.
M N
E1 R E2
R1 T R2
PK1 FK1 FK2 PK2
60
Example
Name
Name CourseID
RollNo
61
Handling Recursive relationships
Make a table T for the participating entity set E
( this might already be existing)
and one table for recursive relationship R.
CourseTable
Example
is CourseID Credits Timing
M N
PreReq
Of
Course PreRequisiteTable
Timing
preReqCourse CourseID
CourseID
Credits
62