0% found this document useful (0 votes)
8 views

L - Other Relational Query Languages

Uploaded by

18090ww
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

L - Other Relational Query Languages

Uploaded by

18090ww
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Extended Learning

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}

Notice that a relation on schema (ID, name, dept_name, salary)


is implicitly defined by the query

 As in the previous query, but output only the ID attribute value

{t |  s instructor (t [ID ] = s [ID ]  s [salary ]  80000)}

Notice that a relation on schema (ID) is implicitly defined


by the query
Example Queries

 Find the names of all instructors whose department is in


the Watson building

{t | s  instructor (t [name ] = s [name ]


 u  department (u [dept_name ] = s[dept_name]
 u [building] = “Watson” ))}
Example Queries

 Find the set of all courses (show ID only) taught in the Fall
2009 semester, or in the Spring 2010 semester, or both

{t | s  section (t [course_id ] = s [course_id ] 


s [semester] = “Fall”  s [year] = 2009)
∨ u  section (t [course_id ] = u [course_id ] 
u [semester] = “Spring”  u [year] = 2010 )}
Example Queries
Find the set of all courses (show ID only) taught in the Fall 2009
semester, and in the Spring 2010 semester

{t | s  section (t [course_id ] = s [course_id ] 


s [semester] = “Fall”  s [year] = 2009)
 u  section (t [course_id ] = u [course_id ] 
u [semester] = “Spring”  u [year] = 2010 )}

Find the set of all courses (show ID only) taught in the Fall 2009
semester, but not in the Spring 2010 semester

{t | s  section (t [course_id ] = s [course_id ] 


s [semester] = “Fall”  s [year] = 2009)
  u  section (t [course_id ] = u [course_id ] 
u [semester] = “Spring”  u [year] = 2010 )}
Universal Quantification

• Find all students (ID only) who have taken all courses
offered in the Biology department

{t |  r  student (t [ID] = r [ID]) 


( u  course (u [dept_name]=“Biology” 
 s  takes (t [ID] = s [ID ] 
s [course_id] = u [course_id]))}
Safety of Expressions
• Avoid generating infinite relations
– { t |  (t r) } results in an infinite relation if the
domain of any attribute of relation r is infinite
– To guard against the problem, we define the
domain of formula P in {t | P (t )} as:
• The set of all values that appear in P, or
• The set of all values that appear in one or more
relations whose names appear in P
– An expression {t | P (t )} in the tuple relational
calculus is safe if all values that appear in the result
are values from dom(P)
• e.g., { t |  (t r) } is not safe; { t | t [A] = 5  true } is
not safe, either.
10
Domain Relational Calculus
• A nonprocedural query language equivalent in power to
the tuple relational calculus
• Each query is an expression of the form:

{  x1, x2, …, xn  | P (x1, x2, …, xn)}

– x1, x2, …, xn represent domain variables


– P represents a formula similar to that of the predicate
calculus

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)}

• Find the names of all instructors whose department is


in the Watson building
{< n > |  i, d, s (< i, n, d, s >  instructor
  b, a (< d, b, a>  department  b = “Watson”
))}
Example Queries
Find the set of all courses taught in the Fall 2009 semester, or in
the Spring 2010 semester, or both
{<c> |  a, s, y, b, r, t ( <c, a, s, y, b, r, t >  section 
s = “Fall”  y = 2009 )
v  a, s, y, b, r, t ( <c, a, s, y, b, r, t >  section ] 
s = “Spring”  y = 2010)}
This case can also be written as
{<c> |  a, s, y, b, r, t ( <c, a, s, y, b, r, t >  section 
( (s = “Fall”  y = 2009 ) v (s = “Spring”  y = 2010))}
Find the set of all courses taught in the Fall 2009 semester, and in
the Spring 2010 semester

{<c> |  a, s, y, b, r, t ( <c, a, s, y, b, r, t >  section 


s = “Fall”  y = 2009 )
  a, s, y, b, r, t ( <c, a, s, y, b, r, t >  section ] 
s = “Spring”  y = 2010)}
Safety of Expressions
The expression:
{  x1, x2, …, xn  | P (x1, x2, …, xn )}
is safe if all of the following hold:
1. All values that appear in tuples of the expression are
values from dom (P ) (that is, the values appear either
in P or in a tuple of a relation mentioned in P ).
2. For every “there exists” subformula of the form  x
(P1(x )), the subformula is true if and only if there is a
value of x in dom (P1) such that P1(x ) is true.
3. For every “for all” subformula of the form  x (P1 (x )),
the subformula is true if and only if P1(x ) is true for all
values x from dom (P1).

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

• Find all loan numbers at the Perryridge branch.

_x is a variable (optional; can be omitted in above query)


