Lecture Notes For DBMS: Relational Algebra
Lecture Notes For DBMS: Relational Algebra
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 dont 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
Title=DBMS(BOOK)=
Acc-No Title Author
100 DBMS Silbershatz
200 DBMS Ramanuj
600 DBMS Silbershatz
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