Database Systems CSE 414

Similar documents
Database Systems CSE 414

Database Design Process Entity / Relationship Diagrams

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

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 MAY 14 TH ENTITIES

CSE 344 JULY 30 TH DB DESIGN (CH 4)

Introduction to Data Management CSE 344

Introduction to Database Systems CSE 414

Introduction to Database Systems CSE 414. Lecture 19: E/R Diagrams

CSE 344 AUGUST 1 ST ENTITIES

Entity/Relationship Modelling

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 Database Systems CSE 414

CSE 344 MAY 11 TH ENTITIES

Introduction to Data Management CSE 344

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

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

Lecture 4. Lecture 4: The E/R Model

Lecture 5. Lecture 5: The E/R Model

Introduction to Data Management CSE 344

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

Introduction to Database Systems

Conceptual Design with ER Model

L11: ER modeling 4. CS3200 Database design (sp18 s2) 2/15/2018

2. E/R Design Considerations

3. Advanced E/R Concepts

3. Advanced E/R Concepts

CSC 261/461 Database Systems Lecture 8. Spring 2018

Introduction to Database Systems CSE 414

Lecture 04: SQL. Monday, April 2, 2007

Introduction to Database Systems CSE 544. Lecture #2 January 16, 2007

Lecture 04: SQL. Wednesday, October 4, 2006

CSC 261/461 Database Systems Lecture 8. Fall 2017

Introduction to Data Management CSE 344

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

CSC 261/461 Database Systems Lecture 5. Fall 2017

CSE 544 Data Models. Lecture #3. CSE544 - Spring,

CSC 261/461 Database Systems Lecture 6. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

High Level Database Models

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

CSC 261/461 Database Systems Lecture 7

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

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

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

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

CS 564 Midterm Review

High-Level Database Models (ii)

The Relational Model 2. Week 3

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

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

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

OVERVIEW OF DATABASE DEVELOPMENT

The Relational Data Model

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

The Relational Model. Chapter 3

Lecture 4: Advanced SQL Part II

Database Systems CSE 414

SQL DATA DEFINITION LANGUAGE

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

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

The Relational Model

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)

Introduction to Data Management CSE 344

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

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

The Relational Model. Week 2

Entity-Relationship Models: Good Design and Constraints

The Relational Model

Database Applications (15-415)

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

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

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

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

CMPT 354 Database Systems I

SQL DATA DEFINITION LANGUAGE

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

Lecture 2: Introduction to SQL

Database Management Systems. Chapter 3 Part 2

Translating an ER Diagram to a Relational Schema

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

The Relational Model

SQL. Assit.Prof Dr. Anantakul Intarapadung

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

Database Systems ( 資料庫系統 )

CS W Introduction to Databases Spring Computer Science Department Columbia University

The Entity-Relationship Model. Overview of Database Design

CIS 330: Applied Database Systems

The Relational Model (ii)

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

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

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

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

CSE 530A. ER Model. Washington University Fall 2013

SYLLABUS ADMIN DATABASE SYSTEMS I WEEK 2 THE ENTITY-RELATIONSHIP MODEL. Assignment #2 changed. A2Q1 moved to A3Q1

Transcription:

Database Systems CSE 414 Lecture 22: E/R Diagrams (4.1-6) and Constraints (7.1-2) CSE 414 - Fall 2017 1

Announcements HW7 will be posted on Tuesday and due on Dec. 1 st 11pm WQ6 will be posted on Tuesday and due on Nov. 30th 11pm CSE 414 - Fall 2017 2

Database Design What it is: Starting from scratch, design the database schema: relation, attributes, keys, foreign keys, constraints etc. Why it s hard: The database will be in operation for years. Updating the schema in production is very hard: schema change modifications are expensive (why?) making the change without introducing any bugs is hard this part is, by far, the most important consideration in practice CSE 414 - Fall 2017 3

Database Design Consider issues such as: What entities to model How entities are related What constraints exist in the domain Several formalisms exists We discuss E/R diagrams Reading: Sec. 4.1-4.6 CSE 414 - Fall 2017 4

Database Design Process Conceptual Model: name product makes company price name address Relational Model: Tables + constraints And also functional dep. Normalization: Eliminates anomalies Conceptual Schema Physical storage details Physical Schema CSE 414 - Fall 2017 5

