Review: Where have we been?

Similar documents
SQL: The Query Language Part 1. Relational Query Languages

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Introduction to Data Management. Lecture #14 (Relational Languages IV)

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of...

The Database Language SQL (i)

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

SQL. Chapter 5 FROM WHERE

Keys, SQL, and Views CMPSCI 645

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

Basic form of SQL Queries

SQL: Queries, Constraints, Triggers

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

SQL: Queries, Programming, Triggers

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

Introduction to Data Management. Lecture 14 (SQL: the Saga Continues...)

CIS 330: Applied Database Systems

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

Database Applications (15-415)

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Enterprise Database Systems

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Relational data model

Database Systems. October 12, 2011 Lecture #5. Possessed Hands (U. of Tokyo)

Database Systems ( 資料庫系統 )

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

Database Applications (15-415)

Database Applications (15-415)

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

The Relational Data Model. Data Model

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

MIS Database Systems Relational Algebra

SQL: The Query Language (Part 1)

Database Management Systems. Chapter 3 Part 1

The Relational Model

Experimenting with bags (tables and query answers with duplicate rows):

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

The Relational Model. Chapter 3

The Relational Model

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model of Data (ii)

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

SQL - Lecture 3 (Aggregation, etc.)

Relational Query Languages

CompSci 516 Data Intensive Computing Systems

Data Science 100. Databases Part 2 (The SQL) Slides by: Joseph E. Gonzalez & Joseph Hellerstein,

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

Database Management Systems. Chapter 5

Midterm Exam #2 (Version A) CS 122A Winter 2017

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Introduction to Data Management. Lecture #10 (Relational Calculus, Continued)

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Relational Algebra 1

Data Science 100 Databases Part 2 (The SQL) Previously. How do you interact with a database? 2/22/18. Database Management Systems

QUERIES, PROGRAMMING, TRIGGERS

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1

SQL: Queries, Programming, Triggers

Relational Algebra. Chapter 4, Part A. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Relational Algebra 1

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

CompSci 516: Database Systems

Principles of Data Management. Lecture #9 (Query Processing Overview)

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Database Applications (15-415)

Relational Algebra. Relational Query Languages

CSEN 501 CSEN501 - Databases I

The Relational Model. Week 2

CSIT5300: Advanced Database Systems

CMPT 354: Database System I. Lecture 3. SQL Basics

Ø Last Thursday: String manipulation & Regular Expressions Ø guest lecture from the amazing Sam Lau Ø reviewed in section and in future labs & HWs

Advanced Database Management Systems

Advanced Database Management Systems

Database Management Systems. Chapter 5

Relational Calculus. Chapter Comp 521 Files and Databases Fall

CS 186, Fall 2002, Lecture 8 R&G, Chapter 4. Ronald Graham Elements of Ramsey Theory

The Relational Model 2. Week 3

CS330. Query Processing

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Overview of Query Evaluation. Overview of Query Evaluation

Relational Query Optimization

Database Systems. Announcement. December 13/14, 2006 Lecture #10. Assignment #4 is due next week.

Database Management System. Relational Algebra and operations

Evaluation of relational operations

Introduction to Data Management. Lecture #11 (Relational Algebra)

Database Systems ( 資料庫系統 )

CSCC43H: Introduction to Databases. Lecture 3

ATYPICAL RELATIONAL QUERY OPTIMIZER

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Database Systems. Course Administration. 10/13/2010 Lecture #4

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

Transcription:

SQL Basic

Review Query languages provide 2 key advantages: Less work for user asking query More opportunities for optimization Algebra and safe calculus are simple and powerful models for query languages for relational model Have same expressive power Algebra is more operational; calculus is more declarative SQL can express every query that is expressible in relational algebra/calculus. (and more)

Review: Where have we been? Theory Relational Calculus Lecture 6 Practice Query Optimization and Execution Relational Algebra Relational Model Lecture 5 Lectures 3 &4 Lecture 2 Relational Operators Files and Access Methods Buffer Management Disk Space Management DB

Where are we going next? SQL This week After the midterm Next week Practice Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB

Review: Relational Calculus Example Find names, ages and reservation dates of sailors rated > 7 who ve reserved boat #103 3 quantifiers, but only 1 is free. The free quantifier defines the shape of the result. {S1 S Sailors S.rating > 7 R(R Reserves R.bid = 103 R.sid = S.sid) (S1.sname = S.name S1.age = S.age S1.day = R.day)} S S S R R sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid bid day 22 101 10/10/96 58 103 11/12/96 sname age day S1 rusty 35.0 11/12/96

Review: The SQL Query Language The most widely used relational query language. Standardized (although most systems add their own special sauce -- including PostgreSQL) We will study SQL92 -- a basic subset

Review: SQL Two sublanguages: DDL Data Definition Language Define and modify schema (at all 3 levels) DML Data Manipulation Language Queries and IUD (insert update delete) DBMS is responsible for efficient evaluation. Relational completeness means we can define precise semantics for relational queries. Optimizer can re-order operations, without affecting query answer. Choices driven by cost model

