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

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

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

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Announcements (September 21) SQL: Part III. Triggers. Active data. Trigger options. Trigger example

Relational Model & Algebra. Announcements (Thu. Aug. 27) Relational data model. CPS 116 Introduction to Database Systems

EECS 647: Introduction to Database Systems

CSEN 501 CSEN501 - Databases I

What s a database system? Review of Basic Database Concepts. Entity-relationship (E/R) diagram. Two important questions. Physical data independence

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

Relational Model & Algebra. Announcements (Tue. Sep. 3) Relational data model. CompSci 316 Introduction to Database Systems

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes

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

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

The Relational Model of Data (ii)

The Relational Data Model. Data Model

Relational data model

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

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

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 16 (SQL: There s STILL More...) It s time for another installment of...

The Relational Model. Week 2

The Relational Model 2. Week 3

Database Applications (15-415)

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

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

Announcements (January 20) Relational Database Design. Database (schema) design. Entity-relationship (E/R) model. ODL (Object Definition Language)

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

The Relational Model. Chapter 3

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

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

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

Plan for today. Query Processing/Optimization. Parsing. A query s trip through the DBMS. Validation. Logical plan

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

Database Management Systems. Chapter 3 Part 1

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

Database Systems ( 資料庫系統 )

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

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

SQL: Queries, Constraints, Triggers (iii)

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

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

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

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

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

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

EECS 647: Introduction to Database Systems

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

CS143: Relational Model

Outer Join, More on SQL Constraints

SQL: Programming Midterm in class next Thursday (October 5)

CSE 344 Midterm. Wednesday, February 19, 2014, 14:30-15:20. Question Points Score Total: 100

Constraints. Local and Global Constraints Triggers

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

Relational Database Design. Announcements. Database (schema) design. CPS 216 Advanced Database Systems. DB2 accounts have been set up

SQL DATA DEFINITION LANGUAGE

Midterm Review. Winter Lecture 13

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

The Relational Model

SQL: Queries, Constraints, Triggers

SQL DATA DEFINITION LANGUAGE

Query Processing: A Systems View. Announcements (March 1) Physical (execution) plan. CPS 216 Advanced Database Systems

A tuple is dangling if it doesn't join with any

CS W Introduction to Databases Spring Computer Science Department Columbia University

Announcements (March 1) Query Processing: A Systems View. Physical (execution) plan. Announcements (March 3) Physical plan execution

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

SQL DATA DEFINITION LANGUAGE

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

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

CSE 444, Winter 2011, Midterm Examination 9 February 2011

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

The Relational Model

NULL. The special value NULL could mean: Unknown Unavailable Not Applicable

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Relational Database Design Part I. Announcement. Relational model: review. CPS 116 Introduction to Database Systems

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

SQL Constraints and Triggers. Week 11

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Basic operators: selection, projection, cross product, union, difference,

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

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

SQL. Chapter 5 FROM WHERE

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Introduction to Data Management CSE 344

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

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

Principles of Data Management

Introduction to Database Systems CSE 414

Lecture 2: Introduction to SQL

Chapter 6 The database Language SQL as a tutorial

Databases. Jörg Endrullis. VU University Amsterdam

CSE 344 Midterm. Wednesday, February 19, 2014, 14:30-15:20. Question Points Score Total: 100

Announcements. SQL: Part IV. Transactions. Summary of SQL features covered so far. Fine prints. SQL transactions. Reading assignments for this week

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

Transcription:

SQL: Part II CPS 116 Introduction to Database Systems Announcements (September 18) 2 Homework #1 due today (11:59pm) Submit in class, slide underneath my office door Sample solution available Thursday Homework #2 assigned today Due next Thursday Project milestone #1 due in 3½ weeks Incomplete information 3 Example: Student (SID, name, age, GPA) Value unknown We do not know Nelson s age W do o own so s g Value not applicable Nelson has not taken any classes yet; what is his GPA? 1

Solution 1 4 A dedicated special value for each domain (type) GPA cannot be 1, so use 1 as a special value to indicate a missing or invalid GPA Leads to incorrect answers if not careful SELECT AVG(GPA) A) FROM Student; Complicates applications SELECT AVG(GPA) FROM Student WHERE GPA <> -1; Remember the Y2K bug? 00 was used as a missing or invalid year value Solution 2 5 A valid-bit for every column Student (SID, name, name_is_valid, age, age_is_valid, GPA, GPA_is_valid) Complicates schema and queries SELECT AVG(GPA) FROM Student WHERE GPA_is_valid; Solution 3? 6 Decompose the table; missing row = missing value StudentName (SID, name) StudentAge (SID, age) StudentGPA (SID, GPA) StudentID (SID) Conceptually the cleanest solution Still complicates schema and queries 2

