Relational Model
Relational Model
mywbut.com 3
Another Relation Example
enrollment (studentName, rollNo, courseNo, sectionNo)
studentName rollNumber courseNo
sectionNo
enrollment
Rajesh
Rajesh
Suresh
CS04B125
CS04B125
CS04B130
2
1
2
CS320
CS370
CS320
mywbut.com 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
mywbut.com 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)
mywbut.com 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.
mywbut.com 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.
Key Constraint: Relation scheme associated keys
Constraint if K is supposed to be a key for scheme R,
any relation instance r on R should not have two tuples
that have identical values for attributes in K.
Also, none of the key attributes can have null value.
mywbut.com 8
Foreign Keys
Tuples in one relation, say r
1
(R
1
), often need to refer to tuples
in another relation, say r
2
(R
2
)
to capture relationships between entities
Primary Key of R
2
: K = {B
1
, B
2
, , B
j
}
A set of attributes F = {A
1
, A
2
, , A
j
} of R
1
such that
dom(A
i
) = dom(B
i
), 1 i j and
whose values are used to refer to tuples in r
2
is called a foreign key in R
1
referring to R
2
.
R
1
, R
2
can be the same scheme also.
There can be more than one foreign key in a relation scheme
mywbut.com 9
Foreign Key Examples(1/2)
Foreign key attribute deptNo of course relation refers to
Primary key attribute deptID of department relation
Course Department
courseId name credits deptNo
CS635 ALGORITHMS 3 1
CS636 A.I 4 1
ES456 D.S.P 3 2
ME650 AERO
DYNAMIC
3 3
deptId name hod phone
1
COMPUTER
SCIENCE
CS01 22576235
2
ELECTRICAL
ENGG
ES01 22576234
3
MECHANICAL
ENGG
ME01 22576233
mywbut.com 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:
univEmployee ( empNo, name, sex, salary, dept, reportsTo)
reportsTo is a foreign key referring to empNo of the same relation
Every employee in the university reports to some other
employee for administrative purposes
- except the vice-chancellor, of course!
mywbut.com 11
Referential Integrity Constraint (RIC)
Let F be a foreign key in scheme R
1
referring to scheme R
2
and let K be the primary key of R
2
.
RIC: any relational instance r
1
on R
1
, r
2
on R
2
must be s.t
for any tuple t in r
1
, either its F-attribute values are null
or they are identical to the K-attribute values of some
tuple in r
2
.
RIC ensures that references to tuples in r
2
are for currently
existing tuples.
That is, there are no dangling references.
mywbut.com 12
Referential Integrity Constraint (RIC) - Example
courseId name credits deptNo
CS635 ALGORITHMS 3 1
CS636 A.I 4 1
ES456 D.S.P 3 2
ME650 AERO
DYNAMIC
3 3
CE751 MASS
TRANSFER
3 4
deptId name hod phone
1
COMPUTER
SCIENCE
CS01 22576235
2
ELECTRICAL
ENGG.
ES01 22576234
3
MECHANICAL
ENGG.
ME01 22576233
COURSE DEPARTMENT
The new course refers to a non-existent department and thus
violates the RIC
mywbut.com 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 students advisor.
department (deptId, name, hod, phone)
Here, phone is that of the departments office.
professor (empId, name, sex, startYear, deptNo, phone)
Here, startYear is the year when the faculty member has
joined the department deptNo.
mywbut.com 14
Example Relational Scheme
course (courseId, cname, credits, deptNo)
Here, deptNo indicates the department that offers the course.
enrollment (rollNo, courseId, sem, year, grade)
Here, sem can be either odd or even indicating the two
semesters of an academic year. The value of grade will
be null for the current semester and non-null for past
semesters.
teaching (empId, courseId, sem, year, classRoom)
preRequisite (preReqCourse, courseID)
Here, if (c1, c2) is a tuple, it indicates that c1 should be
successfully completed before enrolling for c2.
mywbut.com 15
Example Relational Scheme
student (rollNo, name, degree, year, sex, deptNo, advisor)
department (deptId, name, hod, phone)
professor (empId, name, sex, startYear, deptNo, phone)
course (courseId, cname, credits, deptNo)
enrollment (rollNo, courseId, sem, year, grade)
teaching (empId, courseId, sem, year, classRoom)
preRequisite (preReqCourse, courseID)
queries-1
queries-2
queries-3
TCQuery
mywbut.com 16
Example Relational Scheme with RICs shown
student (rollNo, name, degree, year, sex, deptNo, advisor)
department (deptId, name, hod, phone)
professor (empId, name, sex, startYear, deptNo, phone)
course (courseId, cname, credits, deptNo)
enrollment (rollNo, courseId, sem, year, grade)
teaching (empId, courseId, sem, year, classRoom)
preRequisite (preReqCourse, courseID)
mywbut.com 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.
RA operations: select (), project (), cross product (),
union (), intersection (), difference (), join ( )
mywbut.com 18
The select Operator
Unary operator.
can be used to select those tuples of a relation that
satisfy a given condition.
Notation:
( r )
: select operator ( read as sigma)
: selection condition
r : relation name
Result: a relation with the same schema as r
consisting of the tuples in r that satisfy condition
Select operation is commutative:
c1
(
c2
( r ) ) =
c2
(
c1
( r ) )
mywbut.com 19
Selection Condition
Select condition:
Basic condition or Composite condition
Basic condition:
Either A
i
<compOp> A
j
or A
i
<compOp> c
Composite condition:
Basic conditions combined with logical operators
AND, OR and NOT appropriately.
Notation:
<compOp> : one of < , , > , , = ,
A
i
, A
j
: attributes in the scheme R of r
c : constant of appropriate data type
mywbut.com 20
Examples of select expressions
1. Obtain information about a professor with name
giridhar
name = giridhar
(professor)
2. Obtain information about professors who joined the
university between 1980 and 1985
name
(
degree = M.S
(student))
name
Mahesh
Rajesh
Note: Mahesh is displayed only once because
project operation results in a set.
rollNo, name
(student)
mywbut.com 23
Size of project expression result
If r
1
=
L
(r
2
) then scheme of r
1
is L
What about the number of tuples in r
1
?
Two cases arise:
Projection List L contains some key of r
2
Then |r
1
| = |r
2
|
Projection List L does not contain any key of r
2
Then |r
1
| |r
2
|
mywbut.com 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 R
1
, R
2
where
R
1
= (A
1
, A
2
, , A
k
) ; R
2
= (B
1
, B
2
, , B
m
)
R
1
and R
2
are called union-compatible if
k = m and
dom(A
i
) = dom(B
i
) for 1 i k
Set operations union, intersection, difference
Applicable to two relations if their schemes are
union-compatible
If r
3
= r
1
r
2
, scheme of r
3
is R
1
(as a convention)
mywbut.com 25
Set Operations
r
1
- relation with scheme R
1
r
2
- relation with scheme R
2
- union compatible with R
1
r
1
r
2
= {t | t r
1
or t r
2
};
r
1
r
2
= {t | t r
1
and t r
2
}
r
1
r
2
= {t | t r
1
and t r
2
};
By convention, in all the cases, the scheme of the result
is that of the first operand i.e r
1
.
mywbut.com 26
Cross product Operation
11 12 1 11 12 1
11 12 1 21 22 2
. .
. .
11 12 1 1 2
... ...
... ...
... ...
m n
m n
m t t tn
a a a b b b
a a a b b b
a a a b b b
21 22 2 11 12 1
21 22 2 21 22 2
. .
. .
21 22 2 1 2
... ...
... ...
... ...
m n
m n
m t t tn
a a a b b b
a a a b b b
a a a b b b
1 2 1 2
... ...
m n
A A A B B B
1 1 2
...
m
r A A A
11 12 1
21 22 2
...
...
m
m
a a a
a a a
1 2
...
s s sm
a a a
2 1 2
...
n
r B B B
11 12 1
21 22 2
...
...
n
n
b b b
b b b
1 2
...
t t tn
b b b
r
1
: s tuples
r
2
: t tuples
r
1
r
2
r
1
r
2
: s t tuples
.
.
.
mywbut.com 27
Example Query using cross product
Obtain the list of professors along with the name of their
departments
profDetail (eId, pname,deptno)
empId, name, deptNo
(professor)
deptDetail (dId,dname)
deptId, name
(department)
profDept profDetail deptDetail
desiredProfDept
deptno = dId
(profDept)
result
eld, pname, dname
(desiredProfDept)
mywbut.com 28
Join Operation
Cross product : produces all combinations of tuples
often only certain combinations are meaningful
cross product is usually followed by selection
Join : combines tuples from two relations provided they
satisfy a specified condition (join condition)
equivalent to performing cross product followed by
selection
a very useful operation
Depending on the type of condition we have
theta join
equi join
mywbut.com 29
Theta join
Let r
1
- relation with scheme R
1
= (A
1
,A
2
,,A
m
)
r
2
- relation with scheme R
2
= (B
1
,B
2
,,B
n
)
and R
1
R
2
=
Notation for join expression : r
1
r
2
, - join condition
is of the form : C
1
^ C
2
^ ^ C
s
C
i
is of the form : A
j
<CompOp> B
k
<CompOp> : = , , < , , > ,
Scheme of the result relation
Q = (A
1
,A
2
,,A
m
,B
1
,B
2
,,B
n
)
r = {(a
1
,a
2
,,a
m
,b
1
,b
2
,,b
n
) (a
1
,a
2
,,a
m
) r
1
,
(b
1
,b
2
,,b
n
) r
2
and (a
1
,a
2
,,a
m
, b
1
,b
2
,,b
n
) satisfies }
mywbut.com 30
empId name sex startYear deptNo phone
CS01
GIRIDHAR
M 1984 1 22576345
CS02
KESHAV
MURTHY
M 1989 1 22576346
ES01
RAJIV GUPTHA
M 1980 2 22576244
ME01
TAHIR
NAYYAR
M 1999 3 22576243
Professor
Department
deptId name hod phone
1 Computer Science CS01 22576235
2 Electrical Engg. ES01 22576234
3 Mechanical Engg. ME01 22576233
courseId cname credits deptNo
CS635 Algorithms 3 1
CS636 A.I 4 1
ES456 D.S.P 3 2
ME650 Aero
Dynamics
3 3
Courses
mywbut.com 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)
mywbut.com 36
R = (A, B, C, D), S = (A, B), X = (C, D)
x = r s
1 1
2 2
A B
a b
a b
1 1 1 1
2 2 1 1
1 1 2 2
1 1 3 3
2 2 3 3
A B C D
a b c d
a b c d
a b c d
a b c d
a b c d
s
r
x
(c
2
, d
2
) is not present in the result of division as it does not appear
in combination with all the tuples of s in r
c
1
d
1
c
3
d
3
C D
mywbut.com 37
Find those students who have registered for all courses offered
in dept of Computer Science.
Step1: Get the course enrollment information for all students
studEnroll
name, courseId
(student * enrollment)
Step2: Get the course Ids of all courses offered by CS dept
csCourse
courseId
(
dname = computer science
(courses
deptId = deptNo
dept))
Result : studEnroll csCourse
Query using division operation
Schema
mywbut.com 38
Suppose result of step 1 is
name courseId
Mahesh CS635
Mahesh CS636
Rajesh CS635
Piyush CS636
Piyush CS635
Deepak ES456
Lalitha ME650
Mahesh ME650
courseId
CS635
CS636
result of step 2
studEnroll csCourse
result
studEnroll
csCourse
name
Mahesh
Piyush
rollNo, name
(
degree = BTech ^ sex = F
(student))
Obtain the rollNo of students who never obtained an E grade
rollNo
(
grade
E
(enrollment)) is incorrect!!
(what if some student gets E in one course and A in another?)
rollNo
(student)
rollNo
(
grade = E
(enrollment))
Schema
mywbut.com 41
Obtain the department Ids for departments with no lady
professor
deptId
(dept)
deptId
(
sex = F
(professor))
Obtain the rollNo of girl students who have obtained
at least one S grade
rollNo
(
sex = F
(student))
rollNo
(
grade = S
(enrollment))
More Example Queries
mywbut.com 42
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))
reqStuds-Name-AdvId ( rollNo, sName, advId)
preReqCourse
(preRequisite
courseId = cId1
levelOnePrereq))
Similarly, level k prerequisites can be obtained.
But, prerequisites at all levels can not be obtained as there is
no looping mechanism.
Transitive Closure Queries
Schema
mywbut.com 44
Theta join, equi-join, natural join are all called inner joins . The
result of these operations contain only the matching tuples
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.
There are 3 kinds of outer joins:
Left outer join
Right outer join
Full outer join
Outer Join Operation (1/2)
mywbut.com 45
Left outer join: r s
It keeps all tuples in the first, or left relation r in the result. For
some tuple t in r, if no matching tuple is found in s then
S-attributes of t are made null in the result.
Right outer join: r s
Same as above but tuples in the second relation are all kept in
the result. If necessary, R-attributes are made null.
Full outer join: r s
All the tuples in both the relations r and s are in the result.
Outer Join Operation (2/2)
mywbut.com 46
Instance Data for Examples
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
Student
empId name sex startYear deptNo phone
CS01
GIRIDHAR
M 1984 1 22576345
CS02
KESHAV
MURTHY
M 1989 1 22576346
ES01
RAJIV GUPTHA
M 1980 2 22576244
ME01
TAHIR
NAYYAR
M 1999 3 22576243
Professor
mywbut.com 47
Left outer join
temp (student
advisor = empId
professor)