Download presentation
Presentation is loading. Please wait.
Published bySydney Malcolm Lewis Modified over 6 years ago
1
CS 480: Database Systems Lecture 16 February 20,2013
2
Question from last time
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? { < d > | c ( < d,c > drinker a,e ( ( < a,e > serves e = ‘Bud’ ) < d,a > frequents ) ) } I don’t need the c…
3
Question from last time
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? What should be returned if no bar serves ‘Bud’? { < d > | c ( < d,c > drinker a,e ( ( < a,e > serves e = ‘Bud’ ) < d,a > frequents ) ) } I don’t need the c…
4
What will the SQL query return?
{ < d > | c ( < d,c > drinker a,e ( ( < a,e > serves e = ‘Bud’ ) < d,a > frequents ) ) } SELECT d.d_name FROM drinker AS d WHERE ((SELECT f.ba_name FROM frequents AS f WHERE f.d_name=d.d_name) CONTAINS (SELECT s.ba_name FROM serves AS s WHERE s.be_name=‘Bud’))
5
Safety of Expressions { t | (t r) } Consider the formula:
Valid formula according to our definition. However, there are infinitely many tuples that are not in r and satisfy the query.
6
Safety of Expressions { t | (t r) } Consider the formula:
Valid formula according to our definition. However, there are infinitely many tuples that are not in r and satisfy the query.
7
Safety of Expressions { t | (t r) } Consider the formula:
Valid formula according to our definition. However, there are infinitely many tuples that are not in r and satisfy the query. Maybe think of a system that allows such a query and has to answer such a query. it would be impossible to answer.
8
Safety of Expressions We do not want to allow these queries with infinitely many tuples that satisfy the expression. Our objective is to restrict the search for answers to queries, to the database. QUESTION: For which relational calculus queries, is the answer for a given database independent of the domain? The database is finite, so then if we don’t need to search outside of it, we’re probably fine…
9
Domain of a Relational Formula
Domain of a formula P, denoted by dom(P) is the set of all values referenced by P. dom(P) includes: Values mentioned in P itself (constants) Values that appear in a relation that appears in P. Then dom(P) is the set of all values that appear explicitly in P or that appear in one or more relations whose names appear in P.
10
Domain of a Relational Formula
P1 = x > 5 dom(P1) = {5} P2 = y student (y[id] = x[id]) dom(P2) is the set containing any value that appears in the student relation P3 = t instructor t [salary] = 80000 dom(P3) is the set that contains any value that appears in the instructor relation as well as 80000 P4 = (t instructor) dom(P4) is the set that contains all values that appear in the instructor relation.
11
If tuple t satisfies P(t), then t dom(P).
Safety of Expressions The expression { t | P(t) } is safe if it satisfies the following condition: If tuple t satisfies P(t), then t dom(P). The number of tuples that satisfy an unsafe expression could be infinite. Safe expressions are guaranteed to have finite results and have to search for the answer to the query in a finite set.
12
Safety of Expressions P4 = (t instructor)
dom(P4) is the set that contains all values that appear in the instructor relation. Unsafe because there are tuples that satisfy P4 that have values not in the dom(P4).
13
Safety of Expressions P5 = { < a,b > | c ( < a,b,c > r a > 5 ) } P6 = { < a,b > | c ( < a,b,c > r Which one of these is safe?
14
Safety of Expressions P5 = { < a,b > | c ( < a,b,c > r a > 5 ) } P6 = { < a,b > | c ( < a,b,c > r P5 Is unsafe, the OR makes it so that by the second condition (a > 5) it could return almost anything. Which one of these is safe?
15
QBE – Query-by-Example
16
Query-By-Example (QBE)
Developed in the early 1970’s at IBM Still supported in various database systems (most notably MS Access) Early example of end-user development A graphical query language which is based (roughly) on the domain relational calculus Two dimensional syntax – system creates templates of relations that are requested by users Queries are expressed “by example”
17
QBE Skeleton Tables for a Bank Example
BRANCH(branch_name,branch_city,assets) CUSTOMER(customer_name,customer_street,customer_city) LOAN(loan_number,branch_name,amount)
18
QBE Skeleton Tables BRANCH(branch_name, branch_city,assets)
CUSTOMER(customer_name, customer_street,customer_city) LOAN(loan_number, branch_name,amount) BORROWER(customer_name, loan_number) ACCOUNT(account_number, branch_name,balance) DEPOSITOR(customer_name, account_number)
19
Queries on One Relation
Find all loan numbers at the Perryridge branch. P._x _x is a variable (optional; can be omitted in above query) P. means print (display) duplicates are removed by default To retain duplicates use P.ALL
20
Queries on One Relation
Display full details of all loans Method 1: P._x P._y P._z Method 2: Shorthand notation
21
Queries on One Relation
Find the loan number of all loans with a loan amount of more than $700 Available comparison operators are: =, <, , >, ,
22
Queries on One Relation
Find the loan number of all loans with a loan amount of more than $700 Find names of all branches that are not located in Brooklyn Available comparison operators are: =, <, , >, ,
23
Queries on One Relation
Find the loan number of all loans with a loan amount of more than $700 Find names of all branches that are not located in Brooklyn Available comparison operators are: =, <, , >, ,
24
Queries on One Relation
Find the loan numbers of all loans made jointly to Smith and Jones. BORROWER ( cust_name, loan_number )
25
Queries on One Relation
Find the loan numbers of all loans made jointly to Smith and Jones.
26
Queries on One Relation
Find the loan numbers of all loans made jointly to Smith and Jones. Find all customers who live in the same city as Jones. CUSTOMER ( customer_name, customer_street, customer_city )
27
Queries on One Relation
Find the loan numbers of all loans made jointly to Smith and Jones. Find all customers who live in the same city as Jones.
28
From QBE to DRC Generally, QBE queries match existential DRC queries.
{ < x > | c1,c2 ( < c1,x > borrower < c2,x > borrower c1 = ‘Smith’ c2 = ‘Jones’) }
29
From QBE to DRC Generally, QBE queries match existential DRC queries.
{ < x > | n,s1,s2,y ( < x,s1,y > customer < n,s2,y > customer n = ‘Jones’ ) }
30
Queries on Several Relations
Find the names of all customers who have a loan from the Perryridge branch.
31
Queries on Several Relations
Find the names of all customers who have a loan from the Perryridge branch.
32
Queries on Several Relations
Find the names of all customers who have a loan from the Perryridge branch.
33
Queries on Several Relations
Find the names of all customers who have both an account and a loan at the bank.
34
¬ means “there does not exist” when used in the first column.
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. ¬ means “there does not exist” when used in the first column.
35
¬ means “not equal to” when used under an attribute
Negation in QBE Find all customers who have at least two accounts. ¬ means “not equal to” when used under an attribute
36
The Condition Box Allows the expression of constraints on domain variables that are either inconvenient or impossible to express within the skeleton tables. Complex conditions can be used in condition boxes Example: Find the loan numbers of all loans made to Smith, to Jones, or to both jointly Could have been done w/o the condition box by having two variable names x1 and x2.
37
Condition Box QBE supports an interesting syntax for expressing alternative values. Shorthand for: _x = Brooklyn OR _x = Quenns
38
Condition Box Find all account numbers with a balance greater than $1,300 and less than $1,500.
39
Condition Box Find all account numbers with a balance greater than $1,300 and less than $1,500.
40
Condition Box 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.
41
Condition Box 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.
42
Condition Box Find all branches that have assets greater than those of at least one branch located in Brooklyn.
43
Condition Box Find all branches that have assets greater than those of at least one branch located in Brooklyn.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.