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

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. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

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

SQL: Triggers, Views, Indexes. Introduction to Databases CompSci 316 Fall 2018

Relational Model and Algebra. Introduction to Databases CompSci 316 Fall 2018

Relational Database Design Theory. Introduction to Databases CompSci 316 Fall 2017

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

Query Optimization. Introduction to Databases CompSci 316 Fall 2018

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

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

Constraints. Local and Global Constraints Triggers

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

CS W Introduction to Databases Spring Computer Science Department Columbia University

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

Principles of Data Management

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 System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

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

SQL DATA DEFINITION LANGUAGE

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

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

SQL DATA DEFINITION LANGUAGE

Midterm Review. Winter Lecture 13

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

The Relational Model of Data (ii)

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

Lecture 6a Design Theory and Normalization 2/2

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

SQL DATA DEFINITION LANGUAGE

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

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

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

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

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

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

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

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

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

SQL: Queries, Constraints, Triggers (iii)

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Introduction to Database Systems CSE 414

Chapter 7: Constraints and Triggers. Foreign Keys Local and Global Constraints Triggers

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

Databases 1. Defining Tables, Constraints

Relational Database Design: Part I. Introduction to Databases CompSci 316 Fall 2017

Chapter 3: Introduction to SQL

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Constraints and Triggers

Relational data model

The Relational Model. Week 2

SQL: Data Definition Language

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

Integrity and Security

Full file at

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

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Constraints, Views & Indexes. Running Example. Kinds of Constraints INTEGRITY CONSTRAINTS. Keys Foreign key or referential integrity constraints

Chapter 1 SQL and Data

Relational Model, Key Constraints

Querying Data with Transact SQL

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Databases. Jörg Endrullis. VU University Amsterdam

Relational Algebra and SQL

SQL: Queries, Constraints, Triggers

Outer Join, More on SQL Constraints

Slides by: Ms. Shree Jaswal

Missing Information. We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios:

Physical Data Organization. Introduction to Databases CompSci 316 Fall 2018

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

QQ Group

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

The SQL data-definition language (DDL) allows defining :

Database Applications (15-415)

An Introduction to Structured Query Language

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

An Introduction to Structured Query Language

An Introduction to Structured Query Language

Advanced Constraints SQL. by Joe Celko copyright 2007

Chapter 3: Introduction to SQL

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

Interpretation is not exactly ëmissing value." There could be many reasons why no value is

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

An Introduction to Structured Query Language

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

CIS Slightly-different version of Week 10 Lab, also intro to SQL UPDATE and DELETE, and more

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Chapter 4: Intermediate SQL

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Lecture 6 - More SQL

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

E-R diagrams and database schemas. Functional dependencies. Definition (tuple, attribute, value). A tuple has the form

The Relational Model 2. Week 3

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

Textbook: Chapter 4. Chapter 5: Intermediate SQL. CS425 Fall 2016 Boris Glavic. Chapter 5: Intermediate SQL. View Definition.

Basic SQL. Basic SQL. Basic SQL

Transcription:

SQL: Part II Introduction to Databases CompSci 316 Fall 2018

2 Announcements (Thu., Sep. 20) Homework #1 sample solution to be posted on Sakai by this weekend Homework #2 due in 1½ weeks Get started on your project! For ideas, check out project handout and examples from previous years (in intro lecture slides), and talk to us Project mixer next Tuesday Send me your elevator pitch slides by Monday midnight See email for details

3 Project resources Working web dev examples in PHP, Flask, and Play/Java for course VM See Help on course website for more details Duke Co-Lab offerings Many interesting Roots courses Build Your First iphone or ipad App, Making Your Website Interactive, Intro to React.js, Introduction to Linux, etc. Advance registration required Office hours on full-stack web/app development

4 Special announcements Applications for DTech Scholars are open HackDuke is coming!

5 Incomplete information Example: User (uid, name, age, pop) Value unknown We do not know Nelson s age Value not applicable Suppose pop is based on interactions with others on our social networking site Nelson is new to our site; what is his pop?

