34
Query-by-Example (QBE) Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

  • View
    218

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Query-by-Example (QBE)Query-by-Example (QBE)

Presented by: Wong, Lok Man

CS 157A

Dr. Sin Min Lee

Page 2: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

History of QBEHistory of QBE

History:• QBE is the name of both a data-manipulation

language and an early database system that included this language

• QBE was developed at IBM’s T.J. Watson Research Center (1970’s)

Page 3: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

ContentContent

• QBE Basic Structure• Queries on One Relation• Queries on Several Relations• Condition Box• Result Relation• Ordering of the Display of Tuples• Aggregate Operations• Modification of Database• QBE in Microsoft Access

Page 4: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Basic StructureBasic Structure• QBE has two dimensional syntax:

1. Queries look like tables

2. QBE queries are expressed “by example”• Non-procedural• Queries are expressed using skeleton tables

branch Branch-name Branch-city assets

Page 5: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Basic StructureBasic Structure

• User selects skeletons needed• User fills in the skeletons with example row• Example row consists of constants & example elements,

which are domain variables• Constants appear without any qualification• QBE uses an underscore character “_” before domain

variables, as in “_x”

Page 6: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Queries on One RelationQueries on One Relation

Ex: Find all loan numbers at the Perryridge branchBring up the skeleton of the loan relation

P. – before the variable to display the value

X – a variable

Perryridge – branch-name

Loan loan-number branch-name amount

P._x Perryridge

Page 7: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Queries on One Relation (cont.)Queries on One Relation (cont.)

Ex: Suppress duplicate elimination - insert “ALL”

Ex: display entire loan relation – place single P.

loan loan-number branch-name amount

P.ALL Perryridge

loan loan-number branch-name amount

P.

Page 8: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Queries on One Relation (cont.)Queries on One Relation (cont.)

Ex: Find loan numbers of all loans with amount more than $700. Use arithmetic operations (=, <, >, ¬) With no variable, displays all attributes

Ex: Find names of all branches not in Brooklyn

loan loan-number branch-name amount

P. >700

branch branch-name branch-city assets

P. ¬Brooklyn

Page 9: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Queries on One Relation (cont.)Queries on One Relation (cont.)

Ex: Find the loan numbers of all loans made jointly to Smith and Jones. Only one P. is needed.

Ex: Find all customers who live in the same city as Jones

customer customer-name customer-street customer-city

P._x _y

Jones _y

borrower Customer-name loan-number

“Smith” P._x

“Jones” _x

Page 10: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Queries on Several Relations Queries on Several Relations

• QBE allows queries to span several different relations

• The connection among the various relations are achieved through variables that force certain tuples to have the same value on certain attributes

Page 11: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Queries on Several Relation (cont.)Queries on Several Relation (cont.)

Ex: Find the names of all the customers that have a loan from the Perryridge branch

loan loan-number branch-name amount

_X Perryridge

borrower customer-name loan-number

P._y _X

Page 12: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Queries on Several Relation (Cont.)Queries on Several Relation (Cont.)

Ex: Find the names of all customer with both and an account and a loan at the bank

The “¬” symbol represents “there does not exist”.A “¬” under an attribute name stands for “not

equal”.

depositor customer-name account-number

P._x

borrower customer-name loan-number

¬ _x

Page 13: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Queries on Several Relation (Cont.)Queries on Several Relation (Cont.)

Ex: Display all customer-name values that appear in at least two tuples, with the second tuple having an account-number different from the first.

depositor customer-name account-number

P._x _y

_x ¬_y

Page 14: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Condition BoxCondition Box• QBE includes a condition box feature that allow the

expression of general constraints over

any of the domain variables.• Logical operators in words and and or, or as symbols “&”

and “|”, appear in a condition box

Ex: Find the loan numbers of all loans made to Smith, to Jones, or both

borrower customer-name Loan-number

_n P._x

condition

_n = Smith or _n = Jones

Page 15: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Condition Box (Cont.)Condition Box (Cont.)

Ex: Find all branches that have assets greater than those of at least one branch located in Brooklyn

branch branch-name branch-city assets

P._x _y

Brooklyn _z

conditions

_y > _z

Page 16: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Condition Box (Cont.)Condition Box (Cont.)

Ex: Find all account numbers of account with a balance between $1000 and $1500, but not exactly $1255

account account-number branch-name balance

P._y _x

conditions

_x = ( > 999 and < 1501 and ¬ 1255)

Page 17: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Result RelationResult Relation

• If the result of a query includes attributes from several relation schemas, we can declare a temporary result relation that includes all the attributes of the result.

Ex: Create a skeleton table, called result, with attributes customer-name, account-number, and balance, at the Perryridge branch. (Two Steps)

