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

Similar documents
Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

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

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

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

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

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

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

SQL. Chapter 5 FROM WHERE

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

CIS 330: Applied Database Systems

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

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

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

SQL: Queries, Constraints, Triggers

SQL: Queries, Programming, Triggers

SQL: Queries, Constraints, Triggers (iii)

Enterprise Database Systems

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

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

The Relational Model of Data (ii)

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

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

Relational data model

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

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

Introduction to Data Management. Lecture 17 (SQL: There s STILL More...)

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

Database Applications (15-415)

The Relational Model 2. Week 3

Database Management Systems. Chapter 3 Part 1

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

Outer Join, More on SQL Constraints

Introduction to Data Management. Lecture #18 (SQL, the Final Chapter )

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

The Relational Data Model. Data Model

The Relational Model. Chapter 3

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

Keys, SQL, and Views CMPSCI 645

SQL: Queries, Programming, Triggers

The Relational Model. Week 2

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

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

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

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

Review: Where have we been?

Principles of Data Management. Lecture #12 (Query Optimization I)

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

Database Applications (15-415)

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

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

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

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

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

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

CompSci 516 Data Intensive Computing Systems

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

Database Applications (15-415)

SQL: The Query Language Part 1. Relational Query Languages

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

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Introduction to Data Management. Lecture #7 (Relational Design Theory)

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

Principles of Data Management. Lecture #13 (Query Optimization II)

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL

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

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

Introduction to Data Management CSE 344

Database Systems ( 資料庫系統 )

Lecture #8 (Still More Relational Theory...!)

Database Systems ( 資料庫系統 )

SQL DATA DEFINITION LANGUAGE

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

Overview of the Class and Introduction to DB schemas and queries. Lois Delcambre

SQL DATA DEFINITION LANGUAGE

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

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

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

Relational Query Optimization. Highlights of System R Optimizer

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

Introduction to Data Management. Lecture #4 E-R Model, Still Going

CSEN 501 CSEN501 - Databases I

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

Basic form of SQL Queries

Overview of Query Evaluation. Overview of Query Evaluation

Databases - Relational Algebra. (GF Royle, N Spadaccini ) Databases - Relational Algebra 1 / 24

This lecture. Projection. Relational Algebra. Suppose we have a relation

SQL DATA DEFINITION LANGUAGE

Relational Model and Relational Algebra

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

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

SQL - Lecture 3 (Aggregation, etc.)

The Relational Model

Introduction to Data Management CSE 344

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

SQL: Part II. Introduction to Databases CompSci 316 Fall 2018

This lecture. Step 1

Evaluation of Relational Operations. Relational Operations

CompSci 516 Data Intensive Computing Systems. Lecture 11. Query Optimization. Instructor: Sudeepa Roy

Transcription:

Introduction to Data Management Lecture 16 (SQL: There s STILL More...) Instructor: Mike Carey mjcarey@ics.uci.edu Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 It s time for another installment of... Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Announcements v HW and exams: HW #4 info Solution now available on the CS122a wiki page HW #5 info Underway, on-time deadline is Tuesday (5 pm) Midterm Exam 1 Available today! (6-7 pm if you missed 3-4 pm) v Next week s meetings Monday is a holiday! (No class, no discussion!) v Today s lecture plan: STILL more about SQL...! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Midterm #1 Statistics Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Nulls and SQL s 3-Valued Logic AND true false unknown true true false unknown false false false false unknown unknown false unknown OR true false unknown true true true true false true false unknown unknown true unknown unknown NOT true false unknown false true unknown Note: SQL arithmetic expressions involving null values will yield null values (Ex: EMP.sal + EMP.bonus) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 (Ex: Sailors w/some Null Values) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

Basic SQL Queries w/nulls SELECT * FROM Sailors S WHERE age > 35.0 SELECT * FROM Sailors S WHERE age <= 35.0 SELECT COUNT(*) FROM Sailors S WHERE age > 35.0 OR age <= 35.0 OR age IS NULL Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 (Ex: Sailors and Reserves w/nulls) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

Nulls w/aggregates SELECT COUNT(rating) FROM Sailors (11) SELECT COUNT (DISTINCT rating) FROM Sailors (7) SELECT SUM(rating), COUNT(rating), AVG(rating) FROM Sailors (70, 11, 6.3636) (Useful but logically wrong!) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Nulls w/aggregates & Grouping SELECT COUNT( DISTINCT bid) FROM Reserves (4) SELECT bid, COUNT(*) FROM Reserves GROUP BY bid Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Some dangling tuple examples Nulls w/joins à Inner/Outer Joins Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Inner vs. Outer Joins in SQL SELECT DISTINCT s.sname, r.date FROM Sailors s, Reserves r WHERE s.sid = r.sid Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

