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

Similar documents
SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. 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

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

CSEN 501 CSEN501 - Databases I

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

EECS 647: Introduction to Database Systems

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

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)

The Relational Model of Data (ii)

The Relational Data Model. Data Model

The Relational Model. Week 2

The Relational Model 2. Week 3

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

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

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

Database Applications (15-415)

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

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

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

The Relational Model. Chapter 3

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

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

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

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

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

Database Management Systems. Chapter 3 Part 1

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

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

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

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

Database Systems ( 資料庫系統 )

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

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

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

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

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

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

SQL: Queries, Constraints, Triggers (iii)

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

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

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

The Relational Model

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

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

Constraints. Local and Global Constraints Triggers

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

The Relational Model

SQL: Queries, Constraints, Triggers

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

Outer Join, More on SQL Constraints

SQL DATA DEFINITION LANGUAGE

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

EECS 647: Introduction to Database Systems

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

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

SQL DATA DEFINITION LANGUAGE

CS W Introduction to Databases Spring Computer Science Department Columbia University

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

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

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

CS143: Relational Model

SQL DATA DEFINITION LANGUAGE

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

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

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

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

Database Systems. Course Administration

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

SQL. Chapter 5 FROM WHERE

Midterm Review. Winter Lecture 13

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

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

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

SQL Constraints and Triggers. Week 11

Principles of Data Management

Keys, SQL, and Views CMPSCI 645

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

Introduction to Data Management CSE 344

Integrity Constraints (Reminder)

Databases. Jörg Endrullis. VU University Amsterdam

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)

Introduction to Database Systems CSE 414

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

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

CSC 453 Database Technologies. Tanu Malik DePaul University

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

Lecture 2: Introduction to SQL

Chapter 6 The database Language SQL as a tutorial

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

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

COMP 244 DATABASE CONCEPTS & APPLICATIONS

Transcription:

Announcements (September 18) 2 SQL: Part II Homework #1 due today (11:59pm) Submit in class, slide underneath my office door Sample solution available Thursday Homework #2 assigned today CPS 116 Introduction to Database Systems Due next Thursday Project milestone #1 due in 3½ weeks Incomplete information 3 Solution 1 4 Example: Student (SID, name, age, GPA) Value unknown We do not know ownelson s sage Value not applicable Nelson has not taken any classes yet; what is his GPA? 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 Solution 3? 6 A valid-bit for every column Decompose the table; missing row = missing value Student (SID, name, name_is_valid, age, age_is_valid, GPA, GPA_is_valid) StudentName (SID, name) StudentAge (SID, age) StudentGPA (SID, GPA) Complicates schema and queries SELECT AVG(GPA) FROM Student WHERE GPA_is_valid; StudentID (SID) Conceptually the cleanest solution Still complicates schema and queries How to get all information about a student in a table? Would natural join work? 1

SQL s solution 7 Computing with NULL s 8 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 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 Unfortunate consequences 10 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 SELECT AVG(GPA) FROM Student; SELECT SUM(GPA)/COUNT(*) FROM Student; Not equivalent Although AVG(GPA) = SUM(GPA)/COUNT(GPA) still SELECT * FROM Student; SELECT * FROM Student WHERE GPA = GPA; Not equivalent Be careful: NULL breaks many equivalences Another problem Example: Who has NULL GPA values? SELECT * FROM Student WHERE GPA = NULL; Does not work; never returns anything (SELECT * FROM Student) EXCEPT ALL (SELECT * FROM Student WHERE GPA = GPA) Works, but ugly Introduced built-in predicates IS NULL and IS NOT NULL SELECT * FROM Student WHERE GPA IS NULL; 11 Outerjoin motivation 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 12 2

Outerjoin flavors and definitions 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 13 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 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; 15 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 DELETE 18 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 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% ; 3

UPDATE 19 Constraints 20 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 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 constraint examples 22 NOT NULL Key Referential integrity (foreign key) General assertion Tuple- and attribute-based CHECK s 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 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 23 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 This form is required for multi-attribute keys 4

Referential integrity example 25 Referential integrity in SQL 26 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 142 Bart 10 2.3 123 Milhouse 10 3.1 857 Lisa 8 4.3 456 Ralph 8 2.3 SID CID 142 CPS116 142 CPS114 123 CPS116 857 CPS116 CID title CPS116 Intro. to Database Systems CPS130 Analysis of Algorithms CPS114 Computer Networks 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)); 857 CPS130 456 CPS114 Enforcing referential integrity 27 Deferred constraint checking 28 Example: Enroll.SID references Student.SID Insert or update an Enroll row so it refers to a nonexistent SID Reject Delete or update a Student row whose SID is referenced by some Enroll row Reject Cascade: ripple changes to all referring rows Set NULL: set all references to NULL All three options can be specified in SQL 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)); The first INSERT will always violate a constraint! Deferred constraint checking is necessary Check only at the end of a transaction Allowed in SQL as an option Curious how the schema was created in the first place? ALTER TABLE ADD CONSTRAINT (read the manual!) General assertion 29 Tuple- and attribute-based CHECK s 30 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 CREATE ASSERTION EnrollStudentRefIntegrity CHECK (NOT EXISTS (SELECT * FROM Enroll WHERE SID NOT IN (SELECT SID FROM Student))); In SQL3, but not all (perhaps no) DBMS supports it 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? Not quite; not checked when Student is modified 5

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 6