Notes - the David R. Cheriton School of Computer Science

Transcription

Notes - the David R. Cheriton School of Computer Science
Notes
Relational Calculus and Query-By-Example
Alexandra Roatiş
David R. Cheriton School of Computer Science
University of Waterloo
CS 348
Introduction to Database Management
Spring 2015
CS 348
Relational Calculus
Spring 2015
1 / 14
Database Schema Used in Examples
Notes
Department
DeptNo
DeptName
MgrNo
AdmrDept
Project
ProjNo
DeptNo
RespEmp
MajProj
Employee
EmpNo
FirstName
MidInit
LastName
WorkDept
HireDate
Salary
Emp_Act
EmpNo
ProjNo
ActNo
EmStDate
EmEnDate
EmPTime
CS 348
Relational Calculus
Spring 2015
2 / 14
Relational Calculus
Notes
Idea
Use first-order logic (FOL) formulae to specify properties of the
query answer.
Vocabulary:
• set of constants
• set of variables
• set of comparison predicates ( , , . . . )
• set of n-ary predicates
• set of n-ary functions
• logical connectives ( , , , )
• quantifiers: ,
• parentheses
Expressions (called well formed formulae (wff)) built from this
vocabulary.
CS 348
Relational Calculus
Spring 2015
3 / 14
Relational Calculus: Semantics
Notes
How do we interpret variables?
Definition (Valuation)
A valuation is a mapping from variable names to values in the
universe.
Idea
Answers to a query
Valuations for free variables that make the
query formula true with respect to a database.
CS 348
Relational Calculus
Spring 2015
4 / 14
Types of Relational Calculus
Notes
According to the primitive variable used in specifying the queries:
• tuple relational calculus
• domain relational calculus
CS 348
Relational Calculus
Spring 2015
5 / 14
Tuple Relational Calculus
Notes
TRC formulae are built using:
Conjunctive
•
•
•
•
•
Positive
•
First-order
•
•
•
xi R (atomic)
xi a op xj b (atomic)
xi a op c (atomic)
xi
xi
(redundant)
(redundant)
Note
A well-formed TRC query should contain only one free variable.
CS 348
Relational Calculus
Spring 2015
6 / 14
Tuple Relational Calculus: Example
Notes
• Find the last names and hire dates of employees who make more
than $100000.
DRC:
S Employee _ _ _ N _ D S
N D
100000
S
TRC:
W
Employee
E E
W hiredate
E salary
W lastname
E lastname
E hiredate
100000
or
W
E
Employee W lastname
W hiredate
E salary
CS 348
E lastname
E hiredate
100000
Relational Calculus
Spring 2015
7 / 14
Domain Relational Calculus
Notes
DRC formulae are built using:
Conjunctive
•
•
•
•
•
Positive
•
First-order
•
•
•
CS 348
R xi1
xik (atomic)
xi op xj (atomic)
xi op c (atomic)
xi
xi
(redundant)
(redundant)
Relational Calculus
Spring 2015
8 / 14
Domain Relational Calculus: Example
Notes
• Find the last names and hire dates of employees who make more
than $100000.
E F M W S Employee E F M N W D S
N D
S
100000
or
N D
Employee
E F M W S E F M N W D S
S
100000
or
S Employee _ _ _ N _ D S
N D
100000
S
or
N D
S _ _ _ N _ D S
CS 348
Employee
Relational Calculus
S
100000
Spring 2015
9 / 14
Safety of Relational Calculus Formulae
Notes
• Find all dates on which no employee was hired.
D
Employee _ _ _ _ _ D _
Problem
The answer to this query is not finite (assuming D’s domain is not
finite).
Definition (Safety)
A query is safe if, for all databases instances conforming to the
schema, the query result can be computed using only constants
appearing in the database instance or in the query itself.
As long as the database instance is finite, the result of any safe query
will also be finite.
CS 348
Relational Calculus
Spring 2015
10 / 14
Safety – Examples
Notes
• employees who are active in all projects:
E
Employee E
P Project P
EmpAct E P
Safe?
• departments that hire employees every day:
D
Department D
H Employee
D H _
Safe?
CS 348
Relational Calculus
Spring 2015
11 / 14
Query-By-Example
Notes
• QBE: a graphical language heavily influenced by DRC
• QBE is an IBM trademark
• influenced Paradox, Access, other end-user query tools
• convenient for simple queries
• awkward for complex queries
• Language basics:
• query composed of relation “skeletons”
• output attributes marked with “P.” (single skeleton only)
• variables used for equality predicates
• atomic conditions placed below attribute, or in CONDITIONS box
• multiple rows in a skeleton means OR
• skeletons can be negated, but variables appearing in negated
skeleton must also appear in positive skeleton
CS 348
Relational Calculus
Spring 2015
12 / 14
QBE: Example
Notes
• Find the last names and hire dates of employees who make more
than $100000.
DRC:
S Employee _ _ _ N _ D S
N D
100000
S
QBE:
Employee
ENo
FName
MInit
LName
P.
Dept
HDate
P.
Sal
>100000
or
Employee
ENo
FName
LName
P.
CONDITIONS
_S > 100000
CS 348
MInit
Dept
Relational Calculus
HDate
P.
Sal
_S
Spring 2015
13 / 14
QBE: Another Example
Notes
• For each project for which department E21 is responsible, find the
name of the employee in charge of that project.
DRC:
E Project P E21 E _
P N
Employee E _ _ N _ _ _
QBE:
Project
Employee
CS 348
PNo
P.
ENo
_E
Dept
’E21’
FName
RespEmp
_E
MInit
MajProj
LName
_N
Relational Calculus
P. _N
Dept
HDate
Sal
Spring 2015
14 / 14