CPSC-608 Database Systems

Slides:



Advertisements
Similar presentations
SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
Advertisements

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.
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.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
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.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
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 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
CPSC-310 Database Systems
Computational Biology
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
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
Database Design and Programming
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
CMSC-461 Database Management Systems
CPSC-608 Database Systems
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-608 Database Systems Fall 2018 Instructor: Jianer Chen Office: HRBB 315C Phone: 845-4259 Email: chen@cse.tamu.edu Notes 5

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.

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

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

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

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.

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 Joe’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. 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. 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

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;

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

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.

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.

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.

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

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

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