Top Banner
Venkatesh Vinayakarao (Vv) RDBMS and SQL Relational Algebra Venkatesh Vinayakarao [email protected] http://vvtesh.co.in Chennai Mathematical Institute Slide contents are borrowed from the official website of the course text. For the authors’ original version of slides, visit: https ://www.db-book.com/db6/slide-dir/index.html.
30

RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

May 17, 2020

Download

Documents

dariahiddleston
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: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Venkatesh Vinayakarao (Vv)

RDBMS and SQL

Relational Algebra

Venkatesh [email protected]

http://vvtesh.co.in

Chennai Mathematical Institute

Slide contents are borrowed from the official website of the course text. For the authors’ original version of slides, visit: https://www.db-book.com/db6/slide-dir/index.html.

Page 2: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Preliminaries

35

Page 3: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Quiz

• A relation R from a set A to set B is a subset of the cartesian product A x B. True/False?

{ }( , )Relation R =

Let’s say a relation exists between the reds:

Page 4: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

The Fly on the Ceiling - Descartes

37

French mathematician René Descartes (1596-1650)

Image Source: http://sites.psu.edu/solvingproblemshttps://wild.maths.org/ren%C3%A9-descartes-and-fly-ceilingvectorstock.com

Page 5: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

A Relation

• Let the set, id = {1,2,3}

• Let the set, names = {vv, sd}

• What is id x names?

• We have a relation if we assign a sequential id to each name.

38

id name

1 sd

2 vv

id name

1 sd

1 vv

2 sd

2 vv

3 sd

3 vv

Page 6: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Relational Algebra and Query Languages

39

Page 7: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Query Languages

Relation 1Relation x Relation y

Query Language

Procedural LanguageRelational Algebra

Declarative LanguagesTuple Relational Calculus

Domain Relational Calculus

Popular LanguageSQL

Page 8: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Relational Algebra

Relation 1Relation x Relation y

Query Language

Fundamental Operationsselect, project, rename

set difference, cartesian product, union

More Operationsset intersection, natural join, assignment

Page 9: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Select Operation

• Notation: p(r) where p is called the selection predicate

• Example of selection: dept_name=“Physics”(instructor)

Page 10: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Select Operation

• Example of selection: dept_name=“Physics”(instructor)

Page 11: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Project Operation

• Notation: A1, A2,..,Ak (r) where Ai are attribute names

• Example of projection:ID, name, salary (instructor)

• Duplicate rows removed from result, since relations are sets

Page 12: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Projection

45

ID, name, salary (instructor)

Page 13: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Union Operation

• Notation: r s. Defined as:

r s = {t | t r or t s}

• For r s to be valid:• r, s must have the same arity (same number of attributes)• The attribute domains must be compatible (example: 2nd column

of r deals with the same type of values as does the 2nd

column of s)

• Example: to find all courses taught in the Fall 2009 semester,

or in the Spring 2010 semester, or in both

course_id ( semester=“Fall” Λ year=2009 (section))

course_id ( semester=“Spring” Λ year=2010 (section))

Page 14: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Union, Selection and Projection

• course_id ( semester=“Fall” Λ year=2009 (section)) course_id ( semester=“Spring” Λ year=2010 (section))

47

Page 15: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Set Difference Operation

• Notation: r – s. Defined as

r – s = {t | t r and t s}

• Example: to find all courses taught in the Fall 2009 semester,

but not in the Spring 2010 semester

course_id ( semester=“Fall” Λ year=2009 (section)) −

course_id ( semester=“Spring” Λ year=2010 (section))

Page 16: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Union, Selection and Projection

• course_id ( semester=“Fall” Λ year=2009 (section)) −

course_id ( semester=“Spring” Λ year=2010 (section))

49

Page 17: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Set-Intersection Operation

• Notation: r s

• Defined as:

• r s = { t | t r and t s }

Quiz: True/False? r s = r – (r – s)

Page 18: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Cartesian-Product Operation

• Notation r x s

instructor relation teaches relation

Page 19: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

The instructor X teaches table

Page 20: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Join Operation

• The Cartesian-Product instructor X teaches associates every tuple of instructor with every tuple of teaches.

• Most of the resulting rows have information about instructors who did NOT teach a particular course.

• To get only those tuples of instructor X teaches that pertain to instructors and the courses that they taught, we write:

instructor.id = teaches.id (instructor x teaches ))

Page 21: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Join Operation (Cont.)

• instructor.id = teaches.id (instructor x teaches))

Page 22: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Join Operation (Cont.)

• The join operation allows us to combine a select operation and a Cartesian-Product operation into a single operation.

• Consider relations r (R) and s (S). Let “theta” be a predicate on attributes in the schema R “union” S. The join operation r ⋈𝜃 s is defined as follows:

𝑟 ⋈𝜃 𝑠 = 𝜎𝜃 (𝑟 × 𝑠)

• Thus

instructor.id = teaches.id (instructor x teaches ))

• Can equivalently be written as

instructor⋈instructor.id = teaches.id

teaches.

Page 23: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Rename Operation

• 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

returns the result of expression E under the name X, and with the attributes renamed to A1 , A2 , …., An .

)(),...,2

,1

( En

AAAx

Page 24: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Find the highest salary in the university• Steps to reach the solution:

57

95000

Page 25: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Find the highest salary in the university• Compute instructor x instructor

• Compute

• Compute

58

We use rename operation to distinguish the two salary attributes.

Page 26: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Relational Algebra

• A basic expression in the relational algebra consists of either one of the following:• A relation in the database

• A constant relation

• Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra expressions:• E1 E2

• E1 – E2

• E1 x E2

• p (E1), P is a predicate on attributes in E1

• s(E1), S is a list consisting of some of the attributes in E1

• x (E1), x is the new name for the result of E1

Page 27: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Fundamental Operations

60

The select, project, rename, set difference, cartesian product, and

union are sufficient to express queries!

Page 28: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Extended Operations

• Set Intersection• Find the set of all courses taught in both the Fall 2017 and the

Spring 2018 semesters

• Assignment• Find all instructor in the “Physics” and Music department.

Physics dept_name=“Physics” (instructor)

Music dept_name=“Music” (instructor)

Physics Music

61

Result

course_id ( semester=“Fall” Λ year=2017 (section)) course_id ( semester=“Spring” Λ year=2018 (section))

Page 29: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Aggregate Functions

• Improves ease of use

• Most common functions:

62

Functions

sum

max

min

avg

count

Calligraphic G Notation

Compute sum of all values of attribute c on relation r.

Page 30: RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation

Summary

• Very much like normal algebra (x – y). We use relations instead of numbers as basic expressions.

• Basis for commercial query languages such as SQL.

• Three major components:• Fundamental Operations

• Extended Operations

• Aggregate Functions

63