Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-310 Database Systems

Similar presentations


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

1 CPSC-310 Database Systems
Professor Jianer Chen Room 315C HRBB Lecture #12

2 SQL: Structured Query language
How does SQL manipulate (search in/read from/write to) tables?

3 SQL: Structured Query language
How does SQL manipulate (search in/read from/write to) tables? This is the job of a data manipulation language (DML). DML does not change database schema.

4 Select-From-Where Statements
SELECT attributes FROM tables WHERE conditions

5 Select-From-Where Statements
SELECT attributes FROM tables WHERE conditions input

6 Select-From-Where Statements
SELECT attributes FROM tables WHERE conditions output (as a table) input

7 Our Running Example All our SQL queries will be based on the following database schema. Underline indicates key attributes. Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar)

8 Example Using Beers(name, manf), what beers are made by Anheuser-Busch? SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

9 Example Using Beers(name, manf), what beers are made by Anheuser-Busch? SELECT name FROM Beers WHERE manf=’Anheuser-Busch’; Notice SQL uses single-quotes for strings. SQL is case-insensitive, except inside strings.

10 Result of Query name Bud Bud Lite Michelob ……
The answer is a relation with a single attribute, name, and tuples with the name of each beer by Anheuser-Busch, such as Bud.

11 Meaning of Single-Relation Query

12 Meaning of Single-Relation Query
Begin with the relation in the FROM clause.

13 Meaning of Single-Relation Query
Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause.

14 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.

15 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

16 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

17 Operational Semantics
Beers name manf Bud Anheuser-Busch SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

18 Operational Semantics
Beers name manf Bud Anheuser-Busch To implement this algorithm think of a tuple variable tv ranging over each tuple of the relation mentioned in the FROM Clause. tv SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

19 Operational Semantics
Beers name manf Bud Anheuser-Busch To implement this algorithm think of a tuple variable tv ranging over each tuple of the relation mentioned in the FROM Clause. Check if the “current” tuple satisfies the WHERE clause. tv Check if manf = Anheuser-Busch SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

20 Operational Semantics
Beers Include tv.name in the result name manf Bud Anheuser-Busch To implement this algorithm think of a tuple variable tv ranging over each tuple of the relation mentioned in the FROM Clause. Check if the “current” tuple satisfies the WHERE clause. If so, compute the attributes or expressions of the SELECT clause using the components of this tuple. tv Check if manf = Anheuser-Busch SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

21 * In SELECT clauses

22 * In SELECT clauses When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.”

23 * In SELECT clauses SELECT *
When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.” Example using Beers(name, manf): SELECT * FROM Beers WHERE manf = ’Anheuser-Busch’;

24 * In SELECT clauses SELECT *
When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.” Example using Beers(name, manf): SELECT * FROM Beers WHERE manf = ’Anheuser-Busch’; The output has the same schema as Beers: name manf Bud Anheuser-Busch Bud Lite Michelob ……

25 Renaming Attributes

26 Renaming Attributes you want the result to have different attribute names, use “AS <new name>” to rename an attribute.

27 Renaming Attributes SELECT name AS beer, manf
you want the result to have different attribute names, use “AS <new name>” to rename an attribute. Example using Beers(name, manf): SELECT name AS beer, manf FROM Beers WHERE manf = ’Anheuser-Busch’

28 Renaming Attributes SELECT name AS beer, manf
you want the result to have different attribute names, use “AS <new name>” to rename an attribute. Example using Beers(name, manf): SELECT name AS beer, manf FROM Beers WHERE manf = ’Anheuser-Busch’ The result is: beer manf Bud Anheuser-Busch Bud Lite Michelob ……

29 Expressions in SELECT Clauses

30 Expressions in SELECT Clauses
Any expression that makes sense can appear as an element of a SELECT clause.

31 Expressions in SELECT Clauses
Any expression that makes sense can appear as an element of a SELECT clause. Example: from Sells(bar, beer, price): SELECT bar, beer, price*114 AS priceInYen FROM Sells;

