# Domain Relational Calculus and Query-by-Example CS157a John Eagle.

## Presentation on theme: "Domain Relational Calculus and Query-by-Example CS157a John Eagle."— Presentation transcript:

Domain Relational Calculus and Query-by-Example CS157a John Eagle

Domain Relational Calculus A form of Relational Calculus which uses domain variables that take on values from an attributes domain, rather than values for an entire tuple. Serves as the theoretical basis of the QBE (Query-by Example) Language. Definition: An expression in Domain Calculus is of the form { | P(x 1, x 2, …, x n ) } where x 1, x 2, …, x n represents domain variables P represents a formula composed of atoms An Atom has the form ∈ r, where r is a relation on n attributes and x 1, x 2, …, x n are the domain variables or domain constraints. x Θ y, where x and y are domain variables and Θ is the comparison operator (, ≤, ≥, =, ≠ ). It is required that x and y have domains that can be compared by Θ. x Θc, where x is a domain variable, Θ is a comparison operator, and c is a constraint in the domain of attributes for which x is a domain variable.

We build up formulae from atoms by using the following rules: An Atom is a formula. If P 1 is a formula, then so are ¬ P 1 and (P 1 ). If P 1 and P 2 are formulae, then so are P 1 ⋁ P 2, P 1 ⋀ P 2, and P 1 ⇒ P 2. If P 1 (x) is a formula in x, where x is a free domain variable, then ∃ x (P 1 (x)) and ∀ x (P 1 (x)) Example of queries using Domain Relational Calculus: Find all loan numbers for loans with an amount greater than \$1200: { | ∃ a, b ( ∈ loan ⋀ a > 1200) } Equivalent Relational Algebra expression Π loan_number ( σ amount > 1200 (loan))

Result of the query

Query-by-Example (QBE) Developed at IBM in the early 1970’s QBE is both the name of the DML and an early database system that included this language. Has two distinct features: –QBE has a two dimensional syntax. Queries look like tables. –QBE queries are expressed “by example.” Instead of giving procedures for obtaining a desired answer, the user gives an example of what is desired. There are two flavors of QBE: –The original text based version –Graphical version (i.e. Microsoft Access, Borland Paradox) Skeleton Tables Queries in QBE are expressed by using skeleton tables. These tables show the relational schema of the database. Users select the skeleton for the tables needed to form a query and then fills in the skeletons with example rows. An example row consists of constants and example elements (domain variables) This query tells the system to look for tuples in loan that have “Perryridge” as the value for the branch_name attribute and display each corresponding loan_number.

Corresponding Domain Relational Calculus query { | ∃ b, a ( ∈ loan ⋀ b > “Perryridge") } QBE performs duplicate elimination automatically. To suppress this feature, insert ALL after the P. command. QBE assumes that the blank variable is has a unique value To display the entire loan relation insert P. in every field or place P. in the column headed by the relation name

QBE allows queries which involve arithmetic comparisons –For example: Find the loan numbers of all loans with a loan amount of more than \$700 Comparisons can only involve only one arithmetic expression on the right-hand side of the comparison operation, as seen in the previous example. The space on the left-hand side of the comparison operation must be blank. The expression can include both variables and constants. QBE support the following arithmetic operations: ¬, =,, ≤, ≥ –Example: Find the names of all branches that are not located in Brooklyn The primary purpose of variables in QBE is to force values of certain tuples to have the same value on certain attributes. –Example: Find the loan numbers of all loans made jointly to Smith and Jones. In domain relational calculus the query would be written as: { | ∃ x ( ∈ borrower ⋀ x = “Smith”) ⋀ ∃ x ( ∈ borrower ⋀ x = “Jones”)}

The QBE version of this query is: Find all customers who live in the same city as Jones. QBE allows queries that span several different relations (analogous to Cartesian product or natural join in relational algebra). Find the names of all customers who have a loan from the Perryridge branch.

At times it is either inconvenient or impossible to express all the constraints on the domain variables within the skeleton tables. QBE solves this problem by using a condition box. Logical expressions are used in the condition box. Example: Find the loan number of all loans made to Smith, to Jones, or to both jointly.

QBE in Microsoft Access Graphical version of query-by-example Attributes are arranged vertically instead of horizontally. Access uses a line to link attributes of two tables instead of a shared variable (as in the text version), to specify a join condition. Links between tables are created automatically based on the attribute name. Results of a query are displayed in the design grid. Example: Find the customer_name, account_number, and balance for all accounts at the Perryridge branch.

The Query: The Result:

Find the names of all customers who have a loan from the Perryridge branch, and find the loan amount. In domain relational calculus: { | ∃ l ( ∈ borrower ⋀ ∃ b ( ∈ loan ⋀ b = “Perryridge”)} In text based QBE: In relational algebra: Π customer_name, amount ( σ branch_name = “Perryridge” (loan borrower))

In Microsoft Access: