CH 5
CH 5
Query-by-Example (QBE)
Datalog
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
Method 1:
Find the loan number of all loans with a loan amount of more than $700
Find the names of all customers who have both an account and
a loan at the bank.
Find all branches that have assets greater than those of at least
one branch located in Brooklyn
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.
E.g. Find the total balance of all the accounts maintained at
the Perryridge branch.
Delete all loans with a loan amount between $1300 and $1500.
For consistency, we have to delete information from loan and
borrower tables
Basic Structure
Syntax of Datalog Rules
Semantics of Nonrecursive Datalog
Safety
Relational Operations in Datalog
Recursion in Datalog
The Power of Recursion
head body
each of the Li’s is a literal
head – the literal p(t1, t2, ..., tn)
body – the rest of the literals
A fact is a rule with an empty body, written in the form:
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
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
it is not in layers 1, 2, ..., i
all relations used in the bodies of rules defining a p are either stored
in the database, or are in layers 1, 2, ..., i
? empl(X, “Jones”).
Can define the view empl in another way too: