CS W Introduction to Databases Spring Computer Science Department Columbia University

Similar documents
SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

PostgreSQL 7.3 Documentation Prev Chapter 2. Data Definition Next

SQL DATA DEFINITION LANGUAGE

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL DATA DEFINITION LANGUAGE

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

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

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

Constraints. Local and Global Constraints Triggers

SQL DATA DEFINITION LANGUAGE

Conceptual Design. The Entity-Relationship (ER) Model

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

The Relational Model 2. Week 3

The Relational Model

The Relational Model. Chapter 3

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

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

SQL: Data Definition Language

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

Constraints and Triggers

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Databases-1 Lecture-01. Introduction, Relational Algebra

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

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

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

The Relational Model

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

CS54100: Database Systems

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Introduction to Databases

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

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

SQL DDL. CS3 Database Systems Weeks 4-5 SQL DDL Database design. Key Constraints. Inclusion Constraints

Handout 6 CS-605 Spring 18 Page 1 of 7. Handout 6. Physical Database Modeling

Database Systems ( 資料庫系統 )

CS2300: File Structures and Introduction to Database Systems

CS698F Advanced Data Management. Instructor: Medha Atre. Aug 04, 2017 CS698F Adv Data Mgmt 1

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

Translating an ER Diagram to a Relational Schema

Conceptual Design with ER Model

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

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

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

Database Systems. Course Administration

Database Management Systems. Chapter 3 Part 2

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

From E/R Diagrams to Relations

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

Review The Big Picture

High-Level Database Models (ii)

The Relational Model of Data (ii)

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

Introduction to Database Systems CSE 414

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

The Relational Model (ii)

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

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

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

SQL Constraints and Triggers

CSC 261/461 Database Systems Lecture 6. Fall 2017

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

CS425 Midterm Exam Summer C 2012

ER to Relational Model. Professor Jessica Lin

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

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

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

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Databases 1. Defining Tables, Constraints

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

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

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

The Relational Data Model. Data Model

Database Applications (15-415)

Slides by: Ms. Shree Jaswal

CSCD43: Database Systems Technology. Lecture 4

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

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Basic SQL. Basic SQL. Basic SQL

Introduction to Data Management CSE 344

Referential Integrity and Other Table Constraints Ray Lockwood

The Relational Model

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

Database Design. Goal: specification of database schema Methodology:

The Relational Model. Week 2

CSE 344 MAY 14 TH ENTITIES

From ER to Relational Model. Book Chapter 3 (part 2 )

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Introduction to Data Management CSE 344

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

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

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

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

CSCC43H: Introduction to Databases. Lecture 4

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

CIS 330: Applied Database Systems

Introduction to Data Management CSE 344

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

download instant at The Relational Data Model

Transcription:

CS W4111.001 Introduction to Databases Spring 2018 Computer Science Department Columbia University 1 in SQL 1. Key constraints (PRIMARY KEY and UNIQUE) 2. Referential integrity constraints (FOREIGN KEY REFERENCES) 3. on values of attributes: NOT NULL attribute-based CHECK constraints Acknowledgment: These slides contain material from the Database Systems: The Complete Book textbook, 2 nd Edition, H. Garcia-Molina, J. D. Ullman, and J. Widom, 2009 1

Attribute-Based CHECK Use keyword CHECK followed by a parenthesized condition that must be true for every value of this attribute In practice (good style): condition is simple limit on values In general (not necessarily good style): condition can be anything that could follow WHERE in a SQL query Attribute-Based CHECK Example of good-style use: CREATE TABLE Products ( product_no INTEGER, ); name CHAR(255), price REAL CHECK (price > 0), PRIMARY KEY (product_no) 2

Attribute-Based CHECK When is an attribute-based CHECK constraint checked by the DBMS? Only when a tuple gets a new value for the attribute to which the constraint is attached: update of the corresponding attribute insertion of a new tuple in the table where the constraint is defined If constraint is violated, then update or insertion is rejected (Bad-Style) Attribute-Based CHECK Attribute-based CHECK constraints can mention other attributes or relations (bad style): need a subquery for this 3

(Bad-Style) Attribute-Based CHECK Example of bad-style use: Suppose that we want to simulate (incorrectly) a foreign-key constraint: CREATE TABLE Works_In ( ssn INTEGER, did INTEGER CHECK (did IN (SELECT D.did FROM Departments D)), PRIMARY KEY (ssn, did), FOREIGN KEY ssn REFERENCES Employees); Why doesn t this work? (Bad-Style) Attribute-Based CHECK Answer: CHECK is only associated with modifications of attribute in question (i.e., did in Works_In), not with every relation and attribute mentioned. So deletions and updates in the Departments table are a problem, because they don t cause the DBMS to verify that the CHECK constraint still holds 4

in SQL 1. Key constraints (PRIMARY KEY and UNIQUE) 2. Referential integrity constraints (FOREIGN KEY REFERENCES) 3. on values of attributes: NOT NULL attribute-based CHECK constraints 4. on values of tuples of a relation: tuple-based CHECK constraints (or table constraints) Tuple-Based CHECK Like attribute-based CHECK constraints, but associated with each tuple of one table 5

Tuple-Based CHECK Example of good-style use: CREATE TABLE Products ( product_no INTEGER, name CHAR(255), price REAL, discounted_price REAL, CHECK (price > discounted_price), PRIMARY KEY (product_no) ); Tuple-Based CHECK When is a tuple-based CHECK constraint checked by the DBMS? Only when a tuple is inserted into the corresponding table and when a tuple is updated in the corresponding table, and the CHECK constraint is evaluated only for the new or updated tuple If constraint is violated, then insertion or update is rejected 6

(Bad-Style) Tuple-Based CHECK Tuple-based CHECK constraints can mention other relations (bad style): need a subquery for this (Bad-Style) Tuple-Based CHECK Example of bad-style use: Suppose that we want to enforce (incorrectly) the total participation constraint of Employees in Works_In: CREATE TABLE Employees ( ssn INTEGER, name CHAR(30), PRIMARY KEY (ssn), CHECK (ssn IN (SELECT W.ssn FROM Works_In W))); Why doesn t this work? 7

(Bad-Style) Tuple-Based CHECK Answer: CHECK is only associated with insertions or updates of tuples in relation where the CHECK constraint is defined (i.e., Employees), not with every relation mentioned. So deletions or updates in the Works_In table are a problem, because they don t cause the DBMS to verify that the CHECK constraint still holds in SQL 1. Key constraints (PRIMARY KEY and UNIQUE) 2. Referential integrity constraints (FOREIGN KEY REFERENCES) 3. on values of attributes: NOT NULL attribute-based CHECK constraints 4. on values of tuples of a relation: tuple-based CHECK constraints (or table constraints) 5. Schema-level constraints: assertions 8

Assertions To enforce any condition (that can follow WHERE) Not associated with tables or attributes Guaranteed to always hold: any database modification, anywhere, that would make an assertion false is rejected Expensive; not implemented universally (PostgreSQL ) Assertions We can now finally model (correctly) the total participation constraint of Employees in Works_In: CREATE ASSERTION TotalPartEmpWorksIn CHECK ( NOT EXISTS (SELECT * FROM Employees E WHERE E.ssn NOT IN (SELECT W.ssn FROM Works_In W))); 9

Summary Type of constraint Where declared When activated Guaranteed to hold? attribute-based CHECK with attribute on insertion to relation or attribute update not if subqueries tuple-based CHECK with relation schema on insertion to relation or tuple update not if subqueries assertion with database schema on change to any mentioned relation yes 10