0% found this document useful (0 votes)
71 views34 pages

Week04 - Relational Languages

The document discusses relational languages and relational algebra. Relational languages include relational algebra and relational calculus, which are formal but non-user friendly languages. Relational algebra is a procedural language that uses operations like selection, projection, union, and join to manipulate relations without changing the original relations. Common relational algebra operations and their usage are explained, including selection, projection, union, cartesian product, and different types of joins.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views34 pages

Week04 - Relational Languages

The document discusses relational languages and relational algebra. Relational languages include relational algebra and relational calculus, which are formal but non-user friendly languages. Relational algebra is a procedural language that uses operations like selection, projection, union, and join to manipulate relations without changing the original relations. Common relational algebra operations and their usage are explained, including selection, projection, union, cartesian product, and different types of joins.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Database I

Background

Relational Languages

1
Relational Languages
 Relational languages comprises of
 Relational Algebra
 Relational Calculus
 Both Relational Algebra and Calculus are
formal, non-user friendly languages
 They have been used as the basis for other
higher level languages like DML

2
Relational Algebra
 Relational Algebra
 Procedural language (user tell the system how
to manipulate the data)
 Relational algebra operations work on one or
more relations to define another relation
without changing the original relations
 Both operands and results are relations, so
output from one operation can become input
to another operation
 Note: Relations are sets of tuples 3
Relational Algebra (Cont.)
 Allows expressions to be nested, just as in
arithmetic. This property is called closure
 Five basic operations in relational algebra:
 Selection
 Projection
 Cartesian product
 Union, and
 Set Difference
 Also have Join, Intersection, and Division
operations, which can be expressed in terms
4
of 5 basic operations
Relational Algebra (Cont.)
 Selection / Restriction (predicate (R))
 selects tuples that satisfy a given predicate
 denoted by a lowercase Greek sigma (), with the
predicate appearing as a subscript. The argument
relation is given in parentheses
 For example, to select tuples (rows) of the Student
relation where the RollNo is “23”, we would write
RollNo = 23 (Student)
RollNo Name
RollNo Name 22 Asad
23 Jamal 23 Jamal
5
Relational Algebra (Cont.)
 Projection (col1, . . . , coln(R))
 Works on a single relation R and defines a relation
that contains a vertical subset of R, extracting the
values of specified attributes and eliminating
duplicates
 Projection is denoted by the Greek capital letter pi ()
 For example, to show the names of all students from
the previous Student relation, we would write
 Name (Student) Name
Asad
6
Jamal
Relational Algebra (Cont.)
 We can also combine the select and projection as
 Name ( RollNo = 23 (Student) ) Name
Jamal

7
Relational Algebra (Cont.)
 Union ( R  S )
 Union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R
and S, duplicate tuples being eliminated
 If R and S have i and j tuples, respectively, union
is obtained by concatenating them into one
relation with a maximum of (i + j) tuples
 For a union operation to be legal,we require that
 R and S must have the same number of attributes
 The domains of the corresponding attributes must be
the same 8
Relational Algebra (Cont.)
 For example to find all BID AccNo CName Amount
customers of the 100
branch, we must find 100 2346 Jamal 10000
everyone who has a loan 200 2345 Kamal 30000
or an account or both at
the branch 345 2312 Haider 1000
 So we need both borrow Deposit
and deposit relations for BID LoanNo CName Amount
this:
100 17 Kamal 100
CName (BID = 100 (Borrow))
345 23 Kamal 200

CName (BID = 100 (Deposit)) 200 5 Jamal 50
CName Borrow 9
Relational Algebra (Cont.)
 Set Difference (R – S)
 Defines a relation consisting of the tuples that are
in relation R, but not in S
 For Example; To find customers of the 100th
branch who have an account there but no loan,
we write
CName (BID = 100 (Deposit))

CName (BID = 100 (Borrow))
10
Relational Algebra (Cont.)
 Cartesian product (R X S)
 The result of cross product of R and S is a new relation
with a tuple for each possible pairing of tuples from R and
S
 To find the clients of banker Asad and the city in which
they live, we need information in both client and customer
relations CName Banker
Banker=Asaf (Client X Customer) Ahmad Asad
Karim Jamal
CName City
Ahmad Psh
Karim Islm 11
Relational Algebra (Cont.)

12
Relational Algebra (Cont.)
 Join Operations
 Join is a derivative of Cartesian product
 Join is equivalent of using a select operation on
the cartesion product of R and S
 One of the most difficult operations to implement
efficiently in an RDBMS and one reason why
RDBMSs have intrinsic performance problems

13
Relational Algebra (Cont.)
 Various forms of join operation
 Theta join
 Equijoin (a particular type of Theta join)
 Natural join
 Semijoin (all the above are also called inner
joins)
 Outer join (Left outer join)

14
Relational Algebra (Cont.)
 Theta join (-join) R FS
 Defines a relation that contains tuples satisfying
the predicate F from the Cartesian product of R
and S
 The predicate F is of the form R.ai  S.bi where 
may be one of the comparison operators (<, , >,
, =, )
 Can be rewrite Theta join using basic Selection
and Cartesian product operations as
R FS = F(R x S)
 If predicate F contains only equality (=), the term
Equijoin is used 15
Relational Algebra (Cont.)
Dep
 Example of Theta Join ID Name
(ID, Name(Dep)) Dep.ID = Emp.ID 31 Sales
(ID, Name(Emp)) 32 Marketing
33 CS

Dep.ID Dep.Name Emp.Name Emp.ID Emp


31 Sales Asad 31 ID Name
31 Sales Jamal 31 31 Asad
31 Jamal
33 Kamal
34 Haider

16
Relational Algebra (Cont.)
 Natural Join (R S) ID Name

 An Equijoin of the two relations R 31 Sales Dep


and S over all common attributess.
32 Marketing
One occurrence of each common
attribute is eliminated from the 33 CS
result
 For example; ID Name
(ID, Name(Dep)) (ID, Name(Emp)) 31 Asad
Emp
31 Jamal
33 Kamal
ID Dep.Name Emp.Name
34 Haider
31 Sales Asad
31 Sales Jamal 17
Relational Algebra (Cont.)
 Left Outer Join ( R S)
 An outer join does not require each record in the
two joined tables to have a matching record in the
other table
 The result of a left outer join for tables R and S
always contains all records of the "left" table (R),
even if the join-condition does not find any
matching record in the "right" table (S)
 This means that a left outer join returns all the
values from the left table, plus matched values
from the right table (or NULL in case of no
matching join predicate) 18
Relational Algebra (Cont.)
Dep
 Example of Left outer Join ID Name
(ID, Name(Dep)) 31 Sales
(ID, Name(Emp)) 32 Marketing
33 CS

Dep.ID Dep.Name Emp.Name Emp.ID Emp


31 Sales Asad 31 ID Name
31 Sales Jamal 31 31 Asad
32 Marketing NULL NULL 31 Jamal
33 CS NULL NULL 33 Kamal
34 Haider

19
Relational Algebra (Cont.)
Dep
 Right Outer Join ( R S) DepName Manager
 The result of the right outer Sales Salim
join is the set of all Production Nadeem
combinations of tuples in R
Emp
and S that are equal on their
Name EmpID DepName
common attribute names, in
addition to tuples in S that Jamal 3415 Finance
have no matching tuples in R Ajmal 2241 Sales
Karim 3401 Finance
((Dep)) ((Emp))
Kamal 2201 Sales
Name EmpID DepName Manager
Ajmal 2241 Sales Salim
Kamal 2201 Sales Salim
NULL NULL Production Karim 20
Relational Algebra (Cont.)
 Full Outer / Outer Join ( R S)
 The result of the full outer join is the set of all
combinations of tuples in R and S that are equal
on their common attribute names, in addition to
tuples in S that have no matching tuples in R and
tuples in R that have no matching tuples in S in
their common attribute names

((Dep)) ((Emp))

21
Relational Algebra (Cont.)
Dep
 Example Full Outer Join DepName Manager
Sales Salim
Name EmpID DepName Manager Production Nadeem
Ajmal 2241 Sales Salim Emp
Kamal 2201 Sales Salim Name EmpID DepName
Jamal 3415 Finance NULL Jamal 3415 Finance
Karim 3401 Finance NULL Ajmal 2241 Sales
NULL NULL Production Nadeem Karim 3401 Finance
Kamal 2201 Sales

