Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Introduction to Relational Algebra

Relational Database Management System


(RDBMS)
Recall: Database Management System (DBMS)
•DBMS is an application, which holds user data
permanently and then provide different operations on
this data e.g., retrieval of data, insertion of data, updation
of data etc.
•It is a computerized system whose overall purpose is to
maintain information and to make that information
available on demand.
 RDBMS is an enhancement to DBMS. It is a system in which:
The data is perceived by the user as tables; and
 The operators (or operations) at the user's disposal include atleast
RESTRICT (SELECT), PROJECT and JOIN.

 Relational Database:

A database in which data is perceived as tables


 It is a prescription for a way of representing data by
means of tables and a prescription for a way of
manipulating such a representation using some operators.
 It is an abstract theory of data that is based on certain
aspects of mathematics like set theory and predicate
logic.
 The principles of relational model were originally laid
down in 1969-70 by Dr. E.F.Codd.
 Relation
 Tuple, Attribute
 Cardianlity, Degree
 Domain

attributes Employee Relation

ID Name Age Department


S1 Ahmad 23 Sales
S2 Salman 34 Marketing
tuples S3 Karim 21 Sales Cardinality
S4 Tariq 29 Admin
S5 Sadiq 32 Sales

Degree
 There are no duplicate tuples
 Tuples are unordered
 Attributes are unordered
 All attributes have atomic values
 The sequence of attributes within a relation is of no
importance.
 Candidate key
◦ General definition:
 A set of attributes which can uniquely identify each row in the table
◦ Relational Model Definition:
 Let R be a relation. Then candidate key for R is a subset of the set
of attributes of R say K, such that:
1 Uniqueness Property:
no two distinct tuples of R have the same value for K.
2 Irreducibility property:
no proper subset of K has the uniqueness propert.

ID Name Age Department NIC


S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371

Possible Candidate Keys:


• ID
• NIC
 Primary key
◦ is a unique identifier for the table, that is , a column or
column combination with the property that, at any given time,
no two rows of the table contain same value in that column or
column combination.
◦ One of the candidate keys
 Alternate Keys
◦ All candidate keys other than primary key are called alternate
keys

ID Name Age Department NIC


S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371

Primary Key: ID
Alternate Key: NIC
 Foreign key
◦ General definition:
 A set of attributes in a table whose values are taken from the values
of candidate key of some other table
◦ Relational Model Definition:
 Let R2 be a relation. Then a foreign key in R2 is a subset is a subset
of the set of attributes of R2, say FK, such that:
1 there exists a base relation R1 (R1 and R2 not necessarily distinct)
with a candidate key CK and
2 for all time, each value of FK in the current value of R2 is identical
to the value of CK in some tuple in the current value of R1

ID Name Age Department NIC Department Location


S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368 Sales Floor 1
S3 Karim 21 Sales 255-79-256369 Marketing Floor 3
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371 Admin Floor 5

Same values
 Referenced tuple or Target tuple
 Referencing relation
 Referenced relation or Target relation
 Simple key vs Composite key

Referencing relation Target relation


Target tuple

ID Name Age Department NIC Department Location


S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368 Sales Floor 1
S3 Karim 21 Sales 255-79-256369 Marketing Floor 3
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371 Admin Floor 5
 Integrity constraints ensure that the data is
accurate. Every attribute has an associated
domain, there are constraints(called domain
constraints) that form restrictions on the set
of values allowed for the attributes of
relations.
 Null represents a value for an attribute that
is currently unknown or is not applicable for
this tuple.
 In a base relation no attribute of a primary
key can be null.
 A primary key is a minimal identifier that is
used to identify tuples uniquely. This means
that no subset of primary key is sufficient to
provide unique identification of tuples.
 If a foreign key exists in a relation, the
foreign key value must match a candidate
key value of some tuple in its home
relation.
 Possible cases: update,delete
 Operators
◦ Relational Algebra consists of eight operators:
 Four traditional set operators: UNION, INTERSECTION,
DIFFERENCE, CARTESIAN PRODUCT
 Four special relational operators: RESTRICTION, PROJECTION,
JOIN, DIVISION
 Operands
◦ Relations are the operands used in Relational Algebra
 Closure property of Relations
◦ output from some relational operator is always a relation.
 UNION
◦ the union of two type-compatible relations A and B (A
UNION B) is a relation with the same heading as each of
A and B and with a body consisting of the set of all
tuples belonging to A and B.
◦ relations must have same heading
◦ same tuples are included once

B
 UNION
◦ Returns a relation consisting of all tuples appearing in
either or both relations

A
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S4 Tariq 29 Admin 245-71-325370 B
ID Name Age Department NIC
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
A UNION B
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
 INTERSECTION
◦ the intersection of two type-compatible relations A and
B (A INTERSECT B) is a relation with the same heading as
each of A and B and with a body consisting of the set of
all tuples belonging to both A and B.
◦ relations must have same heading

