0% found this document useful (0 votes)
15 views55 pages

CS2202_RelAlgebra

The document discusses query languages used in databases, specifically focusing on relational algebra and relational calculus. It outlines the basic operations of relational algebra, such as selection, projection, union, and joins, and provides examples of queries that can be executed using these operations. Additionally, it covers database modification operations like deletion, insertion, and updating, along with examples of how these can be applied.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views55 pages

CS2202_RelAlgebra

The document discusses query languages used in databases, specifically focusing on relational algebra and relational calculus. It outlines the basic operations of relational algebra, such as selection, projection, union, and joins, and provides examples of queries that can be executed using these operations. Additionally, it covers database modification operations like deletion, insertion, and updating, along with examples of how these can be applied.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Relational Algebra, Tuple and Domain

Relational Calculus
CS2202

1
Query Languages
• Language in which user requests information from the database
• Categories of languages
– Procedural:
• Relational algebra
– Non-procedural, or declarative:
• Tuple relational calculus, Domain relational calculus
• These languages form underlying basis of SQL query languages

2
Relational Algebra
• Procedural language
• Six basic operators
– Selection: 
– Projection: 
– Union: 
– Set difference: –
– Cartesian product: x
– Rename: 
• The operators take one or two relations as inputs and produce a new relation as a
result

3
Selection Operation
A B C D
• Relation r
  1 7
  5 7
  12 3
  23 10  ( A B )( D5) (r )

A B C D

  1 7
  23 10

4
Projection Operation
• Relation r

A B C

 10 1
 A,C (r )
 20 1
A C A C
 30 1
 1  1
 40 2
 1 =  1
 1  2
 2

5
Union Operation
• Relations r,s
A B

 1
 2
 1 A B
r rs  1
A B  2

 2  1

 3  3

s
6
Set Difference Operation
• Relations r,s

A B

 1
 2 A B
 1 rs
 1
r
 1
A B

 2
 3

s
7
Cartesian Product Operation
• Relations r,s

A B
A B C D E
 1
 1  10 a
 2  1  10 a
r rs  1  20 b
 1  10 b
C D E  2  10 a
 2  10 a
 10 a  2  20 b
 10 a  2  10 b
 20 b
 10 b

8
Rename Operation
• Allows us to name, and therefore to refer to, the results of relational-algebra
expressions
• Allows us to refer to a relation by more than one name
• Example:
 x (E)

returns the expression E under the name x


• If a relational-algebra expression E has arity n, then

 x( A , A
1 2 ,..., An ) ( E)
returns the result of expression E under the name x, and with the attributes
renamed to A1 , A2 , …., An .

9
Banking Example
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street,
customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)

10
Example Queries
• Find all loans of over Rs 2000
 am ount 2000(loan)

• Find the loan number for each loan of an amount greater than Rs 2000
 loan_ no ( amount2000(loan))
• Find the names of all customers who have a loan, an account, or both,
from the bank

 customer_ name(depositor )   customer_ name(borrower )

11
Example Queries
• Find the names of all customers who have a loan at Patliputra branch.

 customer_ name( (borrower.loan_ noloan.loan_ no) (borrower  loan))


 ( loan.branch Patliputra)

• Find the names of all customers who have a loan at Patliputra branch but do not
have an account at any branch of the bank.

 custom er_ nam e( ( borrower.loan _ noloan.loan _ no) (borrower  loan))


 ( loan.branch Patliputra)

  custom er_ nam e( depositor )

12
Some other operations
• Additional Operations

– Set intersection

– Natural join

– Outer Join

– Division

• The above operations can be expressed using basic operations we have seen
earlier

13
Set intersection operation
• Relations r,s
A B A B

 1  2
 2  3
 1

r s

A B

 2
r∩s

14
Set intersection operation (contd.)
• Set intersection operation can be built using other basic operations
• How?

r∩s=r-(r-s)

15
Natural join operation
• Cartesian product often requires a selection operation
• The selection operation most often requires that all attributes that are common to
the relations are involved in the Cartesian product be equated
• Steps for natural join-
1. Perform the Cartesian product of its two arguments
2. Perform a selection forcing equality on those attributes that appear in both
relational schemas
3. Finally remove the duplicate attributes

