CMSC 461, Database Management Systems Chapter 6 – …

42
CMSC 461, Database Management Systems Spring 2018 Chapter 6 – Formal Relational Query Languages Jennifer Sleeman https://www.csee.umbc.edu/~jsleem1/courses/461/spr18 These slides are based on “Database System Concepts” book and slides, 6 th edition , and the 2009/2012 CMSC 461 slides by Dr. Kalpakis

Transcript of CMSC 461, Database Management Systems Chapter 6 – …

Page 1: CMSC 461, Database Management Systems Chapter 6 – …

CMSC 461, Database Management SystemsSpring 2018

Chapter 6 – Formal Relational Query Languages

Jennifer Sleeman https://www.csee.umbc.edu/~jsleem1/courses/461/spr18

These slides are based on “Database System Concepts” book and slides, 6th edition, and the 2009/2012 CMSC 461 slides by Dr. Kalpakis

Page 2: CMSC 461, Database Management Systems Chapter 6 – …

Logistics

● Homework 1 due Wednesday 2/7/2018● Dr. Sleeman out on Wednesday

− Class will still meet, guest lecturer● Project is posted, we will review today● Phase 1 of project is due 2/14/2018

Page 3: CMSC 461, Database Management Systems Chapter 6 – …

Lecture Outline

• Intro to Relational Algebra• Fundamental Operations• Additional Operations• Summary• In Class Exercise

Page 4: CMSC 461, Database Management Systems Chapter 6 – …

Lecture Outline

• Intro to Relational Algebra• Fundamental Operations• Additional Operations • Summary• In Class Exercise

Page 5: CMSC 461, Database Management Systems Chapter 6 – …

Relational Algebra

● A procedural query language based on the mathematical theory of sets that is the foundation of commercial DBMS query languages

● The operations typically take one or two relations as inputs and give a new relation as a result

● Can build expressions using multiple relational operations

Page 6: CMSC 461, Database Management Systems Chapter 6 – …

Relational Algebra

● What is the difference between a procedural language and a non-procedural language?

Page 7: CMSC 461, Database Management Systems Chapter 6 – …

Relational Algebra

● Procedural languages tell you how to process a query (a sequence of steps provide the how)

● Non-Procedural or declarative languages tell you what to process but not how to process

Page 8: CMSC 461, Database Management Systems Chapter 6 – …

Relational Algebra

● Six basic operators− select: σ− project: ∏− union: ∪− set difference: – − Cartesian product: x− rename: ρ

Page 9: CMSC 461, Database Management Systems Chapter 6 – …

Lecture Outline

• Intro to Relational Algebra• Fundamental Operations• Additional Operations • Summary• In Class Exercise

Page 10: CMSC 461, Database Management Systems Chapter 6 – …

Select Operation

σp(r) = {t | t ∈ r and p(t)}

Where p is the selection predicate, a formula in propositional calculus consisting of terms connected by logical operators ∧ (and), ∨ (or), ¬ (not)Each term is one of:

<attribute> op <attribute> <attribute> op <constant> where op is one of: = ≠ > ≥ < ≤

Page 11: CMSC 461, Database Management Systems Chapter 6 – …

Select Operation Instructor.dept_name = Department.dept_name (Simple pred)Instructor.dept_name='Finance' (Simple pred)Instructor.dept_name = Department.dept_name or Instructor.Name = 'Wu' (Boolean Combination pred)Instructor.dept_name = Department.dept_name and Instructor.Name = 'Wu' (Boolean Combination pred)Not Instructor.Name = 'Wu' (Boolean Combination pred)

Page 12: CMSC 461, Database Management Systems Chapter 6 – …

Select Operation

Page 13: CMSC 461, Database Management Systems Chapter 6 – …

Select Operation

Page 14: CMSC 461, Database Management Systems Chapter 6 – …

Example Select Operation

σ dept_name=“Physics”(instructor)

Page 15: CMSC 461, Database Management Systems Chapter 6 – …

Project Operation∏

A1, A2, …, Ak (r)Where A1, A2 are attribute names and r is a relation name.The result is defined as the relation of k columns obtained by dropping the columns that are not listedDuplicate rows removed from result, since relations are sets

Page 16: CMSC 461, Database Management Systems Chapter 6 – …

Example Project Operation

To eliminate the dept_name attribute of instructor

∏ID, name, salary (instructor)

Page 17: CMSC 461, Database Management Systems Chapter 6 – …

Union Operation

r ∪ s = {t | t ∈ r or t ∈ s}For r ∪ s to be valid, these relations have to be union compatible.

− r and s must have the same arity (same number of attributes)− the domains of the corresponding attributes must be compatible

(example: 2nd column of r deals with the same type of values as does the 2nd column of s)

Page 18: CMSC 461, Database Management Systems Chapter 6 – …

Example Union Operation

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 19: CMSC 461, Database Management Systems Chapter 6 – …

Set Difference Operation

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

Set difference must be taken between compatible relations.− r and s must have the same arity− Attribute domains of r and s must be compatible

Page 20: CMSC 461, Database Management Systems Chapter 6 – …

