Introduction to Database Systems CSE 414

Similar documents
Introduction to Data Management CSE 344

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

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

CSE 344 MAY 14 TH ENTITIES

Introduction to Data Management CSE 344

CSE 344 AUGUST 1 ST ENTITIES

Database Systems CSE 414

Database Systems CSE 414

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

CSE 344 JULY 30 TH DB DESIGN (CH 4)

Database Design Process Entity / Relationship Diagrams

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Database Systems CSE 414

Introduction to Database Systems

Entity/Relationship Modelling

Database design and implementation CMPSCI 645. Lecture 06: Constraints and E/R model

Introduction to Database Systems CSE 414

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Lecture 04: SQL. Monday, April 2, 2007

Lecture 04: SQL. Wednesday, October 4, 2006

Principles of Database Systems CSE 544. Lecture #2 SQL The Complete Story

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

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

Introduction to Database Systems CSE 414

L12: ER modeling 5. CS3200 Database design (sp18 s2) 2/22/2018

Constraints. Local and Global Constraints Triggers

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

Lecture 4: Advanced SQL Part II

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

Lecture 03: SQL. Friday, April 2 nd, Dan Suciu Spring

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

Database Systems CSE 414

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

CSC 261/461 Database Systems Lecture 5. Fall 2017

Lecture 5. Lecture 5: The E/R Model

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

SQL DATA DEFINITION LANGUAGE

Constraints and Triggers

Introduction to Database Systems. The Relational Data Model

SQL DATA DEFINITION LANGUAGE

Introduction to Database Systems. The Relational Data Model. Werner Nutt

Database Applications (15-415)

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

Introduction to Relational Databases

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

Lecture 4. Lecture 4: The E/R Model

CS W Introduction to Databases Spring Computer Science Department Columbia University

SQL. Assit.Prof Dr. Anantakul Intarapadung

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

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

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

The Entity-Relationship Model (ER Model) - Part 2

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

CS 564 Midterm Review

The Relational Model 2. Week 3

Introduction to Data Management CSE 344

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

The Relational Model. Chapter 3

Relational data model

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

SQL DATA DEFINITION LANGUAGE

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

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

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

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

The Relational Model. Week 2

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

Announcements. Multi-column Keys. Multi-column Keys (3) Multi-column Keys. Multi-column Keys (2) Introduction to Data Management CSE 414

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

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

CSC 453 Database Technologies. Tanu Malik DePaul University

The Relational Model of Data (ii)

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

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

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

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

CS275 Intro to Databases

SQL: Data Definition Language

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

Introduction to Database Systems. Announcements CSE 444. Review: Closure, Key, Superkey. Decomposition: Schema Design using FD

The Relational Model

Database Systems ( 資料庫系統 )

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

SQL. Structured Query Language

COSC344 Database Theory and Applications. σ a= c (P) Lecture 3 The Relational Data. Model. π A, COSC344 Lecture 3 1

Introduction to Data Management CSE 414

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

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

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

From E/R Diagrams to Relations

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

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

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

Basic SQL. Basic SQL. Basic SQL

Announcements. Using Electronics in Class. Review. Staff Instructor: Alvin Cheung Office hour on Wednesdays, 1-2pm. Class Overview

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

Transcription:

Introduction to Database Systems CSE 414 Lecture 16: Constraints CSE 414 - Spring 2015 1

Announcements Reminders: Web quiz due Monday night XML homework due Wednesday night Today: Constraints (7.1, 7.2, 7.4) CSE 414 - Spring 2015 2

Where We Are? We are learning about database design How to design a database schema? Last time: Real world -> E/R Diagrams -> Relations Next, we will learn more about good schemas Today: Constraints and data integrity Next time: Schema normalization, then Views CSE 414 - Spring 2015 3

Integrity Constraints Motivation An integrity constraint is a condition specified on a database schema that restricts the data that can be stored in an instance of the database. ICs help prevent entry of incorrect information How? DBMS enforces integrity constraints Allows only legal database instances (i.e., those that satisfy all constraints) to exist Ensures that all necessary checks are always performed and avoids duplicating the verification logic in each application CSE 414 - Spring 2015 4

Constraints in E/R Diagrams Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a person. Single-value constraints: a person can have only one father. Referential integrity constraints: if you work for a company, it must exist in the database. Other constraints: peoples ages are between 0 and 150. CSE 414 - Spring 2015 5

Keys in E/R Diagrams Underline: name category price No formal way to specify multiple keys in E/R diagrams Product Person address name ssn 6

Single Value Constraints makes vs. makes CSE 414 - Spring 2015 7

Referential Integrity Constraints Product makes Company Each product made by at most one company. Some products made by no company Product makes Company Each product made by exactly one company. CSE 414 - Spring 2015 8

Other Constraints Product <100 makes Company Q: What does this mean? A: A Company entity cannot be connected by relationship to more than 99 Product entities CSE 414 - Spring 2015 9