B
 INTERSECTION
◦ Returns a relation consisting of all tuples appearing
in both of two specified relations

ID Name Age Department NIC


S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S4 Tariq 29 Admin 245-71-325370
B

ID Name Age Department NIC


S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371

A INTERSECT B
ID Name Age Department NIC
S4 Tariq 29 Admin 245-71-325370
 DIFFERENCE
◦ the difference between two type-compatible relations A
and B, in that order (A MINUS B) is a relation with the
same heading as each of A and B and with a body
consisting of the set of all tuples belonging to A and not
to B.
◦ relations must have same heading
◦ direction of operation does matter

B
 DIFFERENCE
◦ Returns a relation consisting of all tuples appearing in
the first and not in the second of two specified relations
A
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S4 Tariq 29 Admin 245-71-325370 B
ID Name Age Department NIC
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
A MINUS B
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
B MINUS A
ID Name Age Department NIC
S3 Karim 21 Sales 255-79-256369
S5 Sadiq 32 Sales 245-68-345371
 PRODUCT (CARTESIAN PRODUCT)
◦ the product of two relations A and B with no common
attribute, (A TIMES B) is a relation with a same heading as
set of all attributes in each of A and B and with a body
consisting of the set of all tuples such that each resulting
tuple is combination of a tuple from A and a tuple from
B.
◦ Cardinality of resulting relation is equal to the product of
the cardinalities of A and B.
◦ Degree of resulting relation is equal to the sum of the
degrees of A and B.
 Returns a relation consisting of all possible tuples that are
a combination of two tuples, one from each of two
specified relations.
 PRODUCT (CARTESIAN PRODUCT)
A B

ID Name Age Department NIC


S1 Ahmad 23
Sales 255-79-256369
S2 Salman 34 Admin 245-71-325370
S4 Tariq 29 Sales 245-68-345371

A TIMES B
ID Name Age Department NIC
S1 Ahmad 23 Sales 255-79-256369
S1 Ahmad 23 Admin 245-71-325370
S1 Ahmad 23 Sales 245-68-345371
S2 Salman 34 Sales 255-79-256369
S2 Salman 34 Admin 245-71-325370
S2 Salman 34 Sales 245-68-345371
S4 Tariq 29 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S4 Tariq 29 Sales 245-68-345371
ID LAST_NAME FIRST_NAME USERID SALARY
1 Patel Ralph rpatel 795
2 Dancs Betty bdancs 860
3 Biri Ben bbiri 1100
4 Newman Chad cnewman 750
5 Ropeburn Audry aropebur 1550

 Q1: Identify the possible candidate keys in the


given relation?
 Q2: There are 4 candidate keys identified in a
relation each having 12 elements. What would
be the cardinality of the relation. Also write the
lowest possible value of degree for that relation.
 Q3: Explain meaning of RESTRICTED and
CASCADE in case of update for a foreign key.
 Q4: Give an example of repeating group from
keeping in view the relation given above.
 Commutative
◦ Union, Intersect and Times are Commutative:
 A UNION B is equal to B UNION A
 A INTERSECT B is equal to B INTERSECT A
 A TIMES B is equal to B TIMES A
◦ MINUS is not Commutative i.e.,
 A MINUS B is not equal to B MINUS A
 Associative
◦ Union, Intersect and Times are Associative:
 (A UNION B) UNION C is equal to A UNION (B UNION C)
 (A INTERSECT B) INTERSECT C is equal to A INTERSECT (B
INERSECT C)
 (A TIMES B) TIMES C is equal to A TIMES (B TIMES C)
◦ MINUS is not Associative i.e.,
 (A MINUS B) MINUS C is not equal to A MINUS (B MINUS C)
 RESTRICTION
◦ is actually abbreviation for θ-restriction, where “θ” stands for
any simple scalar comparison operator(=,<,>,=)
◦ θ-restriction of relation A on attributes X and Y( in that order)
A where X θ Y
is a relation with the same heading as A and with a
body consisting of the set of all tuples of A such that the
condition “X θ Y” evaluates true for those tuples.
◦ X and Y must be defined on the same domain
◦ operator must make sense for that domain

A
 RESTRICTION
◦ Returns a relation consisting of all tuples from a
specified relation that satisfy a specified condition.

A
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371

A WHERE Department=“Sales”
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S3 Karim 21 Sales 255-79-256369
S5 Sadiq 32 Sales 245-68-345371
 PROJECTION
◦ the projection of relation A on X,Y,….,Z (where each of
X,Y,….,Z is an attribute of A) is a relation with heading
{X,Y,…,Z } and body consisting of the set of all tuples
{X:x,Y:y,…,Z:z } such that a tuple appears in A with X-
value x, Y-value y,…., Z-value z.
◦ projection yields a vertical subset of relation