32 Expressions in SELECT Clauses
Any expression that makes sense can appear as an element of a SELECT clause. Example: from Sells(bar, beer, price): SELECT bar, beer, price*114 AS priceInYen FROM Sells; The result is bar beer priceInYen Joe’s Bud 285 Sue’s Miller 342 ……

33 Expressions in SELECT Clauses
We can also have a “constant expression” in the SELECT clause.

34 Expressions in SELECT Clauses
We can also have a “constant expression” in the SELECT clause. Example: from Likes(drinker, beer): SELECT drinker, ‘likes Bud’ AS whoLikesBud FROM Likes; WHERE beer = ‘Bud’;

35 Expressions in SELECT Clauses
We can also have a “constant expression” in the SELECT clause. Example: from Likes(drinker, beer): SELECT drinker, ‘likes Bud’ AS whoLikesBud FROM Likes; WHERE beer = ‘Bud’; The result is drinker whoLikesBud Sally likes Bud Fred ……

36 Complex Conditions in WHERE Clause
From Sells(bar, beer, price), find the price Joe’s Bar charges for Bud: SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’;

37 Complex Conditions in WHERE Clause
From Sells(bar, beer, price), find the price Joe’s Bar charges for Bud: SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’; Notice how we get a single-quote in strings.

38 Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to check match.

39 Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to check match. General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern>

40 Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to check match. General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern> Pattern is a quoted string with % = “any string”; _ = “any character.”

41 Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to check match. General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern> Pattern is a quoted string with % = “any string”; _ = “any character.” Example. From Drinkers(name, addr, phone), find the drinkers with exchange 555 SELECT name FROM Drinkers WHERE phone LIKE ’%555-_ _ _ _’;

42 NULL Values Tuples in SQL relations can have NULL as a value for one or morecomponents. 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.

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

44 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.

45 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).

46 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.

47 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.

48 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))

49 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)))

50 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)

51 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)

52 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

53 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)

54 Surprising Example

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

56 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;

57 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

58 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

59 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 Hoe’s Bar will not get printed!

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

61 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.

62 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.

63 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

64 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.

65 Multirelation Queries

66 Multirelation Queries
Interesting queries often combine data from more than one relation.

67 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.

68 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>”

69 Example Using relations Likes(drinker, beer) and Frequents(drinker, bar), find the beers liked by at least one person who frequents Joe’s Bar.

70 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;

71 Formal Semantics

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

73 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

74 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

75 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

76 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

77 Operational Semantics
Imagine one tuple-variable for each relation in the FROM clause. * These tuple-variables visit each combination of tuples, one from each relation.

78 Operational Semantics
Imagine one tuple-variable for each relation in the FROM clause. * These tuple-variables visit each combination of tuples, one from each relation. drinker bar Sally Joe’s drinker beer Sally Bud tv1 tv2 SELECT beer FROM Likes, Frequents WHERE bar = ’Joe’’s Bar’ AND Frequents.drinker = Likes.drinker; Frequents Likes

79 Operational Semantics
Imagine one tuple-variable for each relation in the FROM clause. * These tuple-variables visit each combination of tuples, one from each relation. If the tuple-variables are pointing to tuples that satisfy the WHERE clause, drinker bar Sally Joe’s drinker beer Sally Bud Check these are equal tv1 2 2 tv2 SELECT beer FROM Likes, Frequents WHERE bar = ’Joe’’s Bar’ AND Frequents.drinker = Likes.drinker; 1 Frequents check for Joe Likes

80 Operational Semantics
Imagine one tuple-variable for each relation in the FROM clause. * These tuple-variables visit each combination of tuples, one from each relation. If the tuple-variables are pointing to tuples that satisfy the WHERE clause, send these tuples to the SELECT clause. to output drinker bar Sally Joe’s drinker beer Sally Bud Check these are equal 3 tv1 2 2 tv2 SELECT beer FROM Likes, Frequents WHERE bar = ’Joe’’s Bar’ AND Frequents.drinker = Likes.drinker; 1 Frequents check for Joe Likes

81 Explicit Tuple-Variables

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

83 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.

84 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.

85 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.

86 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)

87 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;


Download ppt "CPSC-310 Database Systems"

Similar presentations


Ads by Google