You are on page 1of 2

Lecture Notes For DBMS and Data Mining and Data Warehousing

Lecture 10
Relational Algebra:
 Basic operations:
o Selection (σ) Selects a subset of rows from relation.
o Projection (π) Selects a subset of columns from relation.
o Cross-product (×) Allows us to combine two relations.
o Set-difference () Tuples in reln. 1, but not in reln. 2.
o Union (U) Tuples in reln. 1 and in reln. 2.
o Rename( ρ) Use new name for the Tables or fields.
 Additional operations:
o Intersection (∩), join( ), division(÷): Not essential, but (very!) useful.
 Since each operation returns a relation, operations can be composed! (Algebra is
“closed”.)
Projection
 Deletes attributes that are not in projection list.
 Schema of result contains exactly the fields in the projection list, with the same names
that they had in the (only) input relation. ( Unary Operation)
 Projection operator has to eliminate duplicates! (as it returns a relation which is a set)
o Note: real systems typically don’t do duplicate elimination unless the user
explicitly asks for it. (Duplicate values may be representing different real world
entity or relationship)
Consider the BOOK table:
Acc-No Title Author
100 “DBMS” “Silbershatz”
200 “DBMS” “Ramanuj”
300 “COMPILER” “Silbershatz”
400 “COMPILER” “Ullman”
500 “OS” “Sudarshan”
600 “DBMS” “Silbershatz”

πTitle(BOOK) =
Title
“DBMS”
“COMPILER”
“OS”

Selection
 Selects rows that satisfy selection condition.
 No duplicates in result! (Why?)
 Schema of result identical to schema of (only) input relation.
 Result relation can be the input for another relational algebra operation! (Operator
composition.)
σAcc-no>300(BOOK) =
Acc-No Title Author
400 “COMPILER” “Ullman”
500 “OS” “Sudarshan”
600 “DBMS” “Silbershatz”

Department of Electrical and Electronics By: Sulabh Bansal


Lecture Notes For DBMS and Data Mining and Data Warehousing

σTitle=”DBMS”(BOOK)=
Acc-No Title Author
100 “DBMS” “Silbershatz”
200 “DBMS” “Ramanuj”
600 “DBMS” “Silbershatz”

πAcc-no (σTitle=”DBMS” (BOOK))=


Acc-No
100
200
600

Union, Intersection, Set-Difference


 All of these operations take two input relations, which must be union-compatible:
o Same number of fields.
o `Corresponding’ fields have the same type.
 What is the schema of result?
Consider:
Borrower Depositor
Cust-name Loan-no Cust-name Acc-no
Ram L-13 Suleman A-100
Shyam L-30 Radheshyam A-300
Suleman L-42 Ram A-401

List of customers who are either borrower or depositor at bank= πCust-name (Borrower) U
πCust-name (Depositor)=
Cust-name
Ram
Shyam
Suleman
Radeshyam

Customers who are both borrowers and depositors = πCust-name (Borrower) ∩ πCust-name
(Depositor)=
Cust-name
Ram
Suleman

Customers who are borrowers but not depositors = πCust-name (Borrower)  πCust-name
(Depositor)=
Cust-name
Shyam

Department of Electrical and Electronics By: Sulabh Bansal

You might also like