You are on page 1of 13

The Relation Algebra and calculus

3. The RELATIONAL Algebra and Calculus


 Introduction

 Relational databases are the most widely used databases in commercial applications like as
banking system, railway-reservation system, etc.

 Relational databases are based on the relational model.

 Historical Perspective of Relational Model

 A relational data model was first introduced by Dr. Edgar F. Codd of IBM Research in 1970.
 He represented the overview of the relational model in his paper entitled "A Relational Model
of Data for Large Shared Data Banks"
 Popular commercial RDBMS for large databases include Oracle, Microsoft SQL Server, Sybase
SQL Server, and IBM's DB2.

 Structure of Relational Databases

 Domain :
 A domain of an attribute is a set of permitted values for that attribute
 For example, if the bank is organized in three branches, named 'vvn', 'ksad' and
'anand', then, the domain of attribute bname is {'vvn', 'ksad', 'anand'}.
 A value for a branch name must be one of these three values in any relation.

 Similarly, a domain for column ano in Account table is a set of strings starting from
'A followed by unique numerical value, such as {A01, A02, A03, A04, A05}.
 A domain is atomic if elements of domain are considered to be indivisible units.
 For example, a domain for bname is an atomic domain.
 A domain for some attribute, say address may not be atomic, as address can be divided into
sub-parts such as society, city, and pincode.
 A domain of an attribute should be atomic
 An atomic domain is preferable rather than a non-atomic domain.

http://gtustudymaterial.blogspot.in/
1
The Relation Algebra and calculus

 Keys of Relations

 Tuples in a relational model represents individual entities or relationships.


 Tuples are distinguished based on data values stored
 No any two tuples in a relation should have exactly the same values for all attributes.
 There cannot be two identical tuples in a relation.
 Several keys have been defined, as illustrated below.
 Super Key
 Relation Key
 Candidate Key
 Primary Key
 Alternate Key
 Foreign Key

 Super Key
 "A super key is a set of one or more attributes that allows to identify each tuple
uniquely in a relation."
 For example, consider a relation Customer with attributes cid, cname, address,
and contact_no.
 In this relation, each tuple represents an individual customer.
 Here, the cid attribute can distinguish each tuple from another.
 So, cid is a super key for Customer relation.

 Relation Key
 "A super key for which no subset is a super key is called a relation key."
 In other words, relation key is a minimal super key.
 A relation key is also referred as 'key' only.
 A relation key is sufficient to identify each and every tuple uniquely within a
relation.
 A relation key cannot have null or duplicate values, as well as it does not contain any
redundant attribute.
 There can be more than one relation keys for the same relation. For example, for
Customer relation, {cid} as well as {cname, address} are relation keys.

 Candidate Key
 "If a relation contains more than one relation keys, then, they each are called
candidate key."
 For example, for Customer relation, {cid} as well as {cname, address} are relation

http://gtustudymaterial.blogspot.in/
2
The Relation Algebra and calculus

keys. And so, they each are called candidate keys.


 Primary Key
 "A primary key is a candidate key that is chosen by the database designer to
identify tuples uniquely in a relation."
 For example, if database designers choose candidate key cid to identify all
customers uniquely, then, cid is a primary key of the relation Customer.

 Alternate Key
 "An alternate key is a candidate key that is not chosen by the database designer
to identify tuples uniquely in a relation."
 For example, if database designers choose candidate key cid to identify all customers
uniquely, then, another candidate key {cname, address} is an alternate key.

 Foreign Key
 "A foreign key is a set of one or more attributes whose values are derived from
the key attribute of another relation."
 “Explain to the ch-5”

 RELATIONAL ALGEBRA

 Relational algebra is a collection of formal operations acting on relations and producing relations
as results.
 Six basic operators
 select: 
 project: 
 union: 
 set difference: –
 Cartesian product: x
 Rename: 
 The operators take one or two relations as inputs and produce a new relation as a result.
 Relational algebra is a Procedural language.