6 Solution 1 Dedicate a value from each domain (type) pop cannot be 1, so use 1 as a special value to indicate a missing or invalid pop Leads to incorrect answers if not careful SELECT AVG(pop) FROM User; Complicates applications SELECT AVG(pop) FROM User WHERE pop <> -1; Perhaps the value is not as special as you think! Ever heard of the Y2K bug? 00 was used as a missing or invalid year value http://www.90s411.com/images/y2k-cartoon.jpg

7 Solution 2 A valid-bit for every column User (uid, name, name_is_valid, age, age_is_valid, pop, pop_is_valid) Complicates schema and queries SELECT AVG(pop) FROM User WHERE pop_is_valid;

8 Solution 3 Decompose the table; missing row = missing value UserName (uid, name) UserAge (uid, age) UserPop (uid, pop) UserID (uid) Conceptually the cleanest solution Still complicates schema and queries How to get all information about users in a table?

9 SQL s solution A special value NULL For every domain Special rules for dealing with NULL s Example: User (uid, name, age, pop) 789, Nelson, NULL, NULL

10 Computing with NULL s 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)

11 Three-valued logic TRUE = 1, FALSE = 0, UNKNOWN = 0.5! AND " = min(!, ")! OR " = max(!, ") NOT! = 1! 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

12 Unfortunate consequences SELECT AVG(pop) FROM User; SELECT SUM(pop)/COUNT(*) FROM User; Although AVG(pop)=SUM(pop)/COUNT(pop) still SELECT * FROM User; SELECT * FROM User WHERE pop = pop; FBe careful: NULL breaks many equivalences

13 Another problem Example: Who has NULL pop values? SELECT * FROM User WHERE pop = NULL; Does not work (SELECT * FROM User) EXCEPT ALL (SELECT * FROM User WHERE pop = pop); Works, but ugly SQL introduced special, built-in predicates IS NULL and IS NOT NULL SELECT * FROM User WHERE pop IS NULL;

14 Outerjoin motivation Example: a master group membership list SELECT g.gid, g.name AS gname, u.uid, u.name AS uname FROM Group g, Member m, User u WHERE g.gid = m.gid AND m.uid = u.uid; What if a group is empty? It may be reasonable for the master list to include empty groups as well For these groups, uid and uname columns would be NULL

15 Outerjoin flavors and definitions A full outerjoin between R and S (denoted! #) includes all rows in the result of! #, plus Dangling! rows (those that do not join with any # rows) padded with NULL s for # s columns Dangling # rows (those that do not join with any! rows) padded with NULL s for! s columns A left outerjoin (! #) includes rows in! # plus dangling! rows padded with NULL s A right outerjoin (! #) includes rows in! # plus dangling # rows padded with NULL s

16 Outerjoin examples Group Member Group gid name abc Book Club gov Student Government dps Dead Putting Society nuk United Nuclear Workers Member uid gid 142 dps 123 gov 857 abc 857 gov 789 foo Group Member Group Member gid name uid abc Book Club 857 gov Student Government 123 gov Student Government 857 dps Dead Putting Society 142 nuk United Nuclear Workers NULL gid name uid abc Book Club 857 gov Student Government 123 gov Student Government 857 dps Dead Putting Society 142 foo NULL 789 gid name uid abc Book Club 857 gov Student Government 123 gov Student Government 857 dps Dead Putting Society 142 nuk United Nuclear Workers NULL foo NULL 789

17 Outerjoin syntax SELECT * FROM Group LEFT OUTER JOIN Member ON Group.gid = Member.gid; "#$%& 67897# ()*+,../0123453)../0 SELECT * FROM Group RIGHT OUTER JOIN Member ON Group.gid = Member.gid; "#$%& 67897# ()*+,../0123453)../0 SELECT * FROM Group FULL OUTER JOIN Member ON Group.gid = Member.gid; "#$%& 67897# ()*+,../0123453)../0 A similar construct exists for regular ( inner ) joins: SELECT * FROM Group JOIN Member ON Group.gid = Member.gid; These are theta joins rather than natural joins Return all columns in Group and Member For natural joins, add keyword NATURAL; don t use ON

18 SQL features covered so far SELECT-FROM-WHERE statements Set and bag operations Table expressions, subqueries Aggregation and grouping Ordering NULL s and outerjoins FNext: data modification statements, constraints

19 INSERT Insert one row INSERT INTO Member VALUES (789, 'dps'); User 789 joins Dead Putting Society Insert the result of a query INSERT INTO Member (SELECT uid, 'dps' FROM User WHERE uid NOT IN (SELECT uid FROM Member WHERE gid = 'dps')); Everybody joins Dead Putting Society!

20 DELETE Delete everything from a table DELETE FROM Member; Delete according to a WHERE condition Example: User 789 leaves Dead Putting Society DELETE FROM Member WHERE uid = 789 AND gid = 'dps'; Example: Users under age 18 must be removed from United Nuclear Workers DELETE FROM Member WHERE uid IN (SELECT uid FROM User WHERE age < 18) AND gid = 'nuk';

21 UPDATE Example: User 142 changes name to Barney UPDATE User SET name = 'Barney' WHERE uid = 142; Example: We are all popular! UPDATE User SET pop = (SELECT AVG(pop) FROM User); But won t update of every row causes average pop to change? FSubquery is always computed over the old table

22 Constraints 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)

