Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-608 Database Systems

Similar presentations


Presentation on theme: "CPSC-608 Database Systems"— Presentation transcript:

1 CPSC-608 Database Systems
Fall 2018 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes 5

2 SQL: Structured Query language
a very-high-level language. * say “what to do” rather than “how to do it.” * avoid a lot of data-manipulation details needed in procedural languages like C or Java. Database management system figures out the “best” way to execute queries * called “query optimization” For both data definition and data manipulation.

3 Renaming Relations Rename tables (Oracle): ALTER TABLE old-table-name
RENAME TO new-table-name Rename table columns (Oracle): ALTER TABLE table-name RENAME COLUMN old-name TO new-name

4 Meaning of Single-Relation Query
Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause. Apply the extended projection indicated by the SELECT clause. SELECT attributes FROM tables WHERE conditions Step 3 Step 1 Step 2

5 Meaning of Single-Relation Query
Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause. Apply the extended projection indicated by the SELECT clause. SELECT attributes FROM tables WHERE conditions Step 3 Step 1 Step 2

6 NULL Values Tuples in SQL relations can have NULL as a value for one or more components. Meaning depends on context. Two common cases: Missing value : e.g., we know Joe’s Bar has some address, but we don’t know what it is. Inapplicable : e.g., the value of attribute spouse for an unmarried person.

7 Comparing NULL’s to Values
The logic of conditions in SQL is really 3- valued logic: TRUE, FALSE, UNKNOWN.

8 Comparing NULL’s to Values
The logic of conditions in SQL is really 3- valued logic: TRUE, FALSE, UNKNOWN. When any value is compared with NULL, the truth value is UNKNOWN.

9 Comparing NULL’s to Values
The logic of conditions in SQL is really 3- valued logic: TRUE, FALSE, UNKNOWN. When any value is compared with NULL, the truth value is UNKNOWN. But a query only produces a tuple in the answer if its truth value for the WHERE clause is TRUE (not FALSE or UNKNOWN).

10 Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = 1/2.

11 Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = 1/2. AND = MIN; OR = MAX, NOT(x) = 1  x.

12 Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = 1/2. AND = MIN; OR = MAX, NOT(x) = 1  x. Example: TRUE AND (FALSE OR NOT(UNKNOWN))

13 Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = 1/2. AND = MIN; OR = MAX, NOT(x) = 1  x. Example: TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (11/2)))