22
Relational Algebra (Cont.)
 Semi Join (R F S)
 Similar of natural join
 The result of the semijoin is only the set of all
tuples in R for which there is a tuple in S that is
equal on their common attribute names
 Particular useful in distributed systems
 Can be re-written using Projection and Join as:
R F S = a1,…an(R F S)

23
Relational Algebra (Cont.)
Emp
 Example of Semi Join
Name EmpID DepName
(Emp)  (Dep) Jamal 3123 Finance
Ajmal 3224 Sales
Name EmpID DepName
Karim 3356 Production
Ajmal 3224 Sales Dep
Karim 3356 Production
DepName Manager
Sales Gul
Production Haleem

24
Relational Algebra (Cont.)
Std
 Intersection ( R  S )
Student
 Defines a relation consisting of the
set of all tuples that are in both R Zaheer
and S Sara
 Cab be expressed using basic Course
operations: CrName Student
R  S = R – (R – S) English Sara
Urdu Ajmal
Std  Course
Student
Sara
25
Relational Algebra (Cont.)
 Division ( R  S )
 Defines a relation over the attributes C that
consists of set of tuples from R that match
combination of every tuple in S
 Expressed using basic operations:
T1 = C(R)
T2 = C((S X T1) – R)
T = T1 – T 2

26
Relational Algebra (Cont.)
Completed
 Example of Division
Student Task
Zaheer Database1
Completed  DBProject
Zaheer Database2
Student Zaheer Compiler
Zaheer Karim Database1
Sara Karim Compiler
Sara Database1
DBProject Sara Database2
Task Gul Database1
Database1 Gul Compiler1
Database2 27
Relational Algebra (Cont.)

28
Relational Calculus
 Non-Procedural language
 Relational calculus query specifies what (non-
procedural) is to be retrieved rather than how
(procedural) to retrieve it
 It is not related to differential and integral
calculus
 Instead it gets its name from symbolic logic
called Predicate Calculus
 In predicate calculus (first-order logic),
predicate is a truth-valued function with
arguments 29
Relational Calculus (Cont.)
 When we substitute values for the arguments,
function yields an expression, called a proposition,
which can be either true or false
 If predicate contains a variable (e.g. ‘x is a member
of staff’), there must be a range for x
 When we substitute some values of this range for x,
proposition may be true; for other values, it may be
false
 When predicate calculus is applied to databases,
relational calculus has forms: tuple and domain
30
Relational Calculus (Cont.)
 Tuple Relational Calculus
 Used for finding tuples for which a predicate is
true. Based on use of tuple variables
 Tuple variable is a variable that ‘ranges over’ a
named relation: i.e., variable whose only
permitted values are tuples of the relation
 For example; Specify range of a tuple variable S
as the Staff relation as:
Staff(S)
 To find set of all tuples S such that P(S) is true:
31
{S | P(S)}
Relational Calculus (Cont.)
 Tuple Relational Calculus Example
 To find details of all staff earning more than
£10,000:
{S | Staff(S)  S.salary > 10000}
 To find a particular attribute, such as salary, write:
{S.salary|Staff(S)S.salary > 10000}
 Can use two quantifiers to tell how many
instances the predicate applies to:
 Existential quantifier  (‘there exists’)
 Universal quantifier  (‘for all’) 32
Relational Calculus (Cont.)
 Tuple variables qualified by " or $ are called bound
variables, otherwise called free variables
 Existential quantifier used in formula that must be true
for at least one instance, such as:
Staff(S)  (B)(Branch(B) 
(B.branchNo = S.branchNo)  B.city = ‘London’)
 Means ‘There exists a Branch tuple with same
branchNo as the branchNo of the current Staff tuple,
S, and is located in London’
 Universal quantifier is used in statements about every
instance, such as:
(B) (B.city  ‘Paris’)
 Means ‘For all Branch tuples, the address is not in33
Paris’
Relational Calculus (Cont.)
 Domain Relational Calculus
 Uses variables that take values from domains
instead of tuples of relations
 Domain Relational Calculus Example
 Find the names of all managers who earn more
than £25,000

{fN,lN|(sN, posn, sex, DOB, sal, bN)


(Staff (sN, fN, lN, posn, sex, DOB,
sal, bN) 
posn = ‘Manager’  sal > 25000)} 34

You might also like