Review: DDL CREATE TABLE Sailors (sid INTEGER, NOT NULL, sname CHAR(20), rating INTEGER, age REAL, PRIMARY KEY sid) CREATE TABLE Boats (bid INTEGER, bname CHAR NOT (20), NULL, color CHAR(10) PRIMARY KEY bid) CREATE TABLE Reserves (sid INTEGER, bid INTEGER, NOT NULL, day DATE, NOT NULL, PRIMARY KEY NOT (sid, NULL, bid, day), FOREIGN KEY sid REFERENCES Sailors, FOREIGN KEY bid REFERENCES Boats) Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria red Reserves sid bid day 1 102 9/12 2 102 9/13

Integrity Constraints (ICs) A foreign key constraint is an Integrity Constraint: a condition that must be true for any instance of the database; Specified when schema is defined. Checked when relations are modified. Primary/foreign key constraints; but databases support more general constraints as well. e.g. domain constraints like: Rating must be between 1 and 10 ALTER TABLE SAILORS ADD CONSTRAINT RATING CHECK (RATING >= 1 AND RATING < 10) Or even more complex (and potentially nonsensical): ALTER TABLE SAILORS ADD CONSTRAINT RATING CHECK (RATING*AGE/4 <= SID)

DBMSs have fairly sophisticated support for constraints! Specify them on CREATE or ALTER TABLE statements Column Constraints: expressions for column constraint must produce boolean results and reference the related column s value only. NOT NULL NULL UNIQUE PRIMARY KEY CHECK (expression) FOREIGN KEY (column) referenced_table [ ON DELETE action ] [ ON UPDATE action ] } action is one of: NO ACTION, CASCADE, SET NULL, SET DEFAULT

DBMSs have fairly sophisticated support for constraints! Table Constraints: UNIQUE ( column_name [,... ] ) PRIMARY KEY ( column_name [,... ] ) CHECK ( expression ) FOREIGN KEY ( column_name [,... ] ) REFERENCES reftable [ ON DELETE action ] [ ON UPDATE action ] } Here, expressions, keys, etc can include multiple columns

Integrity Constraints can help prevent data consistency errors but they have drawbacks: Expensive Can t always return a meaningful error back to the application. e.g: What if you saw this error when you enrolled in a course online? A violation of the constraint imposed by a unique index or a unique constraint occurred. Can be inconvenient e.g. What if the Sailing Class application wants to register new (unrated) sailors with rating 0? So they aren t widely used Software developers often prefer to keep the integrity logic in applications instead

Intermission

SQL DML DML includes 4 main statements: SELECT (query), INSERT, UPDATE and DELETE We ll spend a lot of time on this one e.g: To find the names of all 19 year old students: PROJECT SELECT S.name FROM Students S WHERE S.age=19 SELECT sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@ee 18 3.2 53650 Smith smith@math 19 3.8

Querying Multiple Relations Can specify a join over two tables as follows: SELECT S.name, E.cid PROJECT FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade= B' JOIN sid cid grade 53831 Carnatic101 C 53831 Reggae203 B 53650 Topology112 A 53666 History105 B SELECT sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@ee 18 3.2 result = S.name Jones E.cid History105

Basic SQL Query DISTINCT: optional keyword indicating answer should not contain duplicates. In SQL, default is that duplicates are not eliminated! (Result is called a multiset ) SELECT FROM WHERE target-list : A list of attributes of tables in relation-list [DISTINCT] target-list relation-list qualification qualification : Comparisons combined using AND, OR and NOT. Comparisons are Attr op const or Attr1 op Attr2, where op is one of,,,, etc. relation-list : A list of relation names, possibly with a rangevariable after each name

Query Semantics Semantics of an SQL query are defined in terms of the following conceptual evaluation strategy: 1. FROM clause: compute cross-product of all tables 2. WHERE clause: Check conditions, discard tuples that fail. (called selection ). 3. SELECT clause: Delete unwanted fields. (called projection ). 4.If DISTINCT specified, eliminate duplicate rows. Probably the least efficient way to compute a query! An optimizer will find more efficient strategies to get the same answer.

Query Semantics Example SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 Boats Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 bid bname color X 101 Nina red 102 Pinta blue 103 Santa Maria red Reserves sid bid day 1 102 9/12 2 103 9/13

Step 1: Compute the cross product Sailors Reserves sid sname rating age 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 SailorsXReserves... sid bid day 1 102 9/12 2 103 9/13 sid sname rating age sid bid day 1 Frodo 7 22 1 102 9/12 1 Frodo 7 22 2 103 9/13 2 Bilbo 2 39 1 102 9/12 2 Bilbo 2 39 2 103 9/13 3 Sam 8 27 1 103 9/12 3 Sam 8 27 2 103 9/13

Step 1: How big? Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 Reserves sid bid day 1 102 9/12 2 103 9/13 Question: If S is cardinality of Sailors, and R is cardinality of Reserves, What is the cardinality of Sailors X Reserves? Answer: S * R Sailors X Reserves = 3X2 = 6