P. means print (display)
duplicates are removed by default
To retain duplicates use P.ALL

19
Queries on One Relation (Cont.)

• Display full details of all loans


Method 1:

P._x P._y P._z

Method 2: Shorthand notation

20
Queries on One Relation (Cont.)
Find the loan number of all loans with a loan amount of more
than $700

Find names of all branches that are not located in Brooklyn

21
Queries on One Relation (Cont.)
Find the loan numbers of all loans made jointly to Smith and Jones.

Find all customers who live in the same city as 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.

* ¬ means “there does not exist”

25
补充
Negation in QBE (Cont.) 内容

Find all customers who have at least two accounts.

* ¬ means “not equal to”

26
补充
The Condition Box 内容

• Find the loan numbers of all loans made to Smith, to


Jones, or to both jointly

• Allows the expression of constraints on domain


variables that are either inconvenient or impossible to
express within the skeleton tables.
• Complex conditions can be used in condition boxes.

27
补充
Condition Box (Cont.) 内容

• QBE supports an interesting syntax for expressing


alternative values

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 内容

• Find the customer_name, account_number, and


balance for all customers who have an account at the
Perryridge branch.
– We need to:
• Join depositor and account.
• Project customer_name, account_number and
balance.
– To accomplish this we:
• Create a skeleton table, called result, with attributes
customer_name, account_number, and balance.
• Write the query.

31
补充
The Result Relation (Cont.) 内容

• The resulting query is:

32
补充
Ordering the Display of Tuples 内容

• AO = ascending order; DO = descending order.


• Example: list in ascending alphabetical order all customers who
have an account at the bank

• When sorting on multiple attributes, the sorting order is specified by


including with each sort operator (AO or DO) an integer surrounded
by parentheses.
• Example: List all account numbers at the Perryridge branch in
ascending alphabetic order with their respective account balances in
descending order.

33
补充
Aggregate Operations 内容

• The aggregate operators are AVG, MAX, MIN, SUM,


and CNT
• The above operators must be postfixed with “ALL”
(e.g., SUM.ALL. or AVG.ALL._x) to ensure that
duplicates are not eliminated.
• Example: Find the total balance of all the accounts
maintained at the Perryridge branch.

34
补充
Aggregate Operations (Cont.) 内容

• UNQ is used to specify that we want to eliminate


duplicates
• Find the total number of customers having an account at
the bank.

35
补充
Query Examples 内容

• Find the average balance at each branch.

The “G” in “P.G” is analogous to SQL’s group by construct


The “ALL” in the “P.AVG.ALL” entry in the balance column
ensures that all balances are considered
To find the average account balance at only those branches
where the average account balance is more than $1,200…
we simply add the condition box:

36
补充
Query Example 内容

• Find all customers who have an account at all


branches located in Brooklyn.
– Approach: for each customer, find the number
of branches in Brooklyn at which they have
accounts, and compare with total number of
branches in Brooklyn
– QBE does not provide subquery functionality, so
both above tasks have to be combined in a
single query.
• Can be done for this query, but there are
queries that require subqueries and cannot
always be expressed in QBE.
37
补充
Query Example (Cont.) 内容

 CNT.UNQ._w specifies the number of distinct branches in Brooklyn.


Note: The variable _w is not connected to other variables in the query
 CNT.UNQ._z specifies the number of distinct branches in Brooklyn at
which customer x has an account.
38
补充
Modification of the Database – Deletion 内容

• Deletion of tuples from a relation is expressed by use of a D.


command. In the case where we delete information in only some of
the columns, null values, specified by –, are inserted.
• Delete customer Smith

• Delete the branch_city value of the branch whose name is


“Perryridge”.

39
补充
Deletion Query Examples 内容

• Delete all loans with a loan amount greater than $1300


and less than $1500.
– For consistency, we have to delete information
from loan and borrower tables

40
补充
Deletion Query Examples (Cont.) 内容

• Delete all accounts at branches located in Brooklyn.

41
补充
Modification of the Database – Insertion 内容

• Insertion is done by placing the I. operator in the query


expression.
• Insert the fact that account A-9732 at the Perryridge
branch has a balance of $700.

42
补充
Modification of the Database – Insertion 内容

Provide as a gift for all loan customers of the Perryridge


branch, a new $200 savings account for every loan
account they have, with the loan number serving as the
account number for the new savings account.

43
补充
Modification of the Database – Updates 内容

• Use the U. operator to change a value in a tuple


without changing all values in the tuple. QBE does not
allow users to update the primary key fields.
• Update the asset value of the Perryridge branch to
$10,000,000.

• Increase all balances by 5 percent.

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

Find the customer_name, account_number and balance for all


accounts at the Perryridge branch

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

You might also like