Example Set Difference Operation

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 21: CMSC 461, Database Management Systems Chapter 6 – …

Cartesian-Product Operation

r x s = {t q | t ∈ r and q ∈ s}Assume that attributes of r and s are disjoint. If attributes of r and s are not disjoint, then renaming must be used.

Page 22: CMSC 461, Database Management Systems Chapter 6 – …

Example Cartesian-Product Operation

To find the names of all instructors in the Physics department together with the course_id of all courses they taught:

∏name,course_id (σ instructor.ID=teaches.ID(σ depart_name =

“Physics” (instructor x teaches))) For r = instructor x teaches:(instructor.ID, name, dept_name, salaryteaches.ID, course_id, sec_id, semester, year)

Page 23: CMSC 461, Database Management Systems Chapter 6 – …

Composition of Operations

Can build expressions using multiple operationsRelational-algebra expression – composition of relational-algebra operationsExample: σA=C(r x s)

σA=C(r x s)

r x s

Page 24: CMSC 461, Database Management Systems Chapter 6 – …

Rename Operation

ρ x (E)Returns the expression E under the name XIf a relational-algebra expression E has arity n, then returns the result of expression E under the name

X, and with theattributes renamed to A1 , A2 , …., An .

Page 25: CMSC 461, Database Management Systems Chapter 6 – …

Rename Operation

● Allows us to name, and therefore to refer to, the results of relational-algebra expressions.

● Allows us to refer to a relation by more than one name.

Page 26: CMSC 461, Database Management Systems Chapter 6 – …

Example Rename Operation

σ instructor.salary < d.salary (instructor X ρ d (instructor)) Using the rename operation to rename a reference to the instructor table so the relation can be referenced twice without ambiguity

Page 27: CMSC 461, Database Management Systems Chapter 6 – …

Example 2 Rename Operation

ρ

d(InstructorID,InstructorName,InstructorDepartName,InstructorS

alary) (instructor) Using the rename operation to rename attributes

Page 28: CMSC 461, Database Management Systems Chapter 6 – …

Alternative – Positional Notation

Name attributes of relation implicitly − $1 – first attribute, $2 – second attribute …

Also applies to results of relational-algebra operations

Page 29: CMSC 461, Database Management Systems Chapter 6 – …

Alternative – Positional Notation

What is the output? ∏$4 (σ $4 < $8 (instructor X instructor))

Page 30: CMSC 461, Database Management Systems Chapter 6 – …

Example QueriesFind the largest salary in the university

− Step 1: find instructor salaries that are less than some other instructor salary (i.e. not maximum)

− using a copy of instructor under a new name d∏instructor.salary (σ instructor.salary < d,salary

(instructor x ρd (instructor)))

− Step 2: Find the largest salary∏salary (instructor) – ∏instructor.salary (σ instructor.salary < d,salary

(instructor x ρd (instructor)))

Page 31: CMSC 461, Database Management Systems Chapter 6 – …

Example QueriesFind the names of all instructors in the Physics department, along with the course_id of all courses they have taught

∏instructor.ID,course_id (σdept_name=“Physics” ( σ instructor.ID=teaches.ID (instructor x teaches)))

∏instructor.ID,course_id (σinstructor.ID=teaches.ID ( σ dept_name=“Physics” (instructor) x teaches))

Page 32: CMSC 461, Database Management Systems Chapter 6 – …

Experimenting with Relational Algebra - Relational

http://ltworf.github.io/relational/On Github https://github.com/ltworf/relational/ Query := Query BinaryOp QueryQuery := (Query)Query := σ PYExprWithoutParenthesis (Query) | σ (PYExpr) (Query)Query := π FieldList (Query)Query := ρ RenameList (Query)FieldList := Ident | Ident , FieldListRenameList := Ident ➡ Ident | Ident ➡ Ident , RenameListBinaryOp := * | - | � | � | ÷ | �� | �LEFT� | �RIGHT� | �FULL�

Page 33: CMSC 461, Database Management Systems Chapter 6 – …

Relational – Creating a relation

Page 34: CMSC 461, Database Management Systems Chapter 6 – …

Adding tuples - Relational

Page 35: CMSC 461, Database Management Systems Chapter 6 – …

Select Operation - Relational

Page 36: CMSC 461, Database Management Systems Chapter 6 – …

Project Operation - Relational

Page 37: CMSC 461, Database Management Systems Chapter 6 – …

Cartesian Product - Relational

Page 38: CMSC 461, Database Management Systems Chapter 6 – …

Cartesian Product - Relational

Page 39: CMSC 461, Database Management Systems Chapter 6 – …

Relational Algebra Expressions - Relational

Page 40: CMSC 461, Database Management Systems Chapter 6 – …

Relational Algebra Expressions - Relational

Page 41: CMSC 461, Database Management Systems Chapter 6 – …

Lecture Outline

• Intro to Relational Algebra• Fundamental Operations• Additional Operations• Summary• In Class Exercise

Page 42: CMSC 461, Database Management Systems Chapter 6 – …

Additional Operations

● We define additional operations that do not add any expressive power to the relational algebra, but that simplify common queries.− Set intersection− Natural join− Division− Assignment