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 2017 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes 3

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 Creating (Declaring) a Relation
Simplest form is: CREATE TABLE <name> ( <list of elements> );

4 Creating (Declaring) a Relation
Simplest form is: CREATE TABLE <name> ( <list of elements> ); Example: CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) NOT NULL );

5 Changing Relation Schema: Adding Attributes
We may add a new attribute (“column”) to a relation schema by: ALTER TABLE <name> ADD <attribute declaration>;

6 Changing Relation Schema: Adding Attributes
We may add a new attribute (“column”) to a relation schema by: ALTER TABLE <name> ADD <attribute declaration>; Example: ALTER TABLE Bars ADD phone CHAR(16)DEFAULT ’unlisted’;

7 Changing Relation Schema: Deleting Attributes
Remove an attribute from a relation schema by: ALTER TABLE <name> DROP <attribute>;

8 Changing Relation Schema: Deleting Attributes
Remove an attribute from a relation schema by: ALTER TABLE <name> DROP <attribute>; Example: we don’t really need the license attribute for bars: ALTER TABLE Bars DROP license;

9 Changing Relation Schema: Removing Relations
To delete a relation: DROP TABLE <name>;

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

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

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

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

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

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

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

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

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

19 Meaning of Single-Relation Query

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

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

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

23 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

24 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

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

26 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’;

27 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’;

28 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’;

29 * in SELECT clauses

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

31 * 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’;

32 * 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 ……

33 Renaming Attributes

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

35 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’

36 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 ……

37 Expressions in SELECT Clauses

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

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

40 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 ……

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

42 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’;

43 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 ……

44 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’;

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

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

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

48 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.”

49 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-_ _ _ _’;

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

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

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

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

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

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

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

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

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

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

60 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

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

62 Surprising Example

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

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

65 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

66 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

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

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

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

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

71 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

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


Download ppt "CPSC-608 Database Systems"

Similar presentations


Ads by Google