SQL s solution 7 A special value NULL For every domain Special rules for dealing with NULL s Example: Student (SID, name, age, GPA) h 789, Nelson, NULL, NULL i Computing with NULL s 8 When we operate on a NULL and another value (including another NULL) using +,, etc., the result is NULL Aggregate functions ignore NULL, except COUNT(*) (since it counts rows) Three-valued logic 9 TRUE = 1, FALSE = 0, UNKNOWN = 0.5 x AND y = min(x, y) x OR y = max(x, y) NOT x = 1 x When we compare a NULL with another value (including another NULL) using =, >, etc., the result is UNKNOWN WHERE and HAVING clauses only select rows for output if the condition evaluates to TRUE UNKNOWN is not enough 3

Unfortunate consequences 10 SELECT AVG(GPA) FROM Student; SELECT SUM(GPA)/COUNT(*) FROM Student; SELECT * FROM Student; SELECT * FROM Student WHERE GPA = GPA; Be careful: NULL breaks many equivalences Another problem 11 Example: Who has NULL GPA values? SELECT * FROM Student WHERE GPA = NULL; Introduced built-in predicates IS NULL and IS NOT NULL SELECT * FROM Student WHERE GPA IS NULL; Outerjoin motivation 12 Example: a master class list SELECT c.cid, c.title, s.sid, s.name FROM Course c, Enroll e, Student s WHERE c.cid = e.cid AND e.sid = s.sid; What if a class is empty? It may be reasonable for the master class list to include empty classes as well For these classes, SID and name columns would be NULL 4

