CPSC-310 Database Systems

Slides:



Advertisements
Similar presentations
CS411 Database Systems Kazuhiro Minami 06: SQL. SQL = Structured Query Language Standard language for querying and manipulating data Has similar capabilities.
Advertisements

Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.
IS698: Database Management Min Song IS NJIT. Overview  Query processing  Query Optmization  SQL.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
SQL 2014, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Onsdag The concepts in a relation data model SQL DDL DML.
SQL 2015, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
Databases 1 Second lecture.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Introduction to SQL Introduction Select-From-Where Statements Queries over Several Relations Subqueries.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
1 Data Modification with SQL CREATE TABLE, INSERT, DELETE, UPDATE Slides from
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
Select-From-Where Statements Multirelation Queries Subqueries
CPSC-310 Database Systems
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
Computational Biology
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Databases : More about SQL
CPSC-310 Database Systems
Example: R3 := R1 × R2 R3( A, R1.B, R2.B, C )‏ R1( A, B )‏ R2( B, C )‏ R3( A, R1.B, R2.B, C )‏
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-608 Database Systems
06a: SQL-1 The Basics– Select-From-Where
CS 440 Database Management Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

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

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

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.

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

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

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

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)

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

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.

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.

Meaning of Single-Relation Query

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

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

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.

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

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

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

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

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

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

* In SELECT clauses

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

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

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

Renaming Attributes

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

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’

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

Expressions in SELECT Clauses

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

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;

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

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

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

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

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

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.

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

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>

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

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

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.

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

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.

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

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.

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.

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

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

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)

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)

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

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)

Surprising Example

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

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;

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

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

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!

Reason: 2-Valued Laws  3-Valued Laws

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.

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.

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

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.

Multirelation Queries

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

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.

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

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

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;

Formal Semantics

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

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

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

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

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

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.

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

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

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

Explicit Tuple-Variables

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

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.

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.

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.

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)

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;