L - Other Relational Query Languages
L - Other Relational Query Languages
1
Outline
• Tuple Relational Calculus
• Domain Relational Calculus
• Query By Example (QBE)
2
Tuple Relational Calculus
• A nonprocedural query language, where each query is of
the form
{t | P (t ) }
• It is the set of all tuples t such that predicate P is true for
t.
• t is a tuple variable, and we use
– t [A] to denote the value of tuple t on attribute A
– t r to denote that tuple t is in relation r
• P is a formula similar to that of the predicate calculus
– So, what does a formula consists of?
3
Predicate Calculus Formula
1. Set of attributes and constants
2. Set of comparison operators: (e.g., , , , , , )
3. Set of connectives: and (∧), or (∨)‚ not ()
4. Implication (): x y, if x is true, then y is true
x y x ∨ y
5. Set of quantifiers:
t r (Q (t )) “there exists” a tuple in t in relation r
such that predicate Q (t ) is true
t r (Q (t )) Q is true “for all” tuples t in relation r
4
Example Queries
Find the ID, name, dept_name, salary for instructors whose
salary is greater than $80,000
{t | t instructor t [salary ] 80000}
Find the set of all courses (show ID only) taught in the Fall
2009 semester, or in the Spring 2010 semester, or both
Find the set of all courses (show ID only) taught in the Fall 2009
semester, but not in the Spring 2010 semester
• Find all students (ID only) who have taken all courses
offered in the Biology department
11
Example Queries
• Find the ID, name, dept_name, salary for instructors
whose salary is greater than $80,000
{< i, n, d, s> | < i, n, d, s> instructor s 80000}
• As in the previous query, but output only the ID
attribute value
{< i> | n, d, s(< i, n, d, s> instructor s 80000)}
14
Query-by-Example (QBE) *
• Basic Structure
• Queries on One Relation
• Queries on Several Relations
• The Condition Box
• The Result Relation
• Ordering the Display of Tuples
• Aggregate Operations
• Modification of the Database
• Microsoft Access QBE
15
QBE — Basic Structure
• A graphical query language which is based (roughly)
on the domain relational calculus
• Two dimensional syntax – system creates templates
of relations that are requested by users
• Queries are expressed “by example”
16
QBE Skeleton Tables for the Bank Example
17
QBE Skeleton Tables (Cont.)
18
Queries on One Relation
19
Queries on One Relation (Cont.)
20
Queries on One Relation (Cont.)
Find the loan number of all loans with a loan amount of more
than $700
21
Queries on One Relation (Cont.)
Find the loan numbers of all loans made jointly to Smith and Jones.
22
Queries on Several Relations
Find the names of all customers who have a loan from the
Perryridge branch.
23
Queries on Several Relations (Cont.)
Find the names of all customers who have both an account and a
loan at the bank.
24
补充
Negation in QBE 内容
Find the names of all customers who have an account at the bank,
but do not have a loan from the bank.
25
补充
Negation in QBE (Cont.) 内容
26
补充
The Condition Box 内容
27
补充
Condition Box (Cont.) 内容
28
补充
Condition Box (Cont.) 内容
Find all account numbers with a balance greater than $1,300 and
less than $1,500
Find all account numbers with a balance greater than $1,300 and
less than $2,000 but not exactly $1,500.
29
补充
Condition Box (Cont.) 内容
Find all branches that have assets greater than those of at least
one branch located in Brooklyn
30
补充
The Result Relation 内容
31
补充
The Result Relation (Cont.) 内容
32
补充
Ordering the Display of Tuples 内容
33
补充
Aggregate Operations 内容
34
补充
Aggregate Operations (Cont.) 内容
35
补充
Query Examples 内容
36
补充
Query Example 内容
39
补充
Deletion Query Examples 内容
40
补充
Deletion Query Examples (Cont.) 内容
41
补充
Modification of the Database – Insertion 内容
42
补充
Modification of the Database – Insertion 内容
43
补充
Modification of the Database – Updates 内容
44
Microsoft Access QBE
• Microsoft Access supports a variant of QBE called Graphical Query
By Example (GQBE)
• GQBE differs from QBE in the following ways
– Attributes of relations are listed vertically, one below the
other, instead of horizontally
– Instead of using variables, lines (links) between attributes
are used to specify that their values should be the same.
• Links are added automatically on the basis of attribute name,
and the user can then add or delete links
• By default, a link specifies an inner join, but can be modified
to specify outer joins.
– Conditions, values to be printed, as well as group by
attributes are all specified together in a box called the
design grid
45
Example Queries
46
Example Queries
Find the name, street and city of all customers who have more than
one account at the bank
47
Next Lecture
• Entity-Relationship Model
48
End of Lecture 5
49