A
 PROJECTION
◦ Returns a relation consisting of all tuples that remain as
(sub) tuples in a specified relation after specified
attributes have been eliminated

A
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371

A [Name] A where
Department=“Sales”
Name
[Name,Department]
Name Age Department
Ahmad
Salman Ahmad 23 Sales
Karim Karim 21 Sales
Tariq Sadiq 32 Sales
Sadiq
 JOIN(NATURAL JOIN)
◦ Let relations A and B have headings [X,…,Y] and
[X,…,Z] respectively, then
A JOIN B
is a relation with heading [X,…,Y,…,Z] and with a
body consisting of the set of all tuples [X:x,Y:y,Z:z]
such that a tuple appears in A with X-value x and Y-
value y and in B with X-value x and Z-value z.
◦ there should be some common attribute(s)

a1 b1 b1 c1
a2 b2 b2 c2
a3 b2 b3 c3

JOIN

a1 b1 c1
a2 b2 c2
a3 b2 c2
 JOIN(NATURAL JOIN)
◦ Returns a relation consisting of all possible tuples that are
combination of two tuples, one from each of the two
specified relations, such that two tuples contributing to
any given combination have a common value for the
common attributes (and that value appears just once)

A B

ID Name ID Subject
S1 Ahmad S1 Math
S2 Salman S2 Urdu
S3 Karim S1 English

A JOIN B

ID Name Subject
S1 Ahmad Math
S1 Ahmad English
S2 Salman Urdu
 θ-JOIN
◦ Let relations A and B have no attribute names in
common (as in Cartesian Product), and let θ be as
defined in restriction. Then the θ- JOIN of relation A
on attribute X with relation B on attribute Y is defined
by the result of the expression
(A TIMES B) where X θ Y
◦ it is a relation with same heading as Cartesian Product
of A and B and with a body consisting of the set of all
those tuples belonging to that Cartesian Product of
that evaluate true for X θ Y.
◦ if θ is “equals”, the θ-JOIN is called an EQUIJOIN.

a1 b1 b1 c1
a2 b2 θ-JOIN b2 c2
a3 b2 b3 c3

a2 b2 b1 c1
a3 b2 b1 c1
 θ- JOIN
A B
ID Name ID Subject
S1 Ahmad S1 Math
S2 Salman S2 Urdu
S3 Karim S1 English

(A TIMES B) where IDA > IDB


IDA Name IDB Subject
S2 Salman S1 Math
S2 Salman S1 English
S3 Karim S1 Math
S3 Karim S1 English
S3 Karim S2 Urdu

(A TIMES B) where IDA = IDB

IDA Name IDB Subject


S1 Ahmad S1 Math
S1 Ahmad S1 English
S2 Salman S2 Urdu
 DIVISION
◦ Let relations A and B have headings
{ x1, x2, …, xm, y1, y2,…, yn} and
{y1, y2,…, yn}
respectively; i.e., attributes y1, y2,…, yn are common,
A additionally has attributes x1, x2, …, xm , B has no
other attributes. Then division of A by B
A DIVIDEDBY B
is a relation with heading x1, x2, …, xm and
body consisting of the set of all tuples
{X1:x1,X2:x2,…,Xm:xm } such that a tuple
{X1:x1,X2:x2,…,Xm:xm, Y1:y1,Y2:y2,…,Yn:yn } appears in A
for all tuples {Y1:y1,Y2:y2,…,Yn:yn } appearing in B.
◦ A is called dividend(DEND) and B is called divisor(DOR)

a1 b1
b1
a2 b2 DIVIDEDBY
b2
a1 b2

a1
 DIVISION
◦ Take two relations, one binary and one unary, and
returns a relation consisting of all values of one
attribute of binary relation that match(in the other
attribute) all the values in the unary relation.
A
ID Subject B
S1 Math
S2 Urdu ID
S1 English S1
S1 Urdu S2

A DIVIDEBY B

Subject
Urdu
 Some Facts
◦ If A and B have no common attribute names in
common, A JOIN B is equal to A TIMES B
◦ In EQUIJOIN, if one of the two attributes having
common values is eliminated (may be through
projection), then the result is equal to the natural join.
◦ JOIN is both commutative and associative i.e.,
A JOIN B = B JOIN A
A JOIN (B JOIN C) = (A JOIN B) JOIN C
 Consider relations A and B such that
◦ Cardinalty of A= 12, Degree of A= 4
◦ Cardinalty of B= 10, Degree of B= 4
◦ Cardinalty of A UNION B = 20
what would be the value of following:
◦ Cardinalty and Degree of A INTERSECT B
◦ Cardinalty and Degree of A TIMES B
◦ Degree of A UNION B
◦ Cardinalty and Degree of A MINUS B
◦ Cardinalty and Degree of B MINUS A

You might also like