DBMS – Lecture 2 : Relational Algebra 
Lecture Plan 
• Relational Algebra (operators) 
• How it allows data independence 
For more visit: www.technotz.info
Recapitulate: Database Abstraction 
• We have seen relational model as an abstraction. 
• Q: How do we operate on the relations/tables? How do 
we access data? 
Ans: Use a language such as: 
- relational algebra 
- SQL 
For more visit: www.technotz.info
Relational Algebra 
• Queries can be expressed in relational algebra 
- To retrieve data from tables 
• Five operators 
- Project: Cutting a table vertically 
- Restrict or Select: Cutting horizontally 
- Cartesian-product: Putting two tables together 
- Union: Adding rows 
- Difference: Deleting rows 
For more visit: www.technotz.info
Project Operation 
Cuts a table vertically. 
• Retrieves some (or all) columns from a table R 
 Example: Retrieve rollno and address of each student 
PROJECT (rollno, name, student) 
or 
student [rollno, name] 
rollno name 
123 
45 
Ram 
Mohan 
For more visit: www.technotz.info
From our familiar relation: student 
rollno* name addr city birth date 
123 
45 
Ram 
Mohan 
30 MG Marg 
10 Laxmi Bai 
Hyd 
Dlh 
83-06-21 
83-09-26 
For more visit: www.technotz.info
Project Operation: Definition 
• Definition 
PROJECT (A1, ..., Ak, R) 
or 
R [A1, ..., Ak] 
- A1 to Ak are attribute names, 
- R is a relation/table. 
* Result: A table with all the rows of R but only k 
attributes (A1 to Ak) 
For more visit: www.technotz.info
Restrict or Select Operation 
Cutting a table horizontally 
• Retrieves some (or all) of the rows from a table R. 
- Those that satisfy a given condition. 
 Ex. Retrieve information about the student with rollno 45 
RESTRICT ((rollno = 45), student) 
rollno* name addr city birth date 
45 Mohan 10 Laxmi Bai Dlh 83-09-26 
For more visit: www.technotz.info
 Ex. Retrieve students from Hyderabad 
RESTRICT (city = 'Hyd', student) 
roll no* name addr city birth date 
123 Ram 30 MG 
Marg 
Hyd 83-06-21 
For more visit: www.technotz.info
RESTRICT (F, R) 
• F is a condition, and R a table 
• Condition F is: 
* Equality or inequality involving attributes and 
values 
 Ex. (A2 < 16) 
 Ex. (A2 = A3) 
* logical-and, logical-or or logical-not of 
conditions. 
 Ex. ( (A2 < 16) AND (A2 = A3) ) 
* Result: A table with all the attributes of R, but only 
those rows that satisfy condition. 
For more visit: www.technotz.info
Composition of Operations 
• Possible to combine the operations 
 Ex. Names of students from Hyderabad 
PROJECT (sname, RESTRICT (city = 'Hyd', student)) 
 Ex. Names of students from Hyderabad with roll no. greater 
than 50 
PROJECT (sname, 
RESTRICT ((city = 'Hyd') AND 
(rollno > 50), student) ) 
name 
Ram 
For more visit: www.technotz.info 
- Above condition has logical-and
Cross Product (*) 
• A "multiplication" of two relations (R, S): 
R * S 
- Let R have m attributes (A1 to Am) and r rows, 
- Let S have n attributes (B1 to Bn) and s rows, 
- Result of cross product (R * S) is a relation with 
 (m+n) attributes: A1 ... Am, B1 ... Bn 
 r * s rows 
* Every row of R is pasted with every row of S. 
For more visit: www.technotz.info
Cross Product Example 
S = PROJECT (rollno, name, student) 
S 
roll no name 
123 Ram 
45 Mohan 
enroll 
cno* rollno* grade 
IT365 123 
IT355 45 
IT365 45 
IT340 123 For more visit: www.technotz.info
Cross-Product: S * register 
rollno name cno* rollno* grade 
123 
Ram 
IT365 
123 
123 
Ram 
IT355 
45 
123 
Ram 
IT365 
45 
123 
Ram 
IT340 
123 
45 
Mohan 
IT365 
123 
45 
Mohan 
IT355 
45 
45 
Mohan 
IT365 
45 
45 
Mohan 
IT340 
123 
For more visit: www.technotz.info
Join Operation: Example 
Join relates rows of two tables on some columns. 
 Ex. Find rollno. and name of students enrolled in 
courses. 
JOIN ((S.rollno = enroll.rollno), S, enroll) (where S is a 
projection of student as above) 
rollno name cno grade 
123 
123 
45 
45 
Ram 
Ram 
Mohan 
Mohan 
IT365 
IT340 
IT355 
IT365 
* Take the cross-product of: S and enroll 
* Keep only those rows where rollno is the same. 
* Do not keep two columns for rollno 
- Both have same value in each row 
 Same as: PROJECT For more (rollno, visit: www.technotz.name, info 
cno, grade, 
(continue...)
...continued 
RESTRICT [ (S.rollno = enroll.rollno), S * enroll ] ) 
rollno name cno grade 
123 
123 
45 
45 
Ram 
Ram 
Mohan 
Mohan 
IT365 
IT340 
IT355 
IT365 
For more visit: www.technotz.info
Join Operation: Definition 
• Join of R1, R2 on some logic formula F: 
JOIN (F, R1, R2) 
• Join: is a combination of CROSS-PROD and 
RESTRICT. 
The above is same as: 
RESTRICT (F, R1 CROSS-PROD R2) 
or 
RESTRICT (F, R1 * R2) 
For more visit: www.technotz.info
Equi-join and Natural Join 
• Equi-join: A join operation in which F contains equality 
only. 
• Natural join: A join operation in which equi-join is 
performed over 
the same column names across two relations. 
 Example: 
JOIN ((student.rollno = enroll.rollno), student, 
enroll) 
same as: 
NATURAL-JOIN (student, enroll) 
For more visit: www.technotz.info
Union (U) Operation 
• Takes two tables R and S (with same columns) and pastes 
them 
horizontally 
(rows add up): R U S 
 Ex. Students doing AI (IT365), and students doing 
DBMS (IT355) 
courses 
PROJECT (rollno, RESTRICT (cno='IT365', enroll)) 
U 
PROJECT (rollno, RESTRICT (cno='IT355', enroll)) 
rollno rollno 
U rollno = 
123 
123 
45 45 
45 
For more visit: www.technotz.info
Difference (-) Operation 
• Takes two tables R and S (with same columns) and removes rows 
of S from 
rows of R: R - S 
 Ex. Students doing AI (IT365) but not DBMS (IT355). 
( PROJECT (rollno, RESTRICT (cno='IT365', enroll)) 
PROJECT (rollno, RESTRICT (cno='IT355', enroll)) ) 
rollno rollno rollno 
123 
- = 
45 123 
45 
For more visit: www.technotz.info
Rename Operation 
• This is used to give new names to columns. 
RENAME (A1 as B1, ... An as Bn, R) 
- Attributes A's are renamed to B's. 
For more visit: www.technotz.info
Conclusions 
• Relational algebra is a query language 
- Allows us to access or retrieve data from tables 
* Query is in terms of tables, attributes, etc. 
- Is independent of physical representation 
* Does not depend on what organization is used: 
 What kinds of files are used ? 
 Whether sequential, b-tree, or hashed ? 
• As a result: 
- When underlying file organization changed, 
- queries need not change 
For more visit: www.technotz.info