16
Natural join operation
• Relations r,s
A B C B D E
 1  1 a 
 4  r s 3 a 
 4  1 a 
 1  2 b 
 2  3 b 
r s
A B C D E
 1  a 
 1  a 
 1  a 
 1  a 
 2  b 
17
Natural join operation (contd.)
• A natural join operation can be rewritten as

r s   RS ( r . A1  s. A1 r . A2  s. A2 ...r . An  s. An (r  s))

• Theta join is a variant of natural join


• It is defined as
r θs    (r  s )

18
Assignment operation
• It is convenient at times to write relational algebra expression by assigning parts of
it to temporary relation variables
• The assignment operation works like assignment in programming languages
• We can rewrite as r s

temp1← r х s
temp2←  r . A1  s. A1 r . A2  s. A2 ...r . An  s. An (temp1))

result←  R S (temp 2)

19
Outer join operation
• An extension of the join operation that avoids loss of information
• Computes the join and then adds tuples from 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 false by definition.

20
Different forms of outer join
• Left outer join
– Includes the tuples from the left relation that did not match with any tuples in
the right relation
– Pads the tuples with null values for all other attributes from the right relation
– Adds them to the result of the natural join
• We can rewrite as r s
r s U (r-πR(r s)) x {(null, …, null)}

Here the constant relation {(null,…,null)} is on schema S-R

21
Different forms of outer join
• Similarly we can define-
• Right outer join
• Full outer join

22
Null value
• It is possible for tuples to have a null value, denoted by null, for some of their
attributes
• null signifies an unknown value or that a value does not exist
• Aggregate functions simply ignore null values (as in SQL)
• For duplicate elimination and grouping, null is treated like any other value, and
two nulls are assumed to be the same (as in SQL)

23
Division Operation
• Notation: r÷s
• Suited to queries that include the phrase “for all”.
• Let r and s be relations on schemas R and S respectively where
– R = (A1, …, Am , B1, …, Bn )
– S = (B1, …, Bn)
The result of r  s is a relation on schema
R – S = (A1, …, Am)
r  s = { t | t   R-S (r)   u  s ( tu  r ) }
Where tu means the concatenation of tuples t and u to produce a single
tuple
Find tuples in one relation that are associated with all tuples in another relation
24
Division operation (Contd.)
• Relations r,s Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S  R
A B B
 1
 2
1 r  s = R-S (r ) – R-S ( ( R-S (r ) x s ) – R-S,S(r ))
 3 2
A
 1
s
 1 
 1
 3 r÷s 
 4
 6
 1
 2
r
25
Example queries
• Find the names of all customers who have a loan and an account at bank.
• customer_name (borrower)  customer_name (depositor)
• Find the name of all customers who have a loan at the bank and the loan amount
– customer_name, amount (borrower loan)

26
Example Queries (contd.)

• Find the largest account balance in the bank


– balance (account) – account.balance ( σaccount.balance <d.balance (account x ρd
account))

27
Example Queries (contd.)
• Find the name of customers who have an account at all the branches located in
“Patna” city.

customer_name,branch_name (depositor account)


 branch_name (σbranch_city=“Patna” (branch))

28
Few more join operations
• Semi join
– The left semi-join is similar to the natural join
– The result of this semi-join is the set of all tuples in r for which there is a tuple
in s that is equal on their common attribute names
– r semiJoin s = ΠR(r s)
• Anti join
– It is similar to the natural join,
– but the result of an anti-join is only those tuples in r for which there
is no tuple in s that is equal on their common attribute names
– r antiJoin s = r – (r semiJoin s)

29
Generalized projection
• An extension of projection which allows operations such as arithmetic and string
functions to be used in the projection list
• ΠF1,F2,…,Fn(E)
– here F1,F2, …, Fn is an arithmetic expression involving constants and attributes
in the schema of E
• Example: ΠID,name,dept_name,salary/12(emp)
• Example: ΠID,(limit-balance) as credit_available(credit_info)

30
Modification of the Database
• The content of the database may be modified using the following operations:
– Deletion
– Insertion
– Updating
• All these operations are expressed using the assignment operator.