1. account account-number branch-name balance

_y Perryridge _z

depositor customer-name account-number

_x _y

Page 18: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Result Relation (Cont.)Result Relation (Cont.)

Once the new skeleton table is created, write the query.

2.

result customer-name

account-number

balance

result customer-name

account-number

balance

P. _x _y _z

Page 19: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Ordering of the Display TuplesOrdering of the Display Tuples

• To gain control over the order in which tuples in a relation are displayed, indicate AO (ascending order) or Do (descending order).

• To specify the order in which the sorting should be carried, add an integer surrounded by parentheses after sort operator (AO(1) or DO(2)).

Page 20: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Ordering of the Display Tuples (Cont.)Ordering of the Display Tuples (Cont.)

Ex: List all account numbers at the Downtown branch in ascending alphabetic order with their respective account balances in descending order.

account account-number branch-name balance

P.AO(1) Downtown P.DO(2)

Page 21: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Aggregate OperationsAggregate Operations

• Aggregate Operators include AVG, MIN, MAX, SUM, and CNT

• We must postfix these operators with “ALL.” to create a multiset on which the aggregate operation is evaluated

• Operator “UNQ.” is to specify that duplicates are to be eliminated

• To compute functions on groups of tuples, then use “G.”.

Page 22: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Aggregate Operations (Cont.)Aggregate Operations (Cont.)

Ex: Find the total balance of all the accounts maintained at the Downtown branch

Ex: Find the total number of customers who have an account at the bank

account account-number branch-name balance

Downtown P.SUM.ALL.

depositor customer-name account-number

P.CNT.UNQ.

Page 23: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Aggregate Operations (Cont.)Aggregate Operations (Cont.)

Ex: Find the average balance at each branch

account account-number branch-name balance

P.G. P.AVG.ALL._x

Page 24: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Ex: Find all customers who have accounts at each of the branches located in Brooklyn

depositor customer-name account-number

P.G._X _y

account account-number branch-name balance

_y _z

branch branch-name branch-city assets

_z Brooklyn

_w Brooklyn

conditions

CNT.UNQ._z = CNT.UNQ._w

Page 25: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Modification of the DatabaseModification of the Database

• Deletion, uses “D.” replacing “P.”

• Insertion, uses “I.” replacing “P.”

• Updates, uses “U.” replacing “P.”

Page 26: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

DeletionDeletion

Ex: Delete customer Roy

Ex: Delete the branch-city value of the branch whose name is “San Jose”

depositor customer-name account-number

D. Roy

branch branch-name branch-city assets

San Jose D.

Page 27: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Deletion (Cont.)Deletion (Cont.)

Ex: Delete all accounts at all branches located in Brooklyn

depositor customer-name account-number

D. _y

branch branch-name branch-city assets

_x Brooklyn

account account-number branch-name balance

D. _y _x

Page 28: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

InsertionInsertion

Ex: Insert the fact that account AM-8976 at the Downtown branch has a balance of $900.

Ex: Insert information into the branch relation about a new branch with name “Capital”, city “Queens,” and asset “200,000”

account account-number branch-name balance

I. AM-8976 Downtown 900

branch branch-name branch-city assets

I. Capital Queens 200,000

Page 29: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Insertion (Cont.)Insertion (Cont.)

Ex: Insert as a gift, for all loan customers of the Downtown branch, a new $300 savings account for every loan account that they have, with the loan number serving as the account number for the savings account.

Page 30: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

Insertion (Cont.)Insertion (Cont.)account account-number branch-name balance

I. _x Downtown 300

depositor customer-name account-number

I. _y _x

loan loan-number branch-name amount

_x Downtown

borrower customer-name Loan-number

_y _x

Page 31: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

UpdatesUpdatesEx: Update the asset value of the San Jose branch

to $100,000,000.

Ex: Update all balances with a 5% increase.

Use “_x” to hold the old balances

branch branch-name branch-city assets

San Jose U.100,000,000

account account-number branch-name balance

U._x * 1.05

Page 32: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

QBE in Microsoft AccessQBE in Microsoft Access

• Original QBE was designed for a text-based display environment

• Access QBE is a graphical display environment, called graphical query-by-example (GQBE)

• GQBE version attributes are written one below the other, instead of horizontally

• Instead of shared variable in QBE, GQBE uses lines to link attributes of two tables

Page 33: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

QBE in Microsoft Access (Cont.)QBE in Microsoft Access (Cont.)

• In GQBE links are automatically created, on the basis of the attribute name

• In GQBE attributes printed in a separate box is specified, called the design grid, instead of using “P.” for QBE.

Page 34: Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

THE END!!!THE END!!!

THANKS FOR STAYING AWAKE!

QUIZ TIME!!!