Top Banner
DBMS – Lecture 2 : Relational Algebra Lecture Plan Relational Algebra (operators) How it allows data independence For more visit: www.technotz.info
21

DBMS : Relational Algebra

Jul 13, 2015

Download

Education

Sridhar Sidhu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DBMS : Relational Algebra

DBMS – Lecture 2 : Relational Algebra

Lecture Plan

• Relational Algebra (operators)

• How it allows data independence

For more visit: www.technotz.info

Page 2: DBMS : Relational Algebra

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

Page 3: DBMS : Relational Algebra

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

Page 4: DBMS : Relational Algebra

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]

Project Operation

rollno name

123

45

Ram

Mohan

For more visit: www.technotz.info

Page 5: DBMS : Relational Algebra

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

Page 6: DBMS : Relational Algebra

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

Page 7: DBMS : Relational Algebra

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

Page 8: DBMS : Relational Algebra

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

Page 9: DBMS : Relational Algebra

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

Page 10: DBMS : Relational Algebra

• 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) )

Composition of Operations

name

Ram

- Above condition has logical-and For more visit: www.technotz.info

Page 11: DBMS : Relational Algebra

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

Page 12: DBMS : Relational Algebra

Cross Product Example

S = PROJECT (rollno, name, student)

S

roll no name

123 Ram

45 Mohan

enroll

cno* rollno* gradeIT365 123

IT355 45

IT365 45

IT340 123 For more visit: www.technotz.info

Page 13: DBMS : Relational Algebra

Cross-Product: S * register

rollno name cno* rollno* grade

123

123

123

123

45

45

45

45

Ram

Ram

Ram

Ram

Mohan

Mohan

Mohan

Mohan

IT365

IT355

IT365

IT340

IT365

IT355

IT365

IT340

123

45

45

123

123

45

45

123For more visit: www.technotz.info

Page 14: DBMS : Relational Algebra

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 (rollno, name, cno, grade,

(continue...) For more visit: www.technotz.info

Page 15: DBMS : Relational Algebra

...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

Page 16: DBMS : Relational Algebra

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

Page 17: DBMS : Relational Algebra

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

Page 18: DBMS : Relational Algebra

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))

U =rollno rollno rollno

123

4545

123

45For more visit: www.technotz.info

Page 19: DBMS : Relational Algebra

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

4545 123

- =

For more visit: www.technotz.info

Page 20: DBMS : Relational Algebra

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

Page 21: DBMS : Relational Algebra

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