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

Similar documents
Introduction to Database Systems CSE 414

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

CSE 344 MAY 14 TH ENTITIES

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

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

CSE 344 AUGUST 1 ST ENTITIES

Database Systems CSE 414

Database Systems CSE 414

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

Entity/Relationship Modelling

Introduction to Database Systems

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

Introduction to Data Management CSE 344

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

Introduction to Database Systems CSE 414

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

Constraints. Local and Global Constraints Triggers

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

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

Database Applications (15-415)

Introduction to Database Systems CSE 414

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

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

Lecture 4. Lecture 4: The E/R Model

The Relational Model

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

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

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

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

Lecture 04: SQL. Wednesday, October 4, 2006

The Relational Model. Week 2

The Relational Model 2. Week 3

Constraints and Triggers

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

SQL DATA DEFINITION LANGUAGE

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

Lecture 5. Lecture 5: The E/R Model

Introduction to Databases CSE 414. Lecture 2: Data Models

SQL DATA DEFINITION LANGUAGE

The Relational Model. Chapter 3

Lecture 04: SQL. Monday, April 2, 2007

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

The Relational Data Model. Data Model

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

Database Systems ( 資料庫系統 )

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

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

CS W Introduction to Databases Spring Computer Science Department Columbia University

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

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

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

The Relational Model

Introduction to Data Management CSE 344

The Relational Model of Data (ii)

Relational data model

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

From E/R Diagrams to Relations

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

Database Systems CSE 414

SQL DATA DEFINITION LANGUAGE

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

Relational Database Systems Part 01. Karine Reis Ferreira

The Relational Model

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

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

Databases 1. Defining Tables, Constraints

Introduction to Database Systems. The Relational Data Model

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

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

Database Applications (15-415)

CSC 453 Database Technologies. Tanu Malik DePaul University

Introduction to Databases

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

Conceptual Design with ER Model

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

Introduction to Data Management CSE 344

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

SQL: Data Definition Language

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

Introduction to Data Management CSE 344. Lecture 2: Data Models

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

CS275 Intro to Databases

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

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

Database Systems. Course Administration

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

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

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

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

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

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

Databases. Jörg Endrullis. VU University Amsterdam

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

Review for Exam 1 CS474 (Norton)

Transcription:

Where Are We? Introduction to Data Management CSE 344 Lecture 15: Constraints We know quite a bit about using a DBMS Start with real-world problem, design ER diagram From ER diagram to relations -> conceptual schema Given a schema, we also know how to Create tables; Insert and update data; Run queries Finally, we know a bit about tuning: indexing We know a bit about internals Client/server, relational algebra, query evaluation Some database theory: datalog, relational calculus We also learned a bit about XML and XQuery 1 2 Next Few Lectures We will learn more about good schemas Today: Constraints and data integrity Next lecture: Schema normalization In two lectures: Views 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 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 3 4 Constraints in E/R Diagrams Keys in E/R Diagrams Finding constraints is part of the modeling process. Commonly used constraints: Underline: category 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. price No formal way to specify multiple keys in E/R diagrams Person Other constraints: peoples ages are between 0 and 150. 5 address ssn 6 1

Single Value Constraints Referential Integrity Constraints v. s. Each product made by at most one company. Some products made by no company 7 Each product made by exactly one company. 8 Other Constraints Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related. <100 Team affiliation University sport number What does this mean? 9 10 Handling Weak Entity Sets Types of Constraints in SQL sport Team number affiliation Convert to a relational schema University University() Team(number,universityName,sport) No need to represent affiliation separately 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 11 12 2

(, category) Key Constraints Keys with Multiple Attributes (, category, price) OR: CREATE TABLE ( CHAR(30) PRIMARY KEY, category VARCHAR(20)) CREATE TABLE ( category VARCHAR(20) PRIMARY KEY ()) CREATE TABLE ( price INT, PRIMARY KEY (, category)) Name Category Price Gadget 10 20 30 13 Gadget 40 14 Other Keys CREATE TABLE ( productid CHAR(10), price INT, PRIMARY KEY (productid), UNIQUE (, category)) There is at most one PRIMARY KEY; there can be many UNIQUE prodname CHAR(30) REFERENCES (), date DATETIME) prodname is a foreign key to () must be a key in Referential integrity constraints May write just (why?) 15 16 Purchase Example with multi-attribute primary key Name OneClick Category gadget ProdName Store Ritz prodname CHAR(30), date DATETIME, FOREIGN KEY (prodname, category) REFERENCES (, category) (, category) must be a KEY in 17 18 3

What happens during updates? What happens during updates? Types of updates: In Purchase: insert/update In : delete/update Name Category ProdName Purchase Store 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 gadget Ritz OneClick 19 20 Maintaining Referential Integrity prodname CHAR(30), date DATETIME, FOREIGN KEY (prodname, category) REFERENCES (, category) ON UPDATE CASCADE ON DELETE SET NULL attributes: NOT NULL -- obvious meaning... CHECK condition -- any condition! tuples CHECK condition 21 22 CREATE TABLE R (A int NOT NULL, B int CHECK (B < 50 or B > 100), C varchar(20), D int, CHECK (C >= 'd' and D > 0)) CREATE TABLE ( productid CHAR(10), price INT CHECK (price > 0), PRIMARY KEY (productid), UNIQUE (, category)) 23 24 4

What is the difference from Foreign-Key? prodname CHAR(30) CHECK (prodname IN (SELECT. FROM ), date DATETIME NOT NULL) General Assertions CREATE ASSERTION myassert CHECK NOT EXISTS( SELECT. FROM, Purchase WHERE. = Purchase.prodName GROUP BY. HAVING count(*) > 200) But most DBMSs do not implement assertions Because it is hard to support them efficiently Instead, they provide triggers 25 26 Database Triggers 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 CREATE TRIGGER Categories AFTER UPDATE OF price ON REFERENCING OLD ROW AS OldTuple NEW ROW AS NewTuple FOR EACH ROW WHEN (OldTuple.price > NewTuple.price) UPDATE SET category = On sale WHERE productid = OldTuple.productID 27 28 5