http://gtustudymaterial.blogspot.in/
3
The Relation Algebra and calculus

 The Select Operation : (


 Operation : Selects tuples from a relation that satisfy a given condition
 Symbol:  (Sigma)
 Notation: p(r)
 p is called the selection predicate
 Operators: The following operators can be used to form a condition.
=, ?, <, =, >, =, A (AND), V (OR) Find out all
 Example: Find out all accounts which belong to branch named "vvn".
 The following query returns all tuples which contains "vvn" as a branch name
 (Account)
bname = “vvn” '

The resultant relation is given in following figure 3.6.

 The Project Operation (


 Operation : Selects specified attributes of a relation.
 Symbol :  (pi)
 Notation :  (attribute list) <Relation>
 Duplicate rows removed from result, since relations are sets
 The projection of R, denoted by  A, B,...R is the set of specified attributes A, B, ... of the relation R,
i.e. a vertical subset of R.
 EXAMPLE  STUDENT#,EXAM ASSESSMENTS is
OUTPUT:-
STUDEN EX
T# A
M
A101 60
A101 50
J326 70
 Example-3 : List out all account numbers having balance less than 7000.
 (ano) (balance < 7000 (Account))

http://gtustudymaterial.blogspot.in/
4
The Relation Algebra and calculus

Figure 3.7 : All account numbers having balance < 7000.

 The Natural Join Operation ( )


 The natural join operation simplifies such type of queries. It combines following three operations
into one operation. The natural join operation -
 Forms a Cartesian product on its argument relations,
 Performs a selection for equality check on common attributes to remove
unnecessary tuples, and
 Removes duplicate attributes.
 The natural join is denoted by symbol (JOIN).
 The notation to perform this operation can be given as
 Relation 1 Relation 2

Example : Combine only consistent information from Account and Branch relation.

As shown in figure 3.11, natural join operation yield only consistent and useful information. It
removes unnecessary tuples as well as duplicate attributes. This makes the retrieval of information
from multiple relations very easy and convenient.

http://gtustudymaterial.blogspot.in/
5
The Relation Algebra and calculus

 Outer Join Operation :


 An extension of the join operation that avoids loss of information.
 Computes the join and then adds tuples form one relation that does not match tuples in the other
relation to the result of the join.
 Uses null values:
 null signifies that the value is unknown or does not exist
 All comparisons involving null are (roughly speaking) false by definition.
 We shall study precise meaning of comparisons with nulls later

 Table name: Client

NAME ID
Rahul 10
Vishal 20

 Table name: Salesman

ID CITY
30 Bombay
20 Madras
40 Bombay

 Join Client Salesman

NAME ID CITY
Vishal 20 madras

 The outer join operation can be divided into three different forms :
 Left outer join ( )
 Right outer join ( )
 Full outer join ( )

 Left outer join ( )


 The left outer join retains all the tuples of the left relation even though there is no
matching tuple in the right relation.
 For such kind of tuple, the attributes of right relation will be padded with null in
resultant relation

http://gtustudymaterial.blogspot.in/
6
The Relation Algebra and calculus

 Left outer join client salesman

NAME ID CITY
Rahul 10 Null
Vishal 20 madras

 Right outer join ( )


 The right outer join retains all the tuples of the right relation even though there is no
matching tuple in the left relation.
 For such kind of tuple, the attributes of left relation will be padded with null in resultant
relation.
 Right outer join client salesman

NAME ID CITY
Null 30 Bombay
Vishal 20 madras
Null 40 Bombay

 Full outer join ( )


 The full outer join retains all the tuples of both of the relations. It also pads null
values whenever required.
 Full outer join client salesman

NAME ID CITY
Rahul 10 Null
Null 30 Bombay
Vishal 20 madras
Null 40 Bombay

 The Union Operation (U)


 The union operation requires that the involved relations must have the same attributes, i.e. the
relations are Union-compatible.
 Operation : Selects tuples those are in either or both of the relations.
 Symbol : U (union)
 Notation : Relation 1 U Relation2
 Requirement :

http://gtustudymaterial.blogspot.in/
7
The Relation Algebra and calculus

 Union must be taken between compatible relations.


 Relations R and S are compatible, if –
 Both have same arity, i.e. total number of attributes, and
 Domains of i tch attribute of R and S are same.-
 For example, in Fig 1.0, STUDENTS-1  STUDENTS-2 is equal to

STUDEN STUDENT- COURS


T# NAME E
A101 Astor APPCO
M
B334 Bernstein INFTEC
J326 Jones APPCO
M
A423 Astor APPCO
M

 The Set-Difference (Minus) Operation ( - ) :


 Operation: Selects tuples those are in one relation but not in another relation.
 Symbol : - (minus)
 Notation : Relation 1 - Relation2
 Requirement :
 Set-difference must be taken between compatible relations.
 Relations R and S are compatible, if -
- Both have same arity, i.e. total number of attributes, and
- Domains of i th attribute of R and S are same.

Example: List out all persons who are customers but not employees.

 (name) (Customer) -  (name) (Employee)

Figure 3.8: Name of persons who are customers but not employees

http://gtustudymaterial.blogspot.in/
8
The Relation Algebra and calculus

 The Set-Intersection Operation (∩)


 Operation: Selects tuples those are in both relations.
 Symbol : ∩ (minus)
 Notation : Relation 1 ∩ Relation2
 Requirement :
 Set-intersection must be taken between compatible relations.
 Relations R and S are compatible, if –
 Both have same arity, i.e. total number of attributes, and
 Domains of i th attribute of R and S are same.
 Example-6: List out all persons who are customers as well as employees.
 Query :  (name) (Customer) ∩  (name) (Employee)

 The Cartesian-Product Operation (X)


 Operation: Combines information of two relations. It is also known as Cross-product
operation and similar to mathematical Cartesian-product operation.
 Symbol : X (cross)
 Notation : Relation 1 X Relation2
 Resultant Relation :
 If Relation 1 and Relation2 have n1 and n2 attributes respectively, then resultant
relation will have n1 + n2 attributes, combining attributes from both the input
relations.
 If both relations have some attribute having same name, it can be distinguished
by combining 'Relation-name .Attribute-name'.
 If Relation 1 and Relation2 have n1 and n2 tuples respectively, then resultant
relation will have n1 * n2 attributes, combining each possible pair of tuples from
both the input relations.

http://gtustudymaterial.blogspot.in/
9
The Relation Algebra and calculus

 Example-8: Combine only consistent information from Account and Branch relation.
 Query :  (Account X Branch)
Account.bname = Branch.bname '

 Division Operation (÷)


 The division operation is suited for a special kind of query. It is useful in queries that
include "for all" phrase.
 This operation is denoted by ÷ (division).
The following example given in figure 3.23 explains the division operation.

 Observe that for all tuples of S, all possible pairs - (al, b1) and (al, b2) - are present in R for
value al of attribute A. So, al is included in resultant relation T.
 Similarly, for all tuples of S, all possible pairs - (a3, b1) and (a3, b2) - are present in R for
value a3 of attribute A. So, a3 is also included in T.
 But, for a2, the combination (a2, b1) is present in R, but, (a2, b2) is not present. So, a2 is
not included in T.
http://gtustudymaterial.blogspot.in/
10
The Relation Algebra and calculus

 Similarly, for a4, the combination (a4, b2) is present in R, but, (a4, b1) is not present. So, a4 is
not included in T.

Example: Find out id numbers of employees who work on all the projects that 'Asmita' works.
Step-1 - Retrieve the list of projects on which 'Asmita' works.

http://gtustudymaterial.blogspot.in/
11
The Relation Algebra and calculus

Fig 1.0
STUDENT-1
STUDEN STUDENT- COURS
T# NAME E
A101 Astor APPCO
M
B334 Bernstein INFTE
C
J326 Jones APPCO
M

STUDENT-2
STUDEN STUDENT- COURS
T# NAME E
J326 Jones APPCO
M
A423 Astor APPCO
M

MODULE
MODUL MODULE-
E# NAME
CS301 IS
CS302 DBS

ASSESSMENTS
STUDE MODU EX
NT # LE # A
M
A101 CS301 60
A101 CS302 50
J326 CS302 70

http://gtustudymaterial.blogspot.in/
12
The Relation Algebra and calculus

http://gtustudymaterial.blogspot.in/
13

You might also like