Advanced Relational Database Design
Advanced Relational Database Design
Languages
Query-by-Example (QBE)
Access
Datalog
Database System Concepts - 6th Edition c.2 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 6th Edition c.3 ©Silberschatz, Korth and Sudarshan
QBE — Basic Structure
Database System Concepts - 6th Edition c.4 ©Silberschatz, Korth and Sudarshan
QBE Skeleton Tables for the Bank Example
Database System Concepts - 6th Edition c.5 ©Silberschatz, Korth and Sudarshan
QBE Skeleton Tables (Cont.)
Database System Concepts - 6th Edition c.6 ©Silberschatz, Korth and Sudarshan
Queries on One Relation
P._x
Database System Concepts - 6th Edition c.7 ©Silberschatz, Korth and Sudarshan
Queries on One Relation (Cont.)
z Method 1:
Database System Concepts - 6th Edition c.8 ©Silberschatz, Korth and Sudarshan
Queries on One Relation (Cont.)
Find the loan number of all loans with a loan amount of more than $700
Database System Concepts - 6th Edition c.9 ©Silberschatz, Korth and Sudarshan
Queries on One Relation (Cont.)
Find the loan numbers of all loans made jointly to Smith and
Jones.
Database System Concepts - 6th Edition c.10 ©Silberschatz, Korth and Sudarshan
Queries on Several Relations
Find the names of all customers who have a loan from the
Perryridge branch.
Database System Concepts - 6th Edition c.11 ©Silberschatz, Korth and Sudarshan
Queries on Several Relations (Cont.)
Find the names of all customers who have both an account and a loan
at the bank.
Database System Concepts - 6th Edition c.12 ©Silberschatz, Korth and Sudarshan
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.
Database System Concepts - 6th Edition c.13 ©Silberschatz, Korth and Sudarshan
Negation in QBE (Cont.)
Database System Concepts - 6th Edition c.14 ©Silberschatz, Korth and Sudarshan
The Condition Box
Database System Concepts - 6th Edition c.15 ©Silberschatz, Korth and Sudarshan
Condition Box (Cont.)
Database System Concepts - 6th Edition c.16 ©Silberschatz, Korth and Sudarshan
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.
Database System Concepts - 6th Edition c.17 ©Silberschatz, Korth and Sudarshan
Condition Box (Cont.)
Find all branches that have assets greater than those of at least one
branch located in Brooklyn.
Database System Concepts - 6th Edition c.18 ©Silberschatz, Korth and Sudarshan
The Result Relation
Database System Concepts - 6th Edition c.19 ©Silberschatz, Korth and Sudarshan
The Result Relation (Cont.)
Database System Concepts - 6th Edition c.20 ©Silberschatz, Korth and Sudarshan
Ordering the Display of Tuples
Database System Concepts - 6th Edition c.21 ©Silberschatz, Korth and Sudarshan
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.
Database System Concepts - 6th Edition c.22 ©Silberschatz, Korth and Sudarshan
Aggregate Operations (Cont.)
Database System Concepts - 6th Edition c.23 ©Silberschatz, Korth and Sudarshan
Query Examples
Database System Concepts - 6th Edition c.24 ©Silberschatz, Korth and Sudarshan
Query Example
Database System Concepts - 6th Edition c.25 ©Silberschatz, Korth and Sudarshan
Query Example (Cont.)
Database System Concepts - 6th Edition c.26 ©Silberschatz, Korth and Sudarshan
Modification of the Database – Deletion
Database System Concepts - 6th Edition c.27 ©Silberschatz, Korth and Sudarshan
Deletion Query Examples
Delete all loans with a loan amount greater than $1300 and less than
$1500.
z For consistency, we have to delete information from loan and
borrower tables
Database System Concepts - 6th Edition c.28 ©Silberschatz, Korth and Sudarshan
Deletion Query Examples (Cont.)
Database System Concepts - 6th Edition c.29 ©Silberschatz, Korth and Sudarshan
Modification of the Database – Insertion
Database System Concepts - 6th Edition c.30 ©Silberschatz, Korth and Sudarshan
Modification of the Database – Insertion (Cont.)
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.
Database System Concepts - 6th Edition c.31 ©Silberschatz, Korth and Sudarshan
Modification of the Database – Updates
Database System Concepts - 6th Edition c.32 ©Silberschatz, Korth and Sudarshan
Microsoft Access QBE
Database System Concepts - 6th Edition c.33 ©Silberschatz, Korth and Sudarshan
An Example Query in Microsoft Access QBE
Database System Concepts - 6th Edition c.34 ©Silberschatz, Korth and Sudarshan
An Aggregation Query in Access QBE
Find the name, street and city of all customers who have more than one
account at the bank.
Database System Concepts - 6th Edition c.35 ©Silberschatz, Korth and Sudarshan
Aggregation in Access QBE
Database System Concepts - 6th Edition c.36 ©Silberschatz, Korth and Sudarshan
Datalog
Basic Structure
Syntax of Datalog Rules
Semantics of Nonrecursive Datalog
Safety
Relational Operations in Datalog
Recursion in Datalog
The Power of Recursion
Database System Concepts - 6th Edition c.37 ©Silberschatz, Korth and Sudarshan
Basic Structure
Database System Concepts - 6th Edition c.38 ©Silberschatz, Korth and Sudarshan
Example Queries
Each rule defines a set of tuples that a view relation must contain.
Database System Concepts - 6th Edition c.39 ©Silberschatz, Korth and Sudarshan
Negation in Datalog
Define a view relation c that contains the names of all customers who
have a deposit but no loan at the bank:
c(N) :– depositor (N, A), not is_borrower (N).
is_borrower (N) :–borrower (N,L).
NOTE: using not borrower (N, L) in the first rule results in a different
meaning, namely there is some loan L for which N is not a borrower.
z To prevent such confusion, we require all variables in negated
“predicate” to also be present in non-negated predicates.
Database System Concepts - 6th Edition c.40 ©Silberschatz, Korth and Sudarshan
Named Attribute Notation
Database System Concepts - 6th Edition c.41 ©Silberschatz, Korth and Sudarshan
Formal Syntax and Semantics of Datalog
Database System Concepts - 6th Edition c.42 ©Silberschatz, Korth and Sudarshan
Syntax of Datalog Rules
Database System Concepts - 6th Edition c.43 ©Silberschatz, Korth and Sudarshan
Syntax of Datalog Rules (Cont.)
head body
z each Li is a literal
z head – the literal p (t1, t2, ..., tn )
z body – the rest of the literals
A fact is a rule with an empty body, written in the form:
p (v1, v2, ..., vn ).
z indicates tuple (v1, v2, ..., vn ) is in relation p
A Datalog program is a set of rules.
Database System Concepts - 6th Edition c.44 ©Silberschatz, Korth and Sudarshan
Semantics of a Rule
Database System Concepts - 6th Edition c.45 ©Silberschatz, Korth and Sudarshan
Semantics of a Rule (Cont.)
We define the set of facts that can be inferred from a given set of
facts l using rule R as:
infer(R, l) = { p (t1, ..., tn) | there is a ground instantiation R’ of R
where p (t1, ..., tn ) is the head of R’, and
the body of R’ is satisfied in l }
Given an set of rules ℜ = {R1, R2, ..., Rn}, we define
infer(ℜ, l) = infer (R1, l ) ∪ infer (R2, l ) ∪ ... ∪ infer (Rn, l )
Database System Concepts - 6th Edition c.46 ©Silberschatz, Korth and Sudarshan
Layering of Rules
Database System Concepts - 6th Edition c.47 ©Silberschatz, Korth and Sudarshan
Layering Rules (Cont.)
Formally:
A relation is a layer 1 if all relations used in the bodies of rules defining
it are stored in the database.
A relation is a layer 2 if all relations used in the bodies of rules defining
it are either stored in the database, or are in layer 1.
A relation p is in layer i + 1 if
z it is not in layers 1, 2, ..., i
z all relations used in the bodies of rules defining a p are either
stored in the database, or are in layers 1, 2, ..., i
Database System Concepts - 6th Edition c.48 ©Silberschatz, Korth and Sudarshan
Semantics of a Program
Database System Concepts - 6th Edition c.49 ©Silberschatz, Korth and Sudarshan
Safety
Database System Concepts - 6th Edition c.50 ©Silberschatz, Korth and Sudarshan
Relational Operations in Datalog
Database System Concepts - 6th Edition c.51 ©Silberschatz, Korth and Sudarshan
Recursion in Datalog
Database System Concepts - 6th Edition c.52 ©Silberschatz, Korth and Sudarshan
Semantics of Recursion in Datalog
Database System Concepts - 6th Edition c.53 ©Silberschatz, Korth and Sudarshan
Example of Datalog-FixPoint Iteration
Database System Concepts - 6th Edition c.54 ©Silberschatz, Korth and Sudarshan
A More General View
Create a view relation empl that contains every tuple (X, Y ) such
that X is directly or indirectly managed by Y.
empl (X, Y ) :– manager (X, Y ).
empl (X, Y ) :– manager (X, Z ), empl (Z, Y )
Find the direct and indirect employees of Jones.
? empl (X, “Jones”).
Can define the view empl in another way too:
empl (X, Y ) :– manager (X, Y ).
empl (X, Y ) :– empl (X, Z ), manager (Z, Y ).
Database System Concepts - 6th Edition c.55 ©Silberschatz, Korth and Sudarshan
The Power of Recursion
Database System Concepts - 6th Edition c.56 ©Silberschatz, Korth and Sudarshan
Recursion in SQL
Database System Concepts - 6th Edition c.57 ©Silberschatz, Korth and Sudarshan
Monotonicity
Database System Concepts - 6th Edition c.58 ©Silberschatz, Korth and Sudarshan
Non-Monotonicity
Database System Concepts - 6th Edition c.59 ©Silberschatz, Korth and Sudarshan
Non-Monotonicity (Cont.)
Database System Concepts - 6th Edition c.60 ©Silberschatz, Korth and Sudarshan
End of Appendix C