DBMS : Relational Algebra

  • 1.
    DBMS – Lecture2 : Relational Algebra Lecture Plan • Relational Algebra (operators) • How it allows data independence For more visit: www.technotz.info
  • 2.
    Recapitulate: Database Abstraction • We have seen relational model as an abstraction. • Q: How do we operate on the relations/tables? How do we access data? Ans: Use a language such as: - relational algebra - SQL For more visit: www.technotz.info
  • 3.
    Relational Algebra •Queries can be expressed in relational algebra - To retrieve data from tables • Five operators - Project: Cutting a table vertically - Restrict or Select: Cutting horizontally - Cartesian-product: Putting two tables together - Union: Adding rows - Difference: Deleting rows For more visit: www.technotz.info
  • 4.
    Project Operation Cutsa table vertically. • Retrieves some (or all) columns from a table R  Example: Retrieve rollno and address of each student PROJECT (rollno, name, student) or student [rollno, name] rollno name 123 45 Ram Mohan For more visit: www.technotz.info
  • 5.
    From our familiarrelation: student rollno* name addr city birth date 123 45 Ram Mohan 30 MG Marg 10 Laxmi Bai Hyd Dlh 83-06-21 83-09-26 For more visit: www.technotz.info
  • 6.
    Project Operation: Definition • Definition PROJECT (A1, ..., Ak, R) or R [A1, ..., Ak] - A1 to Ak are attribute names, - R is a relation/table. * Result: A table with all the rows of R but only k attributes (A1 to Ak) For more visit: www.technotz.info
  • 7.
    Restrict or SelectOperation Cutting a table horizontally • Retrieves some (or all) of the rows from a table R. - Those that satisfy a given condition.  Ex. Retrieve information about the student with rollno 45 RESTRICT ((rollno = 45), student) rollno* name addr city birth date 45 Mohan 10 Laxmi Bai Dlh 83-09-26 For more visit: www.technotz.info
  • 8.
     Ex. Retrievestudents from Hyderabad RESTRICT (city = 'Hyd', student) roll no* name addr city birth date 123 Ram 30 MG Marg Hyd 83-06-21 For more visit: www.technotz.info
  • 9.
    RESTRICT (F, R) • F is a condition, and R a table • Condition F is: * Equality or inequality involving attributes and values  Ex. (A2 < 16)  Ex. (A2 = A3) * logical-and, logical-or or logical-not of conditions.  Ex. ( (A2 < 16) AND (A2 = A3) ) * Result: A table with all the attributes of R, but only those rows that satisfy condition. For more visit: www.technotz.info
  • 10.
    Composition of Operations • Possible to combine the operations  Ex. Names of students from Hyderabad PROJECT (sname, RESTRICT (city = 'Hyd', student))  Ex. Names of students from Hyderabad with roll no. greater than 50 PROJECT (sname, RESTRICT ((city = 'Hyd') AND (rollno > 50), student) ) name Ram For more visit: www.technotz.info - Above condition has logical-and
  • 11.
    Cross Product (*) • A "multiplication" of two relations (R, S): R * S - Let R have m attributes (A1 to Am) and r rows, - Let S have n attributes (B1 to Bn) and s rows, - Result of cross product (R * S) is a relation with  (m+n) attributes: A1 ... Am, B1 ... Bn  r * s rows * Every row of R is pasted with every row of S. For more visit: www.technotz.info
  • 12.
    Cross Product Example S = PROJECT (rollno, name, student) S roll no name 123 Ram 45 Mohan enroll cno* rollno* grade IT365 123 IT355 45 IT365 45 IT340 123 For more visit: www.technotz.info
  • 13.
    Cross-Product: S *register rollno name cno* rollno* grade 123 Ram IT365 123 123 Ram IT355 45 123 Ram IT365 45 123 Ram IT340 123 45 Mohan IT365 123 45 Mohan IT355 45 45 Mohan IT365 45 45 Mohan IT340 123 For more visit: www.technotz.info
  • 14.
    Join Operation: Example Join relates rows of two tables on some columns.  Ex. Find rollno. and name of students enrolled in courses. JOIN ((S.rollno = enroll.rollno), S, enroll) (where S is a projection of student as above) rollno name cno grade 123 123 45 45 Ram Ram Mohan Mohan IT365 IT340 IT355 IT365 * Take the cross-product of: S and enroll * Keep only those rows where rollno is the same. * Do not keep two columns for rollno - Both have same value in each row  Same as: PROJECT For more (rollno, visit: www.technotz.name, info cno, grade, (continue...)
  • 15.
    ...continued RESTRICT [(S.rollno = enroll.rollno), S * enroll ] ) rollno name cno grade 123 123 45 45 Ram Ram Mohan Mohan IT365 IT340 IT355 IT365 For more visit: www.technotz.info
  • 16.
    Join Operation: Definition • Join of R1, R2 on some logic formula F: JOIN (F, R1, R2) • Join: is a combination of CROSS-PROD and RESTRICT. The above is same as: RESTRICT (F, R1 CROSS-PROD R2) or RESTRICT (F, R1 * R2) For more visit: www.technotz.info
  • 17.
    Equi-join and NaturalJoin • Equi-join: A join operation in which F contains equality only. • Natural join: A join operation in which equi-join is performed over the same column names across two relations.  Example: JOIN ((student.rollno = enroll.rollno), student, enroll) same as: NATURAL-JOIN (student, enroll) For more visit: www.technotz.info
  • 18.
    Union (U) Operation • Takes two tables R and S (with same columns) and pastes them horizontally (rows add up): R U S  Ex. Students doing AI (IT365), and students doing DBMS (IT355) courses PROJECT (rollno, RESTRICT (cno='IT365', enroll)) U PROJECT (rollno, RESTRICT (cno='IT355', enroll)) rollno rollno U rollno = 123 123 45 45 45 For more visit: www.technotz.info
  • 19.
    Difference (-) Operation • Takes two tables R and S (with same columns) and removes rows of S from rows of R: R - S  Ex. Students doing AI (IT365) but not DBMS (IT355). ( PROJECT (rollno, RESTRICT (cno='IT365', enroll)) PROJECT (rollno, RESTRICT (cno='IT355', enroll)) ) rollno rollno rollno 123 - = 45 123 45 For more visit: www.technotz.info
  • 20.
    Rename Operation •This is used to give new names to columns. RENAME (A1 as B1, ... An as Bn, R) - Attributes A's are renamed to B's. For more visit: www.technotz.info
  • 21.
    Conclusions • Relationalalgebra is a query language - Allows us to access or retrieve data from tables * Query is in terms of tables, attributes, etc. - Is independent of physical representation * Does not depend on what organization is used:  What kinds of files are used ?  Whether sequential, b-tree, or hashed ? • As a result: - When underlying file organization changed, - queries need not change For more visit: www.technotz.info