Chapter 5: Other Relational Languages
Tuple Relational Calculus Domain Relational Calculus Query-by-Example (QBE)
Tuple Relational Calculus A nonprocedural query language, where each query is of {t | P (t ) } the form It is the set of all tuples t such that predicate P is true for
2. Set of comparison operators: (e.g., , , , , , )
Predicate Calculus Formula
Banking Example branch (branch_name, branch_city, assets ) customer (customer_name,
Example Queries Find the loan_number, branch_name, and amount for loans of over $1200 { t | t loan t [amount ] 1200}
Find the names of all customers having a loan, an account, or both at the bank Example Queries
{ t | s borrower ( t [customer_name ] = s [customer_name ])
Find the names of all customers having a loan at the Perryridge branch Example Queries
{ t | s borrower (t [customer_name ] = s [customer_name ]
Example Queries Find the names of all customers having a loan from the
Perryridge branch, and the cities in which they live
{ t | s loan (s [branch_name ] = “Perryridge” Find the names of all customers who have an account at all branches located in Brooklyn: Example Queries
{ t | r customer (t [customer_name ] = r [customer_name ])
Safety of Expressions It is possible to write tuple calculus expressions that For example, { t | t r } results in an infnite generate infnite relations. relation if the domain of any attribute of relation r is
Domain Relational Calculus
A nonprocedural query language equivalent in power to the tuple relational calculus
Find the loan_number, branch_name, and amount for
loans of over $1200 Example Queries{ l, b, a | l, b, a loan a > 1200}
Example Queries both at the Perryridge branch: Find the names of all customers having a loan, an account, or
{ | c l ( c, l borrower
Safety of Expressions The expression: { x , x , …, x | P (x , x , …, x )}
1 2 n 1 2 n
Basic Structure Queries on One Relation Queries on Several Relations
Query-by-Example (QBE)
QBE — Basic Structure
A graphical query language which is
based (roughly) on the domain relational calculusQBE Skeleton Tables for the Bank Example
QBE Skeleton Tables
(Cont.)Queries on One Relation Find all loan numbers at the Perryridge branch.
Display full details of all loans
Queries on One Relation
(Cont.) Method 1:
Queries on One Relation (Cont.) Find the loan number of all loans with a loan amount of more than $700
Find the loan numbers of all loans made jointly to Smith and Jones.
Queries on One Relation
(Cont.) Find the names of all customers who have a loan from the Perryridge branch.
Queries on Several Relations
Queries on Several Relations (Cont.)
account and a loan at the bank.Find the names of all customers who have both an
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.
Find all customers who have at least two accounts.
Negation in QBE (Cont.)
The Condition Box
Allows the expression of constraints on domain
to express within the skeleton tables. variables that are either inconvenient or impossible Complex conditions can be used in condition boxesCondition Box (Cont.) QBE supports an interesting syntax for expressing alternative values
Find all account numbers with a balance greater than $1,300 and less than $1,500 Condition Box (Cont.)
Condition Box (Cont.) Find all branches that have assets greater than those of at least one branch located in Brooklyn
Find the customer_name, account_number, and balance for all customers who have an account at the Perryridge branch.
The Result Relation
The resulting query is:
The Result Relation (Cont.)
Ordering the Display of Tuples have an account at the bank Example: list in ascending alphabetical order all customers who AO = ascending order; DO = descending order.
Aggregate Operations The aggregate operators are AVG, MAX, MIN, The above operators must be postfxed with SUM, and CNT
Aggregate Operations (Cont.) Find the total number of customers having an account at UNQ is used to specify that we want to eliminate duplicates the bank.
Query Examples
Find the average balance at each branch.
Query Example Find all customers who have an account at all branches located in Brooklyn.
◦ Approach: for each customer, fnd the number of
branches in Brooklyn at which they have accounts, andQuery Example (Cont.)
Modifcation of the Database – Deletion
of a D. command. In the case where we delete Deletion of tuples from a relation is expressed by use specifed by –, are inserted. information in only some of the columns, null values, 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 Deletion Query Examples
Deletion Query Examples (Cont.) Delete all accounts at branches located in Brooklyn.
Modifcation of the Database – Insertion
the query expression.Insertion is done by placing the I. operator in
Insert the fact that account A-9732 at theModifcation of the Database – Insertion (Cont.)
number serving as the account number for the new savings account.
$200 savings account for every loan account they have, with the loan
Provide as a gift for all loan customers of the Perryridge branch, a new
Modifcation of the Database – Updates
without changing all values in the tuple. QBE does Use the U. operator to change a value in a tuple not allow users to update the primary key felds.Microsoft Access QBE Microsoft Access supports a variant of QBE called GQBE difers from QBE in the following ways Graphical Query By Example (GQBE)
An Example Query in Microsoft Access QBE for all accounts at the Perryridge branch Example query: Find the customer_name, account_number and balance
An Aggregation Query in Access QBE
account at the bank Find the name, street and city of all customers who have more than one
Aggregation in Access QBE
The row labeled Total specifes ◦which attributes are group by attributes
◦ which attributes are to be aggregated upon
Basic Structure Syntax of Datalog Rules Semantics of Nonrecursive Datalog
Datalog
Basic Structure Prolog-like logic-based language that allows recursive
A Datalog program consists of a set of rules that
queries; based on frst-order logic. defne views.Example Queries Each rule defnes a set of tuples that a view relation must contain. ◦ E.g. v1 (A, B ) :– account (A, “ Perryridge”, B ), B > 700 is read as
Negation in Datalog Defne a view relation c that contains the names of all customers who have a deposit but no loan at the bank:
Named Attribute Notation Datalog rules use a positional notation that is convenient for relations with a small number of attributes
Formal Syntax and Semantics of Datalog
(meaning) of Datalog programs, in the following We formally defne the syntax and semantics steps 1. We defne the syntax of predicates, and then the syntaxSyntax of Datalog Rules A positive literal has the form p (t , t ..., t ) ◦ ◦ each t is either a constant or variable p is the name of a relation with n attributes 1 2 n i
Syntax of Datalog Rules (Cont.) p (t Rules are built out of literals and have the form: 1 , t 2 , ..., t n ) :– L 1 , L 2 , ..., L m .
Semantics of a Rule A ground instantiation of a rule (or simply variable in the rule by some constant.
instantiation ) is the result of replacing each
◦ Eg. Rule defning v1Semantics of a Rule (Cont.) We defne the set of facts that can be inferred from a given set of facts l using rule R as: 1 n infer(R, l) = { p (t , ..., t ) | there is a ground
Layering of Rules Defne the interest on each account in Perryridge interest(A, l) :– perryridge_account (A,B), perryridge_account(A,B) :– account (A, “Perryridge”, B). interest_rate(A,R), l = B * R/100.
Layering Rules (Cont.) Formally: bodies of rules defning it are stored in the A relation is a layer 1 if all relations used in the
Semantics of a Program Let the layers in a given program be 1, 2, ..., n. Let denote the set of all rules defining view relations in layer i. i Defne I = set of facts stored in the database.
It is possible to write rules that generate an infnite number of answers. gt(X, Y) :– X > Y not_in_loan (B, L) :– not loan (B, L)
Safety
Relational Operations in Datalog account. Project out attribute account_name from query (A) :–account (A, N, B ).
Recursion in Datalog Suppose we are given a relation containing pairs of names X, Y such that Y is a manager (X, Y ) manager of X (or equivalently, X is a direct employee
Semantics of Recursion in Datalog literals
Assumption (for now): program contains no negative
of rules are defned to contain exactly the set of facts l The view relations of a recursive program containing a setExample of Datalog-FixPoint Iteration
A More General View Create a view relation empl that contains indirectly managed by Y. every tuple (X, Y ) such that X is directly or
Recursive views make it possible to write queries, such as transitive closure queries, that cannot be written without
The Power of Recursion
Recursion in SQL Starting with SQL:1999, SQL permits recursive E.g. query to fnd all employee-manager pairs view defnition
Monotonicity A view V is said to be monotonic if given any two sets of the expression used to defne V. I facts 1 and I 2 such that l 1 I 2 , then E (I v v v 1 ) E (I 2 ), where E is
Procedure Datalog-Fixpoint is sound provided the rules in the program are monotonic.
◦ Otherwise, it may make some inferences in an
Non-MonotonicityNon-Monotonicity (Cont.) There are useful queries that cannot be expressed by a stratifed program ◦ Example: given information about the number of
Figure 5.1
Figure 5.2
Figure 5.5
Figure 5.6
Figure 5.9
Figure in-5.2
Figure in-5.15
Figure in-5.18
Figure in-5-31
Figure in-5.36