31
Deletion
• A delete request is expressed similarly to a query, except instead of displaying
tuples to the user, the selected tuples are removed from the database
• Can delete only whole tuples; cannot delete values on only particular attributes
• A deletion is expressed in relational algebra by:
rr–E
where r is a relation and E is a relational algebra query

32
Delete Example

• Delete all account records from “Patliputra” branch


– r1  branch_name=“Patliputra” (account depositor)
– depositor  depositor -  customer_name,account_number (r1 )
– account  account –  account_number, branch_name, balance (r1 )

 Delete all loan records with amount in the range of 0 to 50


– r2  amount>=0 and amount <= 50 (loan borrower)
– loan  loan – loan_no,branch_name,amount (r2)
– borrower  borrower – customer_name,loan_no (r2)

33
• Delete all accounts at branches located in city ‘Gaya’
r1  branch_city = “Gaya” (account branch )
r2   account_number, branch_name, balance (r1)
r3   customer_name, account_number (r2 depositor)
account  account – r2
depositor  depositor – r3

34
Insertion
• To insert data into a relation, we either:
– specify a tuple to be inserted or
– write a query whose result is a set of tuples to be inserted
• In relational algebra, an insertion is expressed by:
r r  E
where r is a relation and E is a relational algebra expression
• The insertion of a single tuple is expressed by letting E be a constant relation
containing one tuple

35
Insert Example
• Insert information in the database specifying that Sumit has Rs 1200 in account A-
973 at the Patliputra branch (assume Sumit’s information is available in Customer
relation)
account  account  {(“A-973”, “Patliputra”, 1200)}
depositor  depositor  {(“Sumit”, “A-973”)}
• Provide as a gift for all loan customers in the Patliputra branch, a Rs 200 savings
account. Let the loan number serve as the account number for the new savings
account
r1  (branch_name = “Patliputra” (borrower loan))
r2  (loan_number, branch_name, (r1))
account  account  (r2 x {(200)})
depositor  depositor  customer_name, loan_number (r1)
36
Updating
• A mechanism to change a value of an attribute of a tuple without changing all
attribute values of the corresponding tuple
• Use the generalized projection operator to do this task
r←ΠF1,F2,…,Fi(r)
Each Fi is either
– the ith attribute of r, if the ith attribute is not updated, or,
– if the attribute is to be updated Fi is an expression, involving only constants
and the attributes of r, which gives the new value for the attribute

37
Update Example
• Make interest payments by increasing all account balances by 5 percent
• account   account_number, branch_name, balance * 1.05 (account)
• Pay all accounts with balances over Rs1,00,000 a six percent interest and pay all
others five percent
• account   account_number, branch_name, balance * 1.06 ( balance  100000 (account ))
  account_number, branch_name, balance * 1.05 ( balance 100000 (account))

38
Tuple Relational Calculus

39
Tuple Relational Calculus
• A nonprocedural query language, where each query is of the form
{t | P (t ) }
– It is the set of all tuples t such that predicate P is true for t
– t is a tuple variable, t [A ] denotes the value of tuple t on attribute A
– t  r denotes that tuple t is in relation r
– P is a formula similar to that of the predicate calculus

40
Predicate Calculus Formula
 Set of attributes and constants
 Set of comparison operators: (e.g., , , , , , )
 Set of connectives: and (), or (v)‚ not ()
 Implication (): x  y, if x is true, then y is true
x  y x v y
 Set of quantifiers:
 t r (Q (t ))  “there exists” a tuple in t in relation r
such that predicate Q (t ) is true
 t r (Q (t )) Q(t) is true “for all” tuples t in relation r

41
Example Queries
• Find the loan_number, branch_name, and amount for loans of over Rs1200
– {t| t  loan  t[amount]> 1200}
• Find the loan number for each loan of an amount greater than Rs1200
– {t |  s loan (t [loan_number ] = s [loan_number ]  s [amount ]  1200)}
• Find the names of all customers having a loan, an account, or both at the bank
– {t | s  borrower ( t [customer_name ] = s [customer_name ])
 u  depositor ( t [customer_name ] = u [customer_name ])}

42
Example Queries (2)
• Find the names of all customers who have a loan and an account at the bank
– {t | s  borrower ( t [customer_name ] = s [customer_name ])
 u  depositor ( t [customer_name ] = u [customer_name] )}
• Find the names of all customers having a loan at Patliputra branch
– {t | s  borrower (t [customer_name ] = s [customer_name ]
 u  loan (u [branch_name ] = “Patliputra”
 u [loan_number ] = s [loan_number ]))}

43
Example Queries (3)
• Find the names of all customers who have a loan at Patliputra branch, but no
account at any branch of the bank
– {t | s  borrower (t [customer_name ] = s [customer_name ]
 u  loan (u [branch_name ] = “Patliputra”
 u [loan_number ] = s [loan_number ]))
 not v  depositor (v [customer_name ] =
t [customer_name ])}

44
Example Queries (4)
• Find the names of all customers having a loan from Patliputra branch, and the
cities in which they live
– {t | l  loan (l [branch_name ] = “Patliputra”
 b  borrower (b [loan_number ] = l [loan_number ]
 t [customer_name ] = b [customer_name ]
  c  customer (b [customer_name ] = c [customer_name ]
 t [customer_city ] = c [customer_city ])))}

45
Example Queries (5)
• Find the names of all customers who have an account at all branches located in
branch_city = “Dhanbad”:
– {t |  b  branch (b [branch_city ] = “Dhanbad”   a  account
(a [branch_name] = b [branch_name ]   d  depositor ( d[account_number ] =
a [account_number ]  ( t [customer_name ] = d [customer_name ])))}

46
Safety of Expression
• It is possible to write tuple calculus expressions that generate infinite relation.
– For example, { t |  t r } results in an infinite relation
• To guard against the problem, we restrict the set of allowable expressions to safe
expressions

47
Safe Expression
• Let’s consider an expression {t | P (t )} in the tuple relational calculus
– dom(P): the set of all values referenced by P
– They include the values mentioned in P itself, as well as values appear in a
tuple of a relation mentioned in P or constants that appear in P
– dom(t |t  customer( t [cust_city] = ‘Patna’ ) is the set containing “Patna” as
well as the values appearing in any attribute of any tuple in customer
• The expression is said to be safe if every component of t appears from the dom(P)
– E.g. { t |  t  customer( t [cust_city] = ‘Patna’ )} is not safe --- as the result is
an infinite set and some of the values may not appear in any relation or tuples
or constants in P.

48
Domain Relational Calculus

49
Domain Relational Calculus
• Another nonprocedural query language equivalent in power to the tuple relational
calculus
• Forms the basis of widely used QBE (Query By Example) language
• Each query is an expression of the form:

{  x1, x2, …, xn  | P (x1, x2, …, xn)}

– x1, x2, …, xn represent domain variables


– P represents a formula similar to that of the predicate calculus

50
Example Queries
• Find the loan_number, branch_name, and amount for loans of over Rs1200
– { l, b, a  |  l, b, a   loan  a > 1200}
• Find the names of all customers who have a loan of over Rs1200
– { c  |  l, b, a ( c, l   borrower   l, b, a   loan  a > 1200)}
• Find the names of all customers who have a loan from Patliputra branch and the
loan amount:
 { c, a  |  l ( c, l   borrower  b ( l, b, a   loan 
b = “Patliputra”))}
 { c, a  |  l ( c, l   borrower   l, “ Patliputra”, a   loan)}

51
Example Queries (2)
• Find the names of all customers having a loan, an account, or both at Patliputra
branch:
– { c  |  l (  c, l   borrower
  b,a ( l, b, a   loan  b = “Patliputra”))
  ac ( c, ac   depositor  br,ba ( ac, br, ba   account 
br = “Patliputra”))}

52
Example Queries (3)
• Find the names of all customers who have an account at all branches located in
Dhanbad:
• { c  |  x,y,z ( x, y, z   branch  y = “Dhanbad”) 
 a,b ( a, x, b   account   c,a   depositor)}

53
Safety of Expression
The expression:
{  x1, x2, …, xn  | P (x1, x2, …, xn )}

is safe if -
all values that appear in tuples of the expression are values from dom (P )

54
Expressive Power of Languages
• The following languages are equivalent
– The basic relational algebra (without extended relational algebra operation)
– The tuple relational calculus (restricted to safe expression)
– The domain relational calculus (restricted to safe expression)

55

You might also like