14 Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = 1/2. AND = MIN; OR = MAX, NOT(x) = 1  x. Example: TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (11/2))) = MIN(1, MAX(0, 1/2)

15 Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = 1/2. AND = MIN; OR = MAX, NOT(x) = 1  x. Example: TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (11/2))) = MIN(1, MAX(0, 1/2) = MIN(1, 1/2)

16 Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = 1/2. AND = MIN; OR = MAX, NOT(x) = 1  x. Example: TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (11/2))) = MIN(1, MAX(0, 1/2) = MIN(1, 1/2) = 1/2

17 Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = 1/2. AND = MIN; OR = MAX, NOT(x) = 1  x. Example: TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (11/2))) = MIN(1, MAX(0, 1/2) = MIN(1, 1/2) = 1/2 (= UNKNOWN)

18 Surprising Example

19 Surprising Example From the following Sells relation: Sells bar beer
price Joe’s Bar Bud NULL

20 Surprising Example From the following Sells relation: SELECT bar
beer price Joe’s Bar Bud NULL SELECT bar FROM Sells WHERE price < 2.00 OR price >= 2.00;

21 Surprising Example From the following Sells relation: SELECT bar
beer price Joe’s Bar Bud NULL SELECT bar FROM Sells WHERE price < 2.00 OR price >= 2.00; UNKNOWN UNKNOWN

22 Surprising Example From the following Sells relation: SELECT bar
beer price Joe’s Bar Bud NULL SELECT bar FROM Sells WHERE price < 2.00 OR price >= 2.00; UNKNOWN UNKNOWN UNKNOWN

23 Surprising Example From the following Sells relation: SELECT bar
beer price Joe’s Bar Bud NULL SELECT bar FROM Sells WHERE price < 2.00 OR price >= 2.00; UNKNOWN UNKNOWN UNKNOWN So Joe’s Bar will not get printed!

24 Reason: 2-Valued Laws  3-Valued Laws

25 Reason: 2-Valued Laws  3-Valued Laws
Some laws in 2-valued logic, like commutativity of AND:  AND  =  AND , also hold in 3-valued logic.

26 Reason: 2-Valued Laws  3-Valued Laws
Some laws in 2-valued logic, like commutativity of AND:  AND  =  AND , also hold in 3-valued logic. Not all, e.g., the law of the excluded middle:  OR NOT  = TRUE.

27 Reason: 2-Valued Laws  3-Valued Laws
Some laws in 2-valued logic, like commutativity of AND:  AND  =  AND , also hold in 3-valued logic. Not all, e.g., the law of the excluded middle:  OR NOT  = TRUE. Always holds in 2-valued logic

28 Reason: 2-Valued Laws  3-Valued Laws
Some laws in 2-valued logic, like commutativity of AND:  AND  =  AND , also hold in 3-valued logic. Not all, e.g., the law of the excluded middle:  OR NOT  = TRUE. Always holds in 2-valued logic In 3-valued logic, when  = UNKNOWN, the left side is MAX(1/2, (1 – 1/2)) = 1/2  1.

29 Multirelation Queries

30 Multirelation Queries
Interesting queries often combine data from more than one relation. We can address several relations in one query by listing them all in the FROM clause. Distinguish attributes of the same name by “<relation>.<attribute>”

31 Example Using relations Likes(drinker, beer) and Frequents(drinker, bar), find the beers liked by at least one person who frequents Joe’s Bar. SELECT beer FROM Likes, Frequents WHERE bar = ’Joe’’s Bar’ AND Frequents.drinker = Likes.drinker;

32 Formal Semantics

33 Formal Semantics Almost the same as for single-relation queries:
SELECT L FROM R1,R2,…,Rk WHERE C

34 Formal Semantics Almost the same as for single-relation queries:
Start with the product of all the relations R1, …, Rk in the FROM clause. SELECT L FROM R1,R2,…,Rk WHERE C R1 R2 …… Rk

35 Formal Semantics Almost the same as for single-relation queries:
Start with the product of all the relations R1, …, Rk in the FROM clause. Apply the selection condition C from the WHERE clause. SELECT L FROM R1,R2,…,Rk WHERE C σC R1 R2 …… Rk

36 Formal Semantics Almost the same as for single-relation queries:
Start with the product of all the relations R1, …, Rk in the FROM clause. Apply the selection condition C from the WHERE clause. Project onto the list L of attributes and expressions in the SELECT clause. SELECT L FROM R1,R2,…,Rk WHERE C πL σC R1 R2 …… Rk

37 Formal Semantics Almost the same as for single-relation queries:
Start with the product of all the relations R1, …, Rk in the FROM clause. Apply the selection condition C from the WHERE clause. Project onto the list L of attributes and expressions in the SELECT clause. SELECT L FROM R1,R2,…,Rk WHERE C πL σC R1 R2 …… Rk

38 Explicit Tuple-Variables

39 Explicit Tuple-Variables
Sometimes, a query needs to use two copies of the same relation.

40 Explicit Tuple-Variables
Sometimes, a query needs to use two copies of the same relation. Distinguish copies by following the relation name by the name of a tuple-variable, in the FROM clause.

41 Explicit Tuple-Variables
Sometimes, a query needs to use two copies of the same relation. Distinguish copies by following the relation name by the name of a tuple-variable, in the FROM clause. It’s always an option to rename relations this way, even when not essential.

42 Explicit Tuple-Variables
Sometimes, a query needs to use two copies of the same relation. Distinguish copies by following the relation name by the name of a tuple-variable, in the FROM clause. It’s always an option to rename relations this way, even when not essential. Example. From Beers(name, manf), find all pairs of beers by the same manufacturer.

43 Explicit Tuple-Variables
Sometimes, a query needs to use two copies of the same relation. Distinguish copies by following the relation name by the name of a tuple-variable, in the FROM clause. It’s always an option to rename relations this way, even when not essential. Example. From Beers(name, manf), find all pairs of beers by the same manufacturer. Do not produce pairs like (Bud, Bud). Do not produce the same pair twice like (Bud, Miller) and (Miller, Bud)

44 Explicit Tuple-Variables
Sometimes, a query needs to use two copies of the same relation. Distinguish copies by following the relation name by the name of a tuple-variable, in the FROM clause. It’s always an option to rename relations this way, even when not essential. Example. From Beers(name, manf), find all pairs of beers by the same manufacturer. Do not produce pairs like (Bud, Bud). Do not produce the same pair twice like (Bud, Miller) and (Miller, Bud) SELECT b1.name, b2.name FROM Beers b1, Beers b2 WHERE b1.manf = b2.manf AND b1.name < b2.name;

45 Subqueries A parenthesized SELECT-FROM- WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses.

46 Subqueries A parenthesized SELECT-FROM- WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses. Example: in place of a relation in the FROM clause, we can place another query, and then query its result.

47 Subqueries A parenthesized SELECT-FROM- WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses. Example: in place of a relation in the FROM clause, we can place another query, and then query its result. * Better use a tuple-variable to name tuples of the result.

48 Subqueries That Return One Tuple
If a subquery is guaranteed to produce one tuple, then the subquery can be used as a value. Usually, the tuple has one component. A run-time error occurs if there is no tuple or more than one tuple.

49 Query + Subquery Solution
Sells(bar, beer, price) Query + Subquery Solution SELECT bar FROM Sells WHERE beer = ’Miller’ AND price = (SELECT price WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’); The price at which Joe sells Bud

50 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite. IN-expressions can appear in WHERE clauses. The <relation> is often a subquery. Example. From Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes. SELECT * FROM Beers WHERE name IN (SELECT beer FROM Likes WHERE drinker = ’Fred’);

51 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name);


Download ppt "CPSC-608 Database Systems"

Similar presentations


Ads by Google