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
Rasmus Ejlers Møgelberg
Relational algebraIntroduction to Database Design 2012, Lecture 5
Rasmus Ejlers Møgelberg
Overview
• Use of logic in constructing queries
• Relational algebra
• Translating queries to relational algebra
• Equations expressed in relational algebra
2
Rasmus Ejlers Møgelberg
Use of logic in constructing queries
• Consider the following problem
• Expressed more formally
• Translate to SQL:
3
Find all students who have taken all courses offered by the biology department
Find all students s such that for all courses c, if c is offered by ‘Biology’ then s has taken c
select * from students where [???]
Rasmus Ejlers Møgelberg
Use of logic in constructing queries
• The problem is not suitable for SQL, because it uses ‘for all’ and ‘if ... then ...’
• So reformulate
• (using classical logic)
4
Find all students s such that for all courses c, if c is offered by ‘Biology’ then s has taken c
Find all students s such that there is no course c such that c is offered by ‘Biology’ and s has not taken c
Rasmus Ejlers Møgelberg
Use of logic in constructing queries
• This can be formulated in SQL:
5
Find all students s such that there is no course c such that c is offered by ‘Biology’ and s has not taken c
select * from student where not exists (select * from course where dept_name = ‘Biology’ and course_id not in (select course_id from takes where takes.id = student.id))
Finds all courses taken by studentFinds all courses offered by Biology not take by student
Rasmus Ejlers Møgelberg
More logic
• Similar analysis is needed for the challenging exercises this week
• You will see more logic in the course Foundations of Computing
6
Rasmus Ejlers Møgelberg
Relational algebra
Rasmus Ejlers Møgelberg
Relational algebra
• A language for expressing basic operations in the relational model
• Two purposes
- Express meaning of queries
- Express execution plans in DBMSs
• SQL is declarative (what)
• Relational algebra is procedual (how)
8
Rasmus Ejlers Møgelberg
Relational algebra in DBMSs
9
Illustration from book
Rasmus Ejlers Møgelberg
Projection
• In SQL
• In relational algebra
10
select name, salary from instructor;
Πname, salary(instructor)
Rasmus Ejlers Møgelberg
Selection
11
select * from instructor where salary > 90000;
σsalary>90000(instructor)
Rasmus Ejlers Møgelberg
Combining selection and projection
12
select name, dept_name from instructor where salary > 90000;
Πname, dept name(σsalary>90000(instructor))
Rasmus Ejlers Møgelberg
Translating SQL into relational algebra
• Expression
• Is translated to
• Relational algebra expression says
- First do selection
- Then do projection
• Relational algebra procedural
13
select name, dept_name from instructor where salary > 90000;
Πname, dept name(σsalary>90000(instructor))
Rasmus Ejlers Møgelberg
Syntax trees
• The syntax tree
• represents the expression (3+4)*5
• Trees grow downwards in computer science!
• Evaluation from bottom up
• Useful graphical way of representing evaluation order (no need for parentheses)