Constraints in SQL Constraints in SQL: Keys, foreign keys Attribute-level constraints Tuple-level constraints Global constraints: assertions simplest Most complex The more complex the constraint, the harder it is to check and to enforce CSE 414 - Spring 2015 10

Key Constraints Product(name, category) CREATE TABLE Product ( name CHAR(30) PRIMARY KEY, category VARCHAR(20)) OR: CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), PRIMARY KEY (name)) CSE 414 - Spring 2015 11

Keys with Multiple Attributes Product(name, category, price) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category)) Name Category Price Gizmo Gadget 10 Camera Photo 20 Gizmo Photo 30 Gizmo Gadget 40 CSE 414 - Spring 2015 12

Other Keys CREATE TABLE Product ( productid CHAR(10), name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (productid), UNIQUE (name, category)) There is at most one PRIMARY KEY; there can be many UNIQUE CSE 414 - Spring 2015 13

Foreign Key Constraints CREATE TABLE Purchase ( prodname CHAR(30) REFERENCES Product(name), date DATETIME) Referential integrity constraints prodname is a foreign key to Product(name) name must be a key in Product May write just Product if name is PK CSE 414 - Spring 2015 14

Foreign Key Constraints Product Purchase Name Category ProdName Store Gizmo gadget Gizmo Wiz Camera Photo Camera Ritz OneClick Photo Camera Wiz CREATE TABLE Purchase ( prodname CHAR(30) REFERENCES Product(name), date DATETIME) CSE 414 - Spring 2015 15

Foreign Key Constraints Example with multi-attribute primary key CREATE TABLE Purchase ( prodname CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodname, category) REFERENCES Product(name, category) (name, category) must be a KEY in Product CSE 414 - Spring 2015 16

What happens during updates? Types of updates: In Purchase: insert/update In Product: delete/update Product Purchase Name Category ProdName Store Gizmo gadget Gizmo Wiz Camera Photo Camera Ritz OneClick Photo Camera Wiz CSE 414 - Spring 2015 17

What happens during updates? SQL has three policies for maintaining referential integrity: Reject violating modifications (default) Cascade: after delete/update do delete/update Set-null set foreign-key field to NULL CSE 414 - Spring 2015 18

Maintaining Referential Integrity CREATE TABLE Purchase ( prodname CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodname, category) REFERENCES Product(name, category) ON UPDATE CASCADE ON DELETE SET NULL ) CSE 414 - Spring 2015 19

Constraints on Attributes and Tuples Constraints on attributes: NOT NULL -- obvious meaning... CHECK condition -- any condition! Constraints on tuples CHECK condition CSE 414 - Spring 2015 20

Constraints on Attributes and Tuples CREATE TABLE R ( A int NOT NULL, B int CHECK (B > 50 and B < 100), C varchar(20), D int, CHECK (C >= 'd' or D > 0)) CSE 414 - Spring 2015 21

Constraints on Attributes and Tuples CREATE TABLE Product ( productid CHAR(10), name CHAR(30), category VARCHAR(20), price INT CHECK (price > 0), PRIMARY KEY (productid), UNIQUE (name, category)) CSE 414 - Spring 2015 22

Constraints on Attributes and Tuples What does this constraint do? What is the difference from CREATE TABLE Purchase ( Foreign-Key? prodname CHAR(30) CHECK (prodname IN (SELECT Product.name FROM Product), date DATETIME NOT NULL) CSE 414 - Spring 2015 23

General Assertions CREATE ASSERTION myassert CHECK (NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200) ) But most DBMSs do not implement assertions Because it is hard to support them efficiently Instead, they provide triggers CSE 414 - Spring 2015 24

Database Triggers Event-Condition-Action rules Event Can be insertion, update, or deletion to a relation Condition Can be expressed on DB state before or after event Action Perform additional DB modifications CSE 414 - Spring 2015 25

More About Triggers Row-level trigger Executes once for each modified tuple Statement-level trigger Executes once for all tuples that are modified in a SQL statement CSE 414 - Spring 2015 26

Database Triggers Example When Product.price is updated, if it is decreased then set Product.category = On sale CREATE TRIGGER ProductCategories AFTER UPDATE OF price ON Product REFERENCING OLD ROW AS OldTuple NEW ROW AS NewTuple FOR EACH ROW WHEN (OldTuple.price > NewTuple.price) UPDATE Product SET category = On sale WHERE productid = OldTuple.productID CSE 414 - Spring 2015 27

SQL Server Example CREATE TRIGGER ProductCategory ON Product AFTER UPDATE AS BEGIN UPDATE Product SET category= sale WHERE productid IN (SELECT i.productid from inserted i, deleted d WHERE i.productid = d.productid AND i.price < d.price) END CSE 414 - Spring 2015 28

Discussion Both constraints and triggers are tools that help us keep the database consistent What are their pros and cons? CSE 414 - Spring 2015 29