Inner vs. Outer Joins in SQL (2) SELECT DISTINCT s.sname, r.date FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Inner vs. Outer Joins in SQL (3) (1) SELECT DISTINCT s.sname, r.date FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid (2) SELECT DISTINCT s.sname, r.date FROM Reserves r RIGHT OUTER JOIN Sailors s ON s.sid = r.sid v Variations on a theme: JOIN (or INNER JOIN) LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN (Varies from RDBMS to RDBMS) (See: http://dev.mysql.com/doc/refman/5.7/en/ join.html for MySQL s join syntax) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

A Few Side Notes... v As a side note: The underlying operations are also part of the extended relational algebra, which adds... Outer joins Grouping Ordering... v You can actually play with those extensions on the relational algebra RelaX site that we used for the last HW assignment (if curious)! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Updates: Oh CRUD! (Create, Retrieve, Update, Delete) v Can add one or more tuples using INSERT: INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, Smith, smith@ee, 18, 3.2) INSERT INTO Students (sid, name, login, age, gpa) SELECT... (your favorite SQL query goes here!)... v Can DELETE all tuples satisfying any SQL query condition: DELETE FROM Students S WHERE S.sid IN (SELECT X.sid FROM Banned X) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

Updates: Oh CRUD! (Cont.) v Can change one or more tuples using UPDATE: UPDATE Sailors SET sname = Arthur, rating = rating + 1 WHERE sname = Art ; v A few things to note: LHS of SET is column name, RHS is (any) expression WHERE predicate is any SQL condition, which again means SQL subqueries are available as a tool, e.g., to search for targets based on multiple tables content Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 SQL Data Integrity (Largely Review) v An integrity constraint describes a condition that every legal instance of a relation must satisfy. Inserts/deletes/updates that violate IC s are disallowed. Can be used to ensure application semantics (e.g., sid is a key, bid refers to a known boat) or prevent inconsistencies (e.g., sname has to be a string, integer age must be < 120) v Types of IC s: Domain constraints, primary key constraints, foreign key constraints, unique constraints, general constraints. Domain constraints: Field values must be of the right type (i.e., per the schema specification). Always enforced! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18

SQL Data Integrity (Cont.) v So far we have been making good use of: PRIMARY KEY UNIQUE NOT NULL FOREIGN KEY Note: MySQL with InnoDB actually permits a foreign key to reference any indexed column(s). v Other features for ensuring field value integrity: DEFAULT (alternative to NULL for missing values) CHECK (called general in the book, kind of...) v More powerful integrity features include ASSERTION (called general in the book, correctly J) TRIGGER (a sledge hammer to use when all else fails!) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 Some Integrity Related Examples v CHECK useful when more general ICs than just keys are involved. v Could use SQL subqueries to express richer constraints (if supported J). v Constraints can be named (to manage them). CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL DEFAULT 18.0, PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10 ) ) CREATE TABLE Reserves ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CONSTRAINT nointerlakeres CHECK (`Interlake <> ( SELECT B.bname FROM Boats B WHERE B.bid=bid)) ) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20

Enforcing Referential Integrity (RI) v Consider Sailors and Reserves; sid in Sailors is a foreign key that references Reserves. v What should be done if a Reserves tuple with a nonexistent sailor id is inserted? (A: Reject it!) v What should be done if a Sailors tuple is deleted? Also delete all Reserves tuples that refer to it. Disallow deletion of a Sailors tuple that s being referred to. Set sid in Reserves tuples that refer to it to a default sid. (In SQL, also: Set sid in Reserves tuples that refer to it to null, denoting `unknown or `inapplicable.) v Similar if primary key of Sailors tuple is updated. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21 RI Enforcement in SQL (Reminder) v SQL/92 and SQL:1999 support all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected) CASCADE (also delete all tuples that refer to the deleted tuple) SET NULL / SET DEFAULT (set foreign key value of referencing tuple) Ex: CREATE TABLE Reserves (sid INTEGER, bid INTEGER, date DATE,... FOREIGN KEY (sid) REFERENCES Sailors ON DELETE CASCADE ON UPDATE SET NULL) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22

One more SQL lecture ahead... Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23