Outerjoin flavors and definitions 13 A full outerjoin between R and S (denoted R & S) includes all rows in the result of R! S, plus Dangling R rows (those that do not join with any S rows) padded with NULL s for S s columns Dangling S rows (those that do not join with any R rows) padded with NULL s for R s columns A left outerjoin (R ' S) includes rows in R! S plus dangling R rows padded with NULL s A right outerjoin (R ( S) includes rows in R! S plus dangling S rows padded with NULL s Outerjoin examples Course CID title CPS199 Independent Study CPS130 Analysis of Algorithms CPS116 Intro. to Database Systems Enroll SID CID 142 CPS116 142 CPS114 123 CPS114 857 CPS116 857 CPS130 456 CPS116 Course ' Enroll Course ( Enroll Course & Enroll CID title SID 14 CPS199 Independent Study NULL CPS130 Analysis of Algorithms 857 CPS116 Intro. to Database Systems 142 CPS116 Intro. to Database Systems 857 CPS116 Intro. to Database Systems 456 CID title SID CPS116 Intro. to Database Systems 142 CPS114 NULL 142 CPS114 NULL 123 CPS116 Intro. to Database Systems 857 CPS130 Analysis of Algorithms 857 CPS116 Intro. to Database Systems 456 CID title SID CPS199 Independent Study NULL CPS116 Intro. to Database Systems 142 CPS114 NULL 142 CPS114 NULL 123 CPS116 Intro. to Database Systems 857 CPS130 Analysis of Algorithms 857 CPS116 Intro. to Database Systems 456 Outerjoin syntax 15 SELECT * FROM Course LEFT OUTER JOIN Enroll ON Course.CID = Enroll.CID; SELECT * FROM Course RIGHT OUTER JOIN Enroll ON Course.CID = Enroll.CID; SELECT * FROM Course FULL OUTER JOIN Enroll ON Course.CID CID = Enroll.CID; These are theta joins rather than natural joins Return all columns in Course and Enroll Equivalent to Course ' Course.CID = Enroll.CID Enroll, Course ( Course.CID = Enroll.CID Enroll, and Course & Course.CID = Enroll.CID Enroll You can write regular ( inner ) joins using this syntax too: SELECT * FROM Course JOIN Enroll ON Course.CID = Enroll.CID; 5

16 Summary of SQL features covered so far SELECT-FROM-WHERE statements Set and bag operations Table expressions, subqueries Aggregation and grouping Ordering NULL s and outerjoins Next: data modification statements, constraints INSERT 17 Insert one row INSERT INTO Enroll VALUES (456, CPS116 ); Student 456 takes CPS116 Insert the result of a query INSERT INTO Enroll (SELECT SID, CPS116 FROM Student WHERE SID NOT IN (SELECT SID FROM Enroll WHERE CID = CPS116 )); Force everybody to take CPS116 DELETE 18 Delete everything DELETE FROM Enroll; Delete according to a WHERE condition Example: Student 456 drops CPS116 DELETE FROM Enroll WHERE SID = 456 AND CID = CPS116 ; Example: Drop students from all CPS classes with GPA lower than 1.0 DELETE FROM Enroll WHERE SID IN (SELECT SID FROM Student WHERE GPA < 1.0) AND CID LIKE CPS% ; 6

UPDATE 19 Example: Student 142 changes name to Barney UPDATE Student SET name = Barney WHERE SID = 142; Example: Let s be fair? UPDATE Student SET GPA = (SELECT AVG(GPA) FROM Student); But update of every row causes average GPA to change! Average GPA is computed over the old Student table Constraints 20 Restrictions on allowable data in a database In addition to the simple structure and type restrictions imposed by the table definitions Declared as part of the schema Enforced by the DBMS Why use constraints? Protect data integrity (catch errors) Tell the DBMS about the data (so it can optimize better) Types of SQL constraints 21 NOT NULL Key Referential integrity (foreign key) General assertion Tuple- and attribute-based CHECK s 7

NOT NULL constraint examples 22 CREATE TABLE Student (SID INTEGER NOT NULL, name VARCHAR(30) NOT NULL, email VARCHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Course (CID CHAR(10) NOT NULL, title VARCHAR(100) NOT NULL); CREATE TABLE Enroll (SID INTEGER NOT NULL, CID CHAR(10) NOT NULL); Key declaration 23 At most one PRIMARY KEY per table Typically implies a primary index Rows are stored inside the index, typically sorted by the primary key value best speedup for queries Any number of UNIQUE keys per table Typically implies a secondary index Pointers to rows are stored inside the index less speedup for queries Key declaration examples CREATE TABLE Student (SID INTEGER NOT NULL PRIMARY KEY, name VARCHAR(30) NOT NULL, email VARCHAR(30) UNIQUE, age INTEGER, GPA FLOAT); CREATE TABLE Course (CID CHAR(10) NOT NULL PRIMARY KEY, title VARCHAR(100) NOT NULL); CREATE TABLE Enroll (SID INTEGER NOT NULL, CID CHAR(10) NOT NULL, PRIMARY KEY(SID, CID)); 24 Doesn t work on DB2: DB2 requires UNIQUE key columns to be NOT NULL 8

Referential integrity example 25 Enroll.SID references Student.SID If an SID appears in Enroll, it must appear in Student Enroll.CID references Course.CID If a CID appears in Enroll, it must appear in Course That is, no dangling pointers Student Enroll Course SID name age GPA SID CID CID title 142 Bart 10 2.3 142 CPS116 CPS116 Intro. to Database Systems 123 Milhouse 10 3.1 142 CPS114 CPS130 Analysis of Algorithms 857 Lisa 8 4.3 123 CPS116 CPS114 Computer Networks 456 Ralph 8 2.3 857 CPS116 857 CPS130 456 CPS114 Referential integrity in SQL 26 Referenced column(s) must be PRIMARY KEY Referencing column(s) form a FOREIGN KEY Example CREATE TABLE Enroll (SID INTEGER NOT NULL REFERENCES Student(SID), CID CHAR(10) NOT NULL, PRIMARY KEY(SID, CID), FOREIGN KEY CID REFERENCES Course(CID)); Enforcing referential integrity 27 Example: Enroll.SID references Student.SID Insert or update an Enroll row so it refers to a nonexistent SID Reject 9

Deferred constraint checking 28 No-chicken-no-egg problem CREATE TABLE Dept (name CHAR(20) NOT NULL PRIMARY KEY, chair CHAR(30) NOT NULL REFERENCES Prof(name)); CREATE TABLE Prof (name CHAR(30) NOT NULL PRIMARY KEY, dept CHAR(20) NOT NULL REFERENCES Dept(name)); General assertion 29 CREATE ASSERTION assertion_name CHECK assertion_condition; assertion_condition is checked for each modification that could potentially violate it Example: Enroll.SID references Student.SID In SQL3, but not all (perhaps no) DBMS supports it Tuple- and attribute-based CHECK s 30 Associated with a single table Only checked when a tuple or an attribute is inserted or updated Example: CREATE TABLE Enroll (SID INTEGER NOT NULL CHECK (SID IN (SELECT SID FROM Student)), CID...); Is it a referential integrity constraint? 10

31 Summary of SQL features covered so far Query SELECT-FROM-WHERE statements Set and bag operations Table expressions, subqueries Aggregation and grouping Ordering Outerjoins Modification INSERT/DELETE/UPDATE Constraints Next: triggers, views, indexes 11