23 Types of SQL constraints NOT NULL Key Referential integrity (foreign key) General assertion Tuple- and attribute-based CHECK s

24 NOT NULL constraint examples CREATE TABLE User (uid INTEGER NOT NULL, name VARCHAR(30) NOT NULL, twitterid VARCHAR(15) NOT NULL, age INTEGER, pop FLOAT); CREATE TABLE Group (gid CHAR(10) NOT NULL, name VARCHAR(100) NOT NULL); CREATE TABLE Member (uid INTEGER NOT NULL, gid CHAR(10) NOT NULL);

25 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

26 Key declaration examples CREATE TABLE User (uid INTEGER NOT NULL PRIMARY KEY, name VARCHAR(30) NOT NULL, twitterid VARCHAR(15) NOT NULL UNIQUE, age INTEGER, pop FLOAT); CREATE TABLE Group (gid CHAR(10) NOT NULL PRIMARY KEY, name VARCHAR(100) NOT NULL); CREATE TABLE Member (uid INTEGER NOT NULL, gid CHAR(10) NOT NULL, PRIMARY KEY(uid, gid)); This form is required for multi-attribute keys

27 Referential integrity example Member.uid references User.uid If an uid appears in Member, it must appear in User Member.gid references Group.gid If a gid appears in Member, it must appear in Group FThat is, no dangling pointers User uid name 142 Bart 123 Milhouse 857 Lisa 456 Ralph 789 Nelson uid gid 142 dps 123 gov 857 abc 857 gov 456 abc 456 gov Member gid abc gov dps Group name

28 Referential integrity in SQL Referenced column(s) must be PRIMARY KEY Referencing column(s) form a FOREIGN KEY Example CREATE TABLE Member (uid INTEGER NOT NULL REFERENCES User(uid), gid CHAR(10) NOT NULL, PRIMARY KEY(uid, gid), FOREIGN KEY (gid) REFERENCES Group(gid)); This form is useful for multi-attribute foreign keys

29 Enforcing referential integrity Example: Member.uid references User.uid Insert or update a Member row so it refers to a nonexistent uid Reject Reject Cascade: ripple changes to all referring rows Set NULL: set all references to NULL All three options can be specified in SQL

30 Deferred constraint checking 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));! 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!)

31 General assertion CREATE ASSERTION!""#$%&'(_(!*# CHECK assertion_condition; assertion_condition is checked for each modification that could potentially violate it Example: Member.uid references User.uid CREATE ASSERTION MemberUserRefIntegrity CHECK (NOT EXISTS (SELECT * FROM Member WHERE uid NOT IN (SELECT uid FROM User))); FIn SQL3, but not all (perhaps no) DBMS supports it

Tuple- and attribute-based CHECK s 32 Associated with a single table Only checked when a tuple/attribute is inserted/updated Reject if condition evaluates to FALSE TRUE and UNKNOWN are fine Examples: CREATE TABLE User(... age INTEGER CHECK(age IS NULL OR age > 0),...); CREATE TABLE Member (uid INTEGER NOT NULL, CHECK(uid IN (SELECT uid FROM User)),...); Is it a referential integrity constraint?

33 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 FNext: triggers, views, indexes