Step 2: Check conditions in where clause SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 SailorsXReserves sid sname rating age sid bid day 1 Frodo 7 22 1 102 9/12 1 Frodo 7 22 2 103 9/13 2 Bilbo 2 39 1 102 9/12 2 Bilbo 2 39 2 103 9/13 3 Sam 8 27 1 102 9/12 3 Sam 8 27 2 103 9/13

Step 3: Delete unwanted fields SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 SailorsXReserves sid sname rating age sid bid day 1 Frodo 7 22 1 102 9/12 1 Frodo 7 22 2 103 9/13 2 Bilbo 2 39 1 102 9/12 2 Bilbo 2 39 2 103 9/13 3 Sam 8 27 1 102 9/12 3 Sam 8 27 2 103 9/13

Range Variables Used for short hand Needed when ambiguity could arise e.g two tables with the same column name: SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND Reserves.bid=103 SELECT sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 Question: do range variables remind you of anything? Variables in relational calculus

Sometimes you need a range variable e.g a Self-join: SELECT R1.bid, R1.date FROM Reserves R1, Reserves R2 WHERE R1.bid = R2.bid and R1.date = R2.date and R1.sid!= R2.sid Reserves Reserves bid day 103 9/12 103 9/12 R1 R1 R1 sid bid day 1 102 9/12 3 103 9/12 4 103 9/13 2 103 9/12 R2 R2 R2 R2 sid bid day 1 102 9/12 3 103 9/12 4 103 9/13 2 103 9/12

Sometimes you need a range variable SELECT R1.bid, R1.day FROM Reserves R1, Reserves R2 WHERE R1.bid = R2.bid and R1.day = R2.day and R1.sid!= R2.sid bid day 103 9/12 103 9/12 What are we computing? Boats reserved on the same day by different sailors

SELECT Clause Expressions Can use * if you want all columns: SELECT * FROM Sailors x WHERE x.age > 20 Can use arithmetic expressions (add other operations we ll discuss later) SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors S WHERE S.sname = Dustin Can use AS to provide column names SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors S1, Sailors S2 WHERE 2*S1.rating = S2.rating - 1

WHERE Clause Expressions Can also have expressions in WHERE clause: SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors S1, Sailors S2 WHERE 2*S1.rating = S2.rating - 1 LIKE is used for string matching. SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors S WHERE S.sname LIKE B_l%o `_ stands for any one character and `% stands for 0 or more arbitrary characters.

SELECT DISTINCT Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 Reserves sid bid day 1 102 9/12 2 103 9/12 2 102 9/13 SELECT DISTINCT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid Find sailors that have reserved at least one boat 2 sid 1

SELECT DISTINCT How about: SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid sid 1 2 2

SELECT DISTINCT How about: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid vs: SELECT DISTINCT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid sname Frodo Bilbo Bilbo sname Frodo Bilbo Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 4 Bilbo 5 32 Reserves sid bid day 1 102 9/12 2 103 9/13 4 105 9/13 Do we find all sailors that reserved at least one boat?

ANDs, ORs, UNIONs and INTERSECTs Find sids of sailors who ve reserved a red or a green boat SELECT R.sid FROM Boats B,Reserves R WHERE(B.color= red OR B.color= green ) AND R.bid=B.bid Sailors sid sname rating age X 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 sid 2 4 Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria red 105 Titanic green Reserves sid bid day 1 102 9/12 2 103 9/13 4 105 9/13

ANDs and ORs Find sids of sailors who ve reserved a red and a green boat Boats SELECT R.sid FROM Boats X B,Reserves R WHERE(B.color= red AND B.color= green ) AND R.bid=B.bid Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria red 105 Titanic green Reserves sid bid day 1 101 9/12 2 103 9/13 1 105 9/13

Use INTERSECT instead of AND SELECT R.sid FROM Boats B,Reserves R WHERE B.color = red AND R.bid=B.bid INTERSECT SELECT R.sid FROM Boats B,Reserves R WHERE B.color = green AND R.bid=B.bid sid 1 2 sid 1 = sid 1 Exercise: try to rewrite this query using a self join instead of INTERSECT! Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria red 105 Titanic green Reserves sid bid day 1 101 9/12 2 103 9/13 1 105 9/13

Could also use UNION for the OR query SELECT R.sid FROM Boats B, Reserves R WHERE B.color = red AND R.bid=B.bid UNION SELECT R.sid FROM Boats B, Reserves R WHERE B.color = green AND R.bid=B.bid sid 2 sid 4 = sid 2 4 Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria red 105 Titanic green Reserves sid bid day 1 102 9/12 2 103 9/13 4 105 9/13

EXCEPT: Set Difference Find sids of sailors who have not reserved a boat SELECT S.sid FROM Sailors S EXCEPT SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid First find the set of sailors who have reserved a boat and then compare it with the rest of the sailors Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 2 39 3 Sam 8 27 Reserves sid bid day 1 102 9/12 2 103 9/13 1 105 9/13 sid 3