Entity / Relationship Diagrams Entity set = a class An entity = an object Product Attribute city Relationship makes CSE 414 - Fall 2017 6

name CEO name price Product makes Company address buys Person employs address name ssn CSE 414 - Fall 2017 7

Keys in E/R Diagrams Every entity set must have a key name price Product CSE 414 - Fall 2017 8

What is a Relation(ship)? A mathematical definition: if A, B are sets, then a relation R is a subset of A B A = {1, 2, 3}, B = {a, b, c, d}, A B = {(1,a), (1,b),..., (3,d)} R = {(1,a), (1,c), (3,b)} A= makes is a subset of Product Company: 1 2 3 B= a b c d Product makes Company CSE 414 - Fall 2017 9

Multiplicity of E/R Relations one-one: 1 2 3 many-one 1 2 3 many-many 1 2 3 a b c d a b c d a b c d CSE 414 - Fall 2017 10

name CEO name price Product makes Company buys What does this say? employs address Person address name ssn CSE 414 - Fall 2017 11

Multi-way Relationships How do we model a purchase relationship between buyers, products and stores? Product Purchase Store Person Can still model as a mathematical set (Q. how?) A. As a set of triples Person Product Store CSE 414 - Fall 2017 12

Arrows in Multiway Relationships Q: What does the arrow mean? Product Purchase Store Person A: A given person buys a given product from at most one store [Arrow pointing to E means that if we select one entity from each of the other entity sets in the relationship, those entities are related to at most one entity in E] CSE 414 - Fall 2017 13

Arrows in Multiway Relationships Q: What does the arrow mean? Product Purchase Store Person A: A given person buys a given product from at most one store AND every store sells to every person at most one product CSE 414 - Fall 2017 14

Converting Multi-way Relationships to Binary date ProductOf Product Purchase StoreOf Store Arrows go in which direction? BuyerOf Person CSE 414 - Fall 2017 15

Converting Multi-way Relationships to Binary date ProductOf Product Purchase StoreOf Store Make sure you understand why! BuyerOf CSE 414 - Fall 2017 Person 16

3. Design Principles What s wrong? Product Purchase Person Country President Person Lesson: be faithful to the specifications of the app! CSE 414 - Fall 2017 17

Design Principles: What s Wrong? Product date Purchase Store personaddr personname Lesson: pick the right kind of entities. CSE 414 - Fall 2017 18

Design Principles: What s Wrong? Dates date Product Purchase Store Lesson: don t complicate life more than it already is. Person CSE 414 - Fall 2017 19

From E/R Diagrams to Relational Schema Entity set relation Relationship relation CSE 414 - Fall 2017 20

Entity Set to Relation prod-id category price Product Product(prod-ID, category, price) prod-id category price Gizmo55 Camera 99.99 Pokemn19 Toy 29.99 CSE 414 - Fall 2017 21

N-N Relationships to Relations date prod-id cust-id date name Orders Shipment Shipping-Co address Represent this in relations CSE 414 - Fall 2017 22

N-N Relationships to Relations date prod-id cust-id date name Orders Shipment Shipping-Co Orders(prod-ID, cust-id, date) Shipment(prod-ID, cust-id, name, date) Shipping-Co(name, address) address prod-id cust-id name date Gizmo55 Joe12 UPS 4/10/2011 Gizmo55 Joe12 FEDEX 4/9/2011 CSE 414 - Fall 2017 23

N-1 Relationships to Relations date prod-id cust-id date name Orders Shipment Shipping-Co address Represent this in relations CSE 414 - Fall 2017 24

N-1 Relationships to Relations date prod-id cust-id date name Orders Shipment Shipping-Co Orders(prod-ID, cust-id, date1, ship_co, ship_date) Shipping-Co(name, address) address Remember: many-one relationship becomes FK, not relation CSE 414 - Fall 2017 25

Ex: NFL Game DB Play Team play_id yardline team_id city receiving_yds PlaysFor PlayedIn Player PlayedIn(play_id, player_id, receiving_yds, ) player_id name (Actually, the key of Play is not play_id. More on this later...) CSE 414 - Fall 2017 26

