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
Embed
RDBMS and SQL Relational Algebravvtesh.co.in/teaching/dbms2019/Lecture2-Relational...Tuple Relational Calculus Domain Relational Calculus Popular Language SQL Relational Algebra Relation
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.
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.
• 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 ))
Join Operation (Cont.)
• instructor.id = teaches.id (instructor x teaches))
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.
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
Find the highest salary in the university• Steps to reach the solution:
57
95000
Find the highest salary in the university• Compute instructor x instructor
• Compute
• Compute
58
We use rename operation to distinguish the two salary attributes.
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
Fundamental Operations
60
The select, project, rename, set difference, cartesian product, and
union are sufficient to express queries!
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.