Database System Concepts, 6th
Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Chapter 6: Formal Relational QueryChapter 6: Formal Relational Query
LanguagesLanguages
©Silberschatz, Korth and Sudarshan6.2Database System Concepts - 6th
Edition
Chapter 6: Formal Relational QueryChapter 6: Formal Relational Query
LanguagesLanguages
Relational Algebra
Intermediate language used within DBMS
Procedural
Tuple Relational Calculus
Domain Relational Calculus
©Silberschatz, Korth and Sudarshan6.3Database System Concepts - 6th
Edition
Relational AlgebraRelational Algebra
Procedural language (More operational(procedural), very useful for
representing execution plans.)
Six basic operators
select: σ
project: ∏
union: ∪
set difference: –
Cartesian product: x
rename: ρ
The operators take one or two relations as inputs and produce a new
relation as a result.
©Silberschatz, Korth and Sudarshan6.4Database System Concepts - 6th
Edition
Relational Algebra in a DBMSRelational Algebra in a DBMS
©Silberschatz, Korth and Sudarshan6.5Database System Concepts - 6th
Edition
Select Operation – ExampleSelect Operation – Example
Relation r
σA=B ^ D > 5 (r)
©Silberschatz, Korth and Sudarshan6.6Database System Concepts - 6th
Edition
Select OperationSelect Operation
Notation: σ p(r)
p is called the selection predicate
Defined as:
σp(r) = {t | t ∈ r and p(t)}
Where p is a formula in propositional calculus consisting of terms
connected by : ∧ (and), ∨ (or), ¬ (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, ≠, >, ≥. <. ≤
Example of selection:
σ dept_name=“Physics”(instructor)
©Silberschatz, Korth and Sudarshan6.7Database System Concepts - 6th
Edition
Project Operation – ExampleProject Operation – Example
Relation r:
∏A,C (r)
©Silberschatz, Korth and Sudarshan6.8Database System Concepts - 6th
Edition
Project OperationProject Operation
Notation:
where A1, A2 are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained by erasing
the columns that are not listed
Duplicate rows removed from result, since relations are sets
Example: To eliminate the dept_name attribute of instructor
∏ID, name, salary (instructor)
)(,,2,1
r
kAAA ∏
©Silberschatz, Korth and Sudarshan6.9Database System Concepts - 6th
Edition
Union Operation – ExampleUnion Operation – Example
Relations r, s:
r ∪ s:
©Silberschatz, Korth and Sudarshan6.10Database System Concepts - 6th
Edition
Union OperationUnion Operation
Notation: r ∪ s
Defined as:
r ∪ s = {t | t ∈ r or t ∈ s}
For r ∪ s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd
column
of r deals with the same type of values as does the 2nd
column of s)
Example: 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))
©Silberschatz, Korth and Sudarshan6.11Database System Concepts - 6th
Edition
Set difference of two relationsSet difference of two relations
Relations r, s:
r – s:
©Silberschatz, Korth and Sudarshan6.12Database System Concepts - 6th
Edition
Set Difference OperationSet Difference Operation
Notation r – s
Defined as:
r – s = {t | t ∈ r and t ∉ s}
Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Example: 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))
©Silberschatz, Korth and Sudarshan6.13Database System Concepts - 6th
Edition
Cartesian-Product Operation –Cartesian-Product Operation –
ExampleExample
Relations r, s:
r x s:
©Silberschatz, Korth and Sudarshan6.14Database System Concepts - 6th
Edition
Cartesian-Product OperationCartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t ∈ r and q ∈ s}
Assume that attributes of r(R) and s(S) are
disjoint. (That is, R ∩ S = ∅).
If attributes of r(R) and s(S) are not disjoint, then
renaming must be used.
©Silberschatz, Korth and Sudarshan6.15Database System Concepts - 6th
Edition
Composition of OperationsComposition of Operations
Can build expressions using multiple operations
Example: σA=C(r x s)
r x s
σA=C(r x s)
©Silberschatz, Korth and Sudarshan6.16Database System Concepts - 6th
Edition
Tuple Relational CalculusTuple Relational Calculus
©Silberschatz, Korth and Sudarshan6.17Database System Concepts - 6th
Edition
Tuple Relational CalculusTuple Relational Calculus
A nonprocedural query language, where each query is of the form
{t | P (t ) } (Query form) (P (t ) is a expression involving variable t)
It is the set of all tuples t such that predicate P is true for t
t is a tuple variable, t [A ] denotes the value of tuple t on attribute A
(Tuple variable: variable that takes on tuples of a relation schema as values.)
t ∈ r denotes that tuple t is in relation r
P is a formula similar to that of the predicate calculus
©Silberschatz, Korth and Sudarshan6.18Database System Concepts - 6th
Edition
Predicate Calculus FormulaPredicate Calculus Formula
1. Set of attributes and constants
2. Set of comparison operators: (e.g., <, ≤, =, ≠, >, ≥)
3. Set of connectives: and (∧), or (v)‚ not (¬)
4. Implication (⇒): x ⇒ y, if x if true, then y is true
x ⇒ y ≡ ¬x v 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
©Silberschatz, Korth and Sudarshan6.19Database System Concepts - 6th
Edition
Example QueriesExample Queries
Find the ID, name, dept_name, salary for instructors whose salary is greater
than $80,000 (express on the query by using tuple relational calculus)
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
 Find the ID, name of instructor who works for department Music
{t[ID], t[name] | t ∈ instructor ∧ t[dept_name] = “Music”}
{t | t ∈ instructor ∧ t [salary ] > 80000}
©Silberschatz, Korth and Sudarshan6.20Database System Concepts - 6th
Edition
Find the name, salary of instructors who teaches in department History or
Biology.
{t[name], t[salary] | t ∈ instructor (t[dept_name] = “History” v t[dept_name] = “Biology”)}
Find the ID, course_id, of all teachers who instructor for department History.
{t[ID], t[course_id] | t∈ teaches ∧ ∃ i∈ instructor (i[ID] = t[ID] ∧ d ∈ department
(d[dept_name] = i[dept_name] ∧ d[dept_name] = ‘History’))}
Find the name of the instructor who teaches in every course.
{t[name] І t ∈ instructor ∧ ∀ s ∈ course (u ∈ teaches (u[ID] = t[ID] ∧ s [course_id] = u
[course_id]))}
Find the names of the students who have no advisor.
{t[name] І t ∈student ∧ ¬ (∃ s∈ advisor (s[s_id] = t[ID]))}
Example QueriesExample Queries
©Silberschatz, Korth and Sudarshan6.21Database System Concepts - 6th
Edition
Example QueriesExample Queries
Find the names of all instructors whose department is in the Watson
building
{t | ∃s ∈ section (t [course_id ] = s [course_id ] ∧
s [semester] = “Fall” ∧ s [year] = 2009
v ∃u ∈ section (t [course_id ] = u [course_id ] ∧
u [semester] = “Spring” ∧ u [year] = 2010)}
Find the set of all courses taught in the Fall 2009 semester, or in
the Spring 2010 semester, or both
{t | ∃s ∈ instructor (t [name ] = s [name ]
∧ ∃u ∈ department (u [dept_name ] = s[dept_name] “
∧ u [building] = “Watson” ))}
©Silberschatz, Korth and Sudarshan6.22Database System Concepts - 6th
Edition
Example QueriesExample Queries
{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 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 taught in the Fall 2009 semester, but not in
the Spring 2010 semester
©Silberschatz, Korth and Sudarshan6.23Database System Concepts - 6th
Edition
Example QueriesExample Queries
Find the names and ages of sailors with rating > 7
{ t | ∃S ∈ Sailors (S.rating > 7 ∧ t.name = S.sname ∧ t.age = S.age)}
• t is a Tuple variable with two fields: name and age.
- the only fields mentioned in t
- t does not range over any relation in the query.
Sailors
©Silberschatz, Korth and Sudarshan6.24Database System Concepts - 6th
Edition
Domain Relational CalculusDomain Relational Calculus
©Silberschatz, Korth and Sudarshan6.25Database System Concepts - 6th
Edition
Domain Relational CalculusDomain 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
©Silberschatz, Korth and Sudarshan6.26Database System Concepts - 6th
Edition
Example QueriesExample 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> | < 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” ))}
©Silberschatz, Korth and Sudarshan6.27Database System Concepts - 6th
Edition
Example QueriesExample Queries
{<c> | ∃ a, s, y, b, r, t ( <c, a, s, y, b, t > ∈ section ∧
s = “Fall” ∧ y = 2009 )
v ∃ a, s, y, b, r, t ( <c, a, s, y, b, t > ∈ section ] ∧
s = “Spring” ∧ y = 2010)}
Find the set of all courses taught in the Fall 2009 semester, or in
the Spring 2010 semester, or both
This case can also be written as
{<c> | ∃ a, s, y, b, r, t ( <c, a, s, y, b, 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, t > ∈ section ∧
s = “Fall” ∧ y = 2009 )
∧ ∃ a, s, y, b, r, t ( <c, a, s, y, b, t > ∈ section ] ∧
s = “Spring” ∧ y = 2010)}
©Silberschatz, Korth and Sudarshan6.28Database System Concepts - 6th
Edition
Safety of ExpressionsSafety 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).
©Silberschatz, Korth and Sudarshan6.29Database System Concepts - 6th
Edition
Universal QuantificationUniversal Quantification
Find all students who have taken all courses offered in the Biology
department
{< i > | ∃ n, d, tc ( < i, n, d, tc > ∈ student ∧
(∀ ci, ti, dn, cr ( < ci, ti, dn, cr > ∈ course ∧ dn =“Biology”
⇒ ∃ si, se, y, g ( <i, ci, si, se, y, g> ∈ takes ))}
Note that without the existential quantification on student, the
above query would be unsafe if the Biology department has not
offered any courses.
* Above query fixes bug in page 246, last query
Database System Concepts, 6th
Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
End of Chapter 6End of Chapter 6
©Silberschatz, Korth and Sudarshan6.31Database System Concepts - 6th
Edition
Figure 6.01Figure 6.01
©Silberschatz, Korth and Sudarshan6.32Database System Concepts - 6th
Edition
Figure 6.02Figure 6.02
©Silberschatz, Korth and Sudarshan6.33Database System Concepts - 6th
Edition
Figure 6.03Figure 6.03
©Silberschatz, Korth and Sudarshan6.34Database System Concepts - 6th
Edition
Figure 6.04Figure 6.04
©Silberschatz, Korth and Sudarshan6.35Database System Concepts - 6th
Edition
Figure 6.05Figure 6.05
©Silberschatz, Korth and Sudarshan6.36Database System Concepts - 6th
Edition
Figure 6.06Figure 6.06
©Silberschatz, Korth and Sudarshan6.37Database System Concepts - 6th
Edition
Figure 6.07Figure 6.07
©Silberschatz, Korth and Sudarshan6.38Database System Concepts - 6th
Edition
Figure 6.08Figure 6.08
©Silberschatz, Korth and Sudarshan6.39Database System Concepts - 6th
Edition
Figure 6.09Figure 6.09
©Silberschatz, Korth and Sudarshan6.40Database System Concepts - 6th
Edition
Figure 6.10Figure 6.10
©Silberschatz, Korth and Sudarshan6.41Database System Concepts - 6th
Edition
Figure 6.11Figure 6.11
©Silberschatz, Korth and Sudarshan6.42Database System Concepts - 6th
Edition
Figure 6.12Figure 6.12
©Silberschatz, Korth and Sudarshan6.43Database System Concepts - 6th
Edition
Figure 6.13Figure 6.13
©Silberschatz, Korth and Sudarshan6.44Database System Concepts - 6th
Edition
Figure 6.14Figure 6.14
©Silberschatz, Korth and Sudarshan6.45Database System Concepts - 6th
Edition
Figure 6.15Figure 6.15
©Silberschatz, Korth and Sudarshan6.46Database System Concepts - 6th
Edition
Figure 6.16Figure 6.16
©Silberschatz, Korth and Sudarshan6.47Database System Concepts - 6th
Edition
Figure 6.17Figure 6.17
©Silberschatz, Korth and Sudarshan6.48Database System Concepts - 6th
Edition
Figure 6.18Figure 6.18
©Silberschatz, Korth and Sudarshan6.49Database System Concepts - 6th
Edition
Figure 6.19Figure 6.19
©Silberschatz, Korth and Sudarshan6.50Database System Concepts - 6th
Edition
Figure 6.20Figure 6.20
©Silberschatz, Korth and Sudarshan6.51Database System Concepts - 6th
Edition
Figure 6.21Figure 6.21
©Silberschatz, Korth and Sudarshan6.52Database System Concepts - 6th
Edition
DeletionDeletion
A delete request is expressed similarly to a query, except
instead of displaying tuples to the user, the selected tuples are
removed from the database.
Can delete only whole tuples; cannot delete values on only
particular attributes
A deletion is expressed in relational algebra by:
r ← r – E
where r is a relation and E is a relational algebra query.
©Silberschatz, Korth and Sudarshan6.53Database System Concepts - 6th
Edition
Deletion ExamplesDeletion Examples
Delete all account records in the Perryridge branch.
Delete all accounts at branches located in Needham.
r1 ← σbranch_city = “Needham” (account branch )
r2 ← ∏ account_number, branch_name, balance (r1)
r3 ← ∏ customer_name, account_number (r2 depositor)
account ← account – r2
depositor ← depositor – r3
Delete all loan records with amount in the range of 0 to 50
loan ← loan – σ amount ≥ 0 and amount ≤ 50 (loan)
account ← account – σ branch_name = “Perryridge” (account )
©Silberschatz, Korth and Sudarshan6.54Database System Concepts - 6th
Edition
InsertionInsertion
To insert data into a relation, we either:
specify a tuple to be inserted
write a query whose result is a set of tuples to be inserted
in relational algebra, an insertion is expressed by:
r ← r ∪ E
where r is a relation and E is a relational algebra expression.
The insertion of a single tuple is expressed by letting E be a constant
relation containing one tuple.
©Silberschatz, Korth and Sudarshan6.55Database System Concepts - 6th
Edition
Insertion ExamplesInsertion Examples
Insert information in the database specifying that Smith has $1200 in
account A-973 at the Perryridge branch.
Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number serve
as the account number for the new savings account.
account ← account ∪ {(“A-973”, “Perryridge”, 1200)}
depositor ← depositor ∪ {(“Smith”, “A-973”)}
r1 ← (σbranch_name = “Perryridge” (borrower loan))
account ← account ∪ ∏loan_number, branch_name, 200 (r1)
depositor ← depositor ∪ ∏customer_name, loan_number (r1)
©Silberschatz, Korth and Sudarshan6.56Database System Concepts - 6th
Edition
UpdatingUpdating
A mechanism to change a value in a tuple without charging all values in
the tuple
Use the generalized projection operator to do this task
Each Fi is either
the I th
attribute of r, if the I th
attribute is not updated, or,
if the attribute is to be updated Fi is an expression, involving only
constants and the attributes of r, which gives the new value for the
attribute
)(,,,, 21
rr lFFF ∏←
©Silberschatz, Korth and Sudarshan6.57Database System Concepts - 6th
Edition
Update ExamplesUpdate Examples
Make interest payments by increasing all balances by 5 percent.
Pay all accounts with balances over $10,000 6 percent interest
and pay all others 5 percent
account ← ∏ account_number, branch_name, balance * 1.06 (σ BAL > 10000 (account ))
∪ ∏ account_number, branch_name, balance * 1.05 (σBAL ≤ 10000 (account))
account ← ∏ account_number, branch_name, balance * 1.05 (account)
©Silberschatz, Korth and Sudarshan6.58Database System Concepts - 6th
Edition
Example QueriesExample Queries
Find the names of all customers who have a loan and an account at
bank.
∏customer_name (borrower) ∩ ∏customer_name (depositor)
Find the name of all customers who have a loan at the bank and the
loan amount
∏customer_name, loan_number, amount (borrower loan)
©Silberschatz, Korth and Sudarshan6.59Database System Concepts - 6th
Edition
Query 1
∏customer_name (σbranch_name = “Downtown” (depositor account )) ∩
∏customer_name (σbranch_name = “Uptown” (depositor account))
Query 2
∏customer_name, branch_name (depositor account)
÷ ρtemp(branch_name) ({(“Downtown” ), (“Uptown” )})
Note that Query 2 uses a constant relation.
Example QueriesExample Queries
Find all customers who have an account from at least the “Downtown”
and the Uptown” branches.
©Silberschatz, Korth and Sudarshan6.60Database System Concepts - 6th
Edition
Find all customers who have an account at all branches located in
Brooklyn city.
Bank Example QueriesBank Example Queries
∏customer_name, branch_name (depositor account)
÷ ∏branch_name (σbranch_city = “Brooklyn” (branch))

Ch6 formal relational query languages

  • 1.
    Database System Concepts,6th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-use Chapter 6: Formal Relational QueryChapter 6: Formal Relational Query LanguagesLanguages
  • 2.
    ©Silberschatz, Korth andSudarshan6.2Database System Concepts - 6th Edition Chapter 6: Formal Relational QueryChapter 6: Formal Relational Query LanguagesLanguages Relational Algebra Intermediate language used within DBMS Procedural Tuple Relational Calculus Domain Relational Calculus
  • 3.
    ©Silberschatz, Korth andSudarshan6.3Database System Concepts - 6th Edition Relational AlgebraRelational Algebra Procedural language (More operational(procedural), very useful for representing execution plans.) Six basic operators select: σ project: ∏ union: ∪ set difference: – Cartesian product: x rename: ρ The operators take one or two relations as inputs and produce a new relation as a result.
  • 4.
    ©Silberschatz, Korth andSudarshan6.4Database System Concepts - 6th Edition Relational Algebra in a DBMSRelational Algebra in a DBMS
  • 5.
    ©Silberschatz, Korth andSudarshan6.5Database System Concepts - 6th Edition Select Operation – ExampleSelect Operation – Example Relation r σA=B ^ D > 5 (r)
  • 6.
    ©Silberschatz, Korth andSudarshan6.6Database System Concepts - 6th Edition Select OperationSelect Operation Notation: σ p(r) p is called the selection predicate Defined as: σp(r) = {t | t ∈ r and p(t)} Where p is a formula in propositional calculus consisting of terms connected by : ∧ (and), ∨ (or), ¬ (not) Each term is one of: <attribute> op <attribute> or <constant> where op is one of: =, ≠, >, ≥. <. ≤ Example of selection: σ dept_name=“Physics”(instructor)
  • 7.
    ©Silberschatz, Korth andSudarshan6.7Database System Concepts - 6th Edition Project Operation – ExampleProject Operation – Example Relation r: ∏A,C (r)
  • 8.
    ©Silberschatz, Korth andSudarshan6.8Database System Concepts - 6th Edition Project OperationProject Operation Notation: where A1, A2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets Example: To eliminate the dept_name attribute of instructor ∏ID, name, salary (instructor) )(,,2,1 r kAAA ∏
  • 9.
    ©Silberschatz, Korth andSudarshan6.9Database System Concepts - 6th Edition Union Operation – ExampleUnion Operation – Example Relations r, s: r ∪ s:
  • 10.
    ©Silberschatz, Korth andSudarshan6.10Database System Concepts - 6th Edition Union OperationUnion Operation Notation: r ∪ s Defined as: r ∪ s = {t | t ∈ r or t ∈ s} For r ∪ s to be valid. 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (example: 2nd column of r deals with the same type of values as does the 2nd column of s) Example: 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))
  • 11.
    ©Silberschatz, Korth andSudarshan6.11Database System Concepts - 6th Edition Set difference of two relationsSet difference of two relations Relations r, s: r – s:
  • 12.
    ©Silberschatz, Korth andSudarshan6.12Database System Concepts - 6th Edition Set Difference OperationSet Difference Operation Notation r – s Defined as: r – s = {t | t ∈ r and t ∉ s} Set differences must be taken between compatible relations. r and s must have the same arity attribute domains of r and s must be compatible Example: 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))
  • 13.
    ©Silberschatz, Korth andSudarshan6.13Database System Concepts - 6th Edition Cartesian-Product Operation –Cartesian-Product Operation – ExampleExample Relations r, s: r x s:
  • 14.
    ©Silberschatz, Korth andSudarshan6.14Database System Concepts - 6th Edition Cartesian-Product OperationCartesian-Product Operation Notation r x s Defined as: r x s = {t q | t ∈ r and q ∈ s} Assume that attributes of r(R) and s(S) are disjoint. (That is, R ∩ S = ∅). If attributes of r(R) and s(S) are not disjoint, then renaming must be used.
  • 15.
    ©Silberschatz, Korth andSudarshan6.15Database System Concepts - 6th Edition Composition of OperationsComposition of Operations Can build expressions using multiple operations Example: σA=C(r x s) r x s σA=C(r x s)
  • 16.
    ©Silberschatz, Korth andSudarshan6.16Database System Concepts - 6th Edition Tuple Relational CalculusTuple Relational Calculus
  • 17.
    ©Silberschatz, Korth andSudarshan6.17Database System Concepts - 6th Edition Tuple Relational CalculusTuple Relational Calculus A nonprocedural query language, where each query is of the form {t | P (t ) } (Query form) (P (t ) is a expression involving variable t) It is the set of all tuples t such that predicate P is true for t t is a tuple variable, t [A ] denotes the value of tuple t on attribute A (Tuple variable: variable that takes on tuples of a relation schema as values.) t ∈ r denotes that tuple t is in relation r P is a formula similar to that of the predicate calculus
  • 18.
    ©Silberschatz, Korth andSudarshan6.18Database System Concepts - 6th Edition Predicate Calculus FormulaPredicate Calculus Formula 1. Set of attributes and constants 2. Set of comparison operators: (e.g., <, ≤, =, ≠, >, ≥) 3. Set of connectives: and (∧), or (v)‚ not (¬) 4. Implication (⇒): x ⇒ y, if x if true, then y is true x ⇒ y ≡ ¬x v 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
  • 19.
    ©Silberschatz, Korth andSudarshan6.19Database System Concepts - 6th Edition Example QueriesExample Queries Find the ID, name, dept_name, salary for instructors whose salary is greater than $80,000 (express on the query by using tuple relational calculus) 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  Find the ID, name of instructor who works for department Music {t[ID], t[name] | t ∈ instructor ∧ t[dept_name] = “Music”} {t | t ∈ instructor ∧ t [salary ] > 80000}
  • 20.
    ©Silberschatz, Korth andSudarshan6.20Database System Concepts - 6th Edition Find the name, salary of instructors who teaches in department History or Biology. {t[name], t[salary] | t ∈ instructor (t[dept_name] = “History” v t[dept_name] = “Biology”)} Find the ID, course_id, of all teachers who instructor for department History. {t[ID], t[course_id] | t∈ teaches ∧ ∃ i∈ instructor (i[ID] = t[ID] ∧ d ∈ department (d[dept_name] = i[dept_name] ∧ d[dept_name] = ‘History’))} Find the name of the instructor who teaches in every course. {t[name] І t ∈ instructor ∧ ∀ s ∈ course (u ∈ teaches (u[ID] = t[ID] ∧ s [course_id] = u [course_id]))} Find the names of the students who have no advisor. {t[name] І t ∈student ∧ ¬ (∃ s∈ advisor (s[s_id] = t[ID]))} Example QueriesExample Queries
  • 21.
    ©Silberschatz, Korth andSudarshan6.21Database System Concepts - 6th Edition Example QueriesExample Queries Find the names of all instructors whose department is in the Watson building {t | ∃s ∈ section (t [course_id ] = s [course_id ] ∧ s [semester] = “Fall” ∧ s [year] = 2009 v ∃u ∈ section (t [course_id ] = u [course_id ] ∧ u [semester] = “Spring” ∧ u [year] = 2010)} Find the set of all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or both {t | ∃s ∈ instructor (t [name ] = s [name ] ∧ ∃u ∈ department (u [dept_name ] = s[dept_name] “ ∧ u [building] = “Watson” ))}
  • 22.
    ©Silberschatz, Korth andSudarshan6.22Database System Concepts - 6th Edition Example QueriesExample Queries {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 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 taught in the Fall 2009 semester, but not in the Spring 2010 semester
  • 23.
    ©Silberschatz, Korth andSudarshan6.23Database System Concepts - 6th Edition Example QueriesExample Queries Find the names and ages of sailors with rating > 7 { t | ∃S ∈ Sailors (S.rating > 7 ∧ t.name = S.sname ∧ t.age = S.age)} • t is a Tuple variable with two fields: name and age. - the only fields mentioned in t - t does not range over any relation in the query. Sailors
  • 24.
    ©Silberschatz, Korth andSudarshan6.24Database System Concepts - 6th Edition Domain Relational CalculusDomain Relational Calculus
  • 25.
    ©Silberschatz, Korth andSudarshan6.25Database System Concepts - 6th Edition Domain Relational CalculusDomain 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
  • 26.
    ©Silberschatz, Korth andSudarshan6.26Database System Concepts - 6th Edition Example QueriesExample 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> | < 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” ))}
  • 27.
    ©Silberschatz, Korth andSudarshan6.27Database System Concepts - 6th Edition Example QueriesExample Queries {<c> | ∃ a, s, y, b, r, t ( <c, a, s, y, b, t > ∈ section ∧ s = “Fall” ∧ y = 2009 ) v ∃ a, s, y, b, r, t ( <c, a, s, y, b, t > ∈ section ] ∧ s = “Spring” ∧ y = 2010)} Find the set of all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or both This case can also be written as {<c> | ∃ a, s, y, b, r, t ( <c, a, s, y, b, 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, t > ∈ section ∧ s = “Fall” ∧ y = 2009 ) ∧ ∃ a, s, y, b, r, t ( <c, a, s, y, b, t > ∈ section ] ∧ s = “Spring” ∧ y = 2010)}
  • 28.
    ©Silberschatz, Korth andSudarshan6.28Database System Concepts - 6th Edition Safety of ExpressionsSafety 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).
  • 29.
    ©Silberschatz, Korth andSudarshan6.29Database System Concepts - 6th Edition Universal QuantificationUniversal Quantification Find all students who have taken all courses offered in the Biology department {< i > | ∃ n, d, tc ( < i, n, d, tc > ∈ student ∧ (∀ ci, ti, dn, cr ( < ci, ti, dn, cr > ∈ course ∧ dn =“Biology” ⇒ ∃ si, se, y, g ( <i, ci, si, se, y, g> ∈ takes ))} Note that without the existential quantification on student, the above query would be unsafe if the Biology department has not offered any courses. * Above query fixes bug in page 246, last query
  • 30.
    Database System Concepts,6th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-use End of Chapter 6End of Chapter 6
  • 31.
    ©Silberschatz, Korth andSudarshan6.31Database System Concepts - 6th Edition Figure 6.01Figure 6.01
  • 32.
    ©Silberschatz, Korth andSudarshan6.32Database System Concepts - 6th Edition Figure 6.02Figure 6.02
  • 33.
    ©Silberschatz, Korth andSudarshan6.33Database System Concepts - 6th Edition Figure 6.03Figure 6.03
  • 34.
    ©Silberschatz, Korth andSudarshan6.34Database System Concepts - 6th Edition Figure 6.04Figure 6.04
  • 35.
    ©Silberschatz, Korth andSudarshan6.35Database System Concepts - 6th Edition Figure 6.05Figure 6.05
  • 36.
    ©Silberschatz, Korth andSudarshan6.36Database System Concepts - 6th Edition Figure 6.06Figure 6.06
  • 37.
    ©Silberschatz, Korth andSudarshan6.37Database System Concepts - 6th Edition Figure 6.07Figure 6.07
  • 38.
    ©Silberschatz, Korth andSudarshan6.38Database System Concepts - 6th Edition Figure 6.08Figure 6.08
  • 39.
    ©Silberschatz, Korth andSudarshan6.39Database System Concepts - 6th Edition Figure 6.09Figure 6.09
  • 40.
    ©Silberschatz, Korth andSudarshan6.40Database System Concepts - 6th Edition Figure 6.10Figure 6.10
  • 41.
    ©Silberschatz, Korth andSudarshan6.41Database System Concepts - 6th Edition Figure 6.11Figure 6.11
  • 42.
    ©Silberschatz, Korth andSudarshan6.42Database System Concepts - 6th Edition Figure 6.12Figure 6.12
  • 43.
    ©Silberschatz, Korth andSudarshan6.43Database System Concepts - 6th Edition Figure 6.13Figure 6.13
  • 44.
    ©Silberschatz, Korth andSudarshan6.44Database System Concepts - 6th Edition Figure 6.14Figure 6.14
  • 45.
    ©Silberschatz, Korth andSudarshan6.45Database System Concepts - 6th Edition Figure 6.15Figure 6.15
  • 46.
    ©Silberschatz, Korth andSudarshan6.46Database System Concepts - 6th Edition Figure 6.16Figure 6.16
  • 47.
    ©Silberschatz, Korth andSudarshan6.47Database System Concepts - 6th Edition Figure 6.17Figure 6.17
  • 48.
    ©Silberschatz, Korth andSudarshan6.48Database System Concepts - 6th Edition Figure 6.18Figure 6.18
  • 49.
    ©Silberschatz, Korth andSudarshan6.49Database System Concepts - 6th Edition Figure 6.19Figure 6.19
  • 50.
    ©Silberschatz, Korth andSudarshan6.50Database System Concepts - 6th Edition Figure 6.20Figure 6.20
  • 51.
    ©Silberschatz, Korth andSudarshan6.51Database System Concepts - 6th Edition Figure 6.21Figure 6.21
  • 52.
    ©Silberschatz, Korth andSudarshan6.52Database System Concepts - 6th Edition DeletionDeletion A delete request is expressed similarly to a query, except instead of displaying tuples to the user, the selected tuples are removed from the database. Can delete only whole tuples; cannot delete values on only particular attributes A deletion is expressed in relational algebra by: r ← r – E where r is a relation and E is a relational algebra query.
  • 53.
    ©Silberschatz, Korth andSudarshan6.53Database System Concepts - 6th Edition Deletion ExamplesDeletion Examples Delete all account records in the Perryridge branch. Delete all accounts at branches located in Needham. r1 ← σbranch_city = “Needham” (account branch ) r2 ← ∏ account_number, branch_name, balance (r1) r3 ← ∏ customer_name, account_number (r2 depositor) account ← account – r2 depositor ← depositor – r3 Delete all loan records with amount in the range of 0 to 50 loan ← loan – σ amount ≥ 0 and amount ≤ 50 (loan) account ← account – σ branch_name = “Perryridge” (account )
  • 54.
    ©Silberschatz, Korth andSudarshan6.54Database System Concepts - 6th Edition InsertionInsertion To insert data into a relation, we either: specify a tuple to be inserted write a query whose result is a set of tuples to be inserted in relational algebra, an insertion is expressed by: r ← r ∪ E where r is a relation and E is a relational algebra expression. The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple.
  • 55.
    ©Silberschatz, Korth andSudarshan6.55Database System Concepts - 6th Edition Insertion ExamplesInsertion Examples Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch. Provide as a gift for all loan customers in the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account. account ← account ∪ {(“A-973”, “Perryridge”, 1200)} depositor ← depositor ∪ {(“Smith”, “A-973”)} r1 ← (σbranch_name = “Perryridge” (borrower loan)) account ← account ∪ ∏loan_number, branch_name, 200 (r1) depositor ← depositor ∪ ∏customer_name, loan_number (r1)
  • 56.
    ©Silberschatz, Korth andSudarshan6.56Database System Concepts - 6th Edition UpdatingUpdating A mechanism to change a value in a tuple without charging all values in the tuple Use the generalized projection operator to do this task Each Fi is either the I th attribute of r, if the I th attribute is not updated, or, if the attribute is to be updated Fi is an expression, involving only constants and the attributes of r, which gives the new value for the attribute )(,,,, 21 rr lFFF ∏←
  • 57.
    ©Silberschatz, Korth andSudarshan6.57Database System Concepts - 6th Edition Update ExamplesUpdate Examples Make interest payments by increasing all balances by 5 percent. Pay all accounts with balances over $10,000 6 percent interest and pay all others 5 percent account ← ∏ account_number, branch_name, balance * 1.06 (σ BAL > 10000 (account )) ∪ ∏ account_number, branch_name, balance * 1.05 (σBAL ≤ 10000 (account)) account ← ∏ account_number, branch_name, balance * 1.05 (account)
  • 58.
    ©Silberschatz, Korth andSudarshan6.58Database System Concepts - 6th Edition Example QueriesExample Queries Find the names of all customers who have a loan and an account at bank. ∏customer_name (borrower) ∩ ∏customer_name (depositor) Find the name of all customers who have a loan at the bank and the loan amount ∏customer_name, loan_number, amount (borrower loan)
  • 59.
    ©Silberschatz, Korth andSudarshan6.59Database System Concepts - 6th Edition Query 1 ∏customer_name (σbranch_name = “Downtown” (depositor account )) ∩ ∏customer_name (σbranch_name = “Uptown” (depositor account)) Query 2 ∏customer_name, branch_name (depositor account) ÷ ρtemp(branch_name) ({(“Downtown” ), (“Uptown” )}) Note that Query 2 uses a constant relation. Example QueriesExample Queries Find all customers who have an account from at least the “Downtown” and the Uptown” branches.
  • 60.
    ©Silberschatz, Korth andSudarshan6.60Database System Concepts - 6th Edition Find all customers who have an account at all branches located in Brooklyn city. Bank Example QueriesBank Example Queries ∏customer_name, branch_name (depositor account) ÷ ∏branch_name (σbranch_city = “Brooklyn” (branch))

Editor's Notes

  • #4 A query plan (or query execution plan) is an ordered set of steps used to access data in a SQL relational database management system. The set of operations that the optimizer chooses to perform the most efficient query is called the “query execution plan”. The Query Execution Plans describe the steps and the order used to access or modify data in the database.  Once you have this information you can identify what parts of the query are slow.
  • #11 The arity of a relation (or predicate) is the dimension of the domain in the corresponding Cartesian product. (A function of arity n thus has arity n+1 considered as a relation.)  The term &amp;quot;arity&amp;quot; is rarely employed in everyday usage. For example, rather than saying &amp;quot;the arity of the addition operation is 2&amp;quot; or &amp;quot;addition is an operation of arity 2&amp;quot; one usually says &amp;quot;addition is a binary operation&amp;quot;.
  • #17 Predicate are used to describe certain properties or relationships between individuals or objects. For instance, in “Mary and ANNE are sisters,” the phrase “are sisters” is a predicate. Z+ ={x | x is a positive integer}
  • #18 When we write a relational-algebra expression, we provide a sequence of procedures that generates the answer to our query. The tuple relational calculus, by contrast, is a nonprocedural query languages. It describes the desired information without giving a specific procedure for obtaining that information. A query in the tuple relational calculus is expressed as: {t | P (t ) } That is, it is the set of all tuples t such that predicate p is true for t. Following our earlier notation, we use t[A] to denote the value of tuple t on attribute A, and we use t  r denotes that tuple t is in relation r
  • #19 Quantifiers indicate how frequently a certain statement is true (, ). Let A represent an expression, and let x represent a variable. If we want to indicate that A is true for all possible values of x, we write x A. Here, x is called the universal quantifier, and A is called the scope of the quantifier. The variable x is said to be bound by the quantifier. The symbol is pronounced “for all”. If we want to indicate that A is true for at least one value x, we write x A.
  • #20 Suppose that we want only the ID attribute, rather than all attributes of the instructor relation. To write this query in the tuple relational calculus, we need to write an expression for a relation on the schema (ID). We need those tuples on (ID) such that there is a tuple in instructor with salary attribute &amp;gt; 80000. To express this request, we need the construct “there exists” from mathematical logic. t r (Q (t )) mean ”there exists a tuple n t in relation r such that predicate Q (t ) is true.” Using this notation, we can write the query “Find the instructor ID for each instructor with a salary greater than $80,000” as: {t |  s instructor (t [ID ] = s [ID ]  s [salary ]  80000)} In English, we read the preceding expression as “The set of all tuples t such that there exists a tuple s in relation instructor for which the values of t and s for ID attribute are equal, and value of s for the salary attribute is greater than $80,000”.
  • #22 Consider the query “Find the names of all instructors whose department is in the Watson building” This query is slightly more complex than the previous queries, since it involves two relations: instructor and department. As we shall see, however, all it requires is that we have two “there exists” clauses in our tuple-relational-calculus expression, connected by and (). We write the query as follows: {t | s  instructor (t [name ] = s [name ]  u  department (u [dept_name ] = s[dept_name] “  u [building] = “Watson” ))} Tuple variable u is restricted to departments that are located in the Watson building, while tuple variable s is restricted to instructors whose dept_name matches that of tuple variable u. To find the set of all courses taught in the Fall 2009 semester, the Spring 2010 semester, or both, we used the union operation in relational algebra. In the tuple relational calculus, we shall need two “there exists” clauses, connected by or (v):
  • #26 A second form of relational calculus, domain relational calculus, uses domain variables that takes on values from on attributes domain , rather than values for an entire tuple. TRC: Variables range over (i.e., get bound to) tuples. Like SQL. DRC: Variables range over domain elements (= field values). Like Query-By-Example (QBE)