Multi-way Relationships to Relations Product name address prod-id price Purchase Store Person Purchase(prod-ID, ssn, name) name ssn CSE 414 - Fall 2017 27

Modeling Subclasses Some objects in a class may be special define a new class better: define a subclass Products Software products Educational products So --- we define subclasses in E/R CSE 414 - Fall 2017 28

Subclasses name category price Product isa isa Software Product Educational Product platforms Age Group CSE 414 - Fall 2017 29

Subclasses to Relations (one option) Product Name Price Category name category Gizmo 99 gadget price Product Camera 49 photo Toy 39 gadget isa isa Sw.Product Name Gizmo platforms unix Software Product platforms Educational Product Other ways to convert are possible Age Group Ed.Product Name Gizmo Toy Age Group toddler retired CSE 414 - Fall 2017 30

Modeling Union Types with Subclasses FurniturePiece Person Company Say: each piece of furniture is owned either by a person or by a company CSE 414 - Fall 2017 31

Modeling Union Types with Subclasses Say: each piece of furniture is owned either by a person or by a company Solution 1. Acceptable but imperfect (What s wrong?) Person FurniturePiece Company ownedbyperson ownedbycomp. CSE 414 - Fall 2017 32

Modeling Union Types with Subclasses Solution 2: better, more laborious Owner isa Person ownedby isa Company FurniturePiece CSE 414 - Fall 2017 33

Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related. Team affiliation University sport number name Team(sport, number, universityname) University(name) CSE 414 - Fall 2017 34

What Are the Keys of R? A B R H S T C W U V V D E Q F CSE 414 - Fall 2017 L G Z 35 K

Ex: NFL Game DB Game gsis_id GameOf price DriveOf Play play_id yardline Drive drive_id pos_team CSE 414 - Fall 2017 36

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. Most important issue in practice 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 - Fall 2017 37

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: can have only one genetic 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. some values should not be NULL CSE 414 - Fall 2017 38

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 CSE 414 - Fall 2017 39

Single Value Constraints makes vs. makes CSE 414 - Fall 2017 40

Referential Integrity Constraints Product makes Company Each product made by at most one company. Some products made by no company Which one is FK? Product makes Company Each product made by exactly one company. CSE 414 - Fall 2017 41

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 - Fall 2017 42

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 (Still, performance is secondary to correctness.) CSE 414 - Fall 2017 43

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 - Fall 2017 44

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 - Fall 2017 45

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 - Fall 2017 46

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 (i.e., PK or UNIQUE) May write just Product if name is PK CSE 414 - Fall 2017 47

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 - Fall 2017 48

What happens when data changes? 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 - Fall 2017 49

What happens when data changes? SQL has three options for maintaining referential integrity on changes: NO ACTION reject bad modifications (default) CASCADE after delete/update do delete/update SET NULL set foreign-key field to NULL SET DEFAULT set FK field to default value need to be declared with column, e.g., CREATE TABLE Product (pid INT DEFAULT 42) CSE 414 - Fall 2017 50

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) Product Purchase Name Category ProdName Category Gizmo gadget Gizmo gadget Camera Photo Snap Camera OneClick Photo EasyShoot Camera CSE 414 - Fall 2017 51

Constraints on Attributes and Tuples Constraints on attributes: NOT NULL -- obvious meaning... CHECK condition -- any condition! Constraints on tuples CHECK condition CSE 414 - Fall 2017 52

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)) CSE 414 - Fall 2017 53

Constraints on Attributes and Tuples CREATE TABLE Product ( productid CHAR(10), name CHAR(30), category VARCHAR(20) CHECK (category in ( toy, gadget, apparel )), price INT CHECK (price > 0), PRIMARY KEY (productid)) CSE 414 - Fall 2017 54

Constraints on Attributes and Tuples CREATE TABLE Product ( productid CHAR(10), name CHAR(30) NOT NULL, category VARCHAR(20) CHECK (category in ( toy, gadget, apparel )), price INT CHECK (price > 0), PRIMARY KEY (productid)) CSE 414 - Fall 2017 55

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 - Fall 2017 56

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 - Fall 2017 57

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, DBMSs provide triggers CSE 414 - Fall 2017 58