Database Design Process Entity / Relationship Diagrams

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

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

Introduction to Data Management CSE 344

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. Lecture 19: E/R Diagrams

Introduction to Database Systems CSE 414

Entity/Relationship Modelling

CSE 344 AUGUST 1 ST ENTITIES

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

Introduction to Data Management CSE 344

CSE 344 MAY 11 TH ENTITIES

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

Lecture 4. Lecture 4: The E/R Model

Lecture 5. Lecture 5: The E/R Model

Introduction to Data Management CSE 344

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

Introduction to Data Management CSE 344

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

Introduction to Database Systems

Conceptual Design with ER Model

3. Advanced E/R Concepts

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

2. E/R Design Considerations

3. Advanced E/R Concepts

CSC 261/461 Database Systems Lecture 8. Spring 2018

Lecture 04: SQL. Monday, April 2, 2007

Introduction to Database Systems CSE 414

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

Lecture 04: SQL. Wednesday, October 4, 2006

CSC 261/461 Database Systems Lecture 7

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

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

Introduction to Data Management CSE 344

CSC 261/461 Database Systems Lecture 8. Fall 2017

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

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

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

High Level Database Models

High-Level Database Models (ii)

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

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

SQL DATA DEFINITION LANGUAGE

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

SQL DATA DEFINITION LANGUAGE

CS 564 Midterm Review

OVERVIEW OF DATABASE DEVELOPMENT

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

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

The Relational Model 2. Week 3

The Relational Model

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

The Relational Data Model

Introduction to Database Systems. The Relational Data Model

CSC 261/461 Database Systems Lecture 5. Fall 2017

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

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

Translating an ER Diagram to a Relational Schema

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

Lecture 2: Introduction to SQL

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language

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

The Relational Model

CS W Introduction to Databases Spring Computer Science Department Columbia University

Database Management Systems. Chapter 3 Part 2

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

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

Database Systems CSE 414

The Relational Model. Chapter 3

Database Applications (15-415)

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

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

Let s briefly review important EER inheritance concepts

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

The Relational Model. Week 2

The Entity-Relationship Model. Overview of Database Design

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

Entity-Relationship Models: Good Design and Constraints

The Relational Model

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

Database Applications (15-415)

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

CMPT 354 Database Systems I

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

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

Lecture 4: Advanced SQL Part II

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

Chapter 4. The Relational Model

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

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

Chapter 8 INTEGRITY 1

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

Handout 6 Logical design: Translating ER diagrams into SQL CREATE statements

Transcription:

Announcements Database Systems CSE 414 Lecture 22: E/R Diagrams (4.1-6) and Constraints (7.1-2) 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 1 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 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 3 4 Conceptual Model: Database Design Process product company Entity / Relationship Diagrams Relational Model: Tables + constraints And also functional dep. Normalization: Eliminates anomalies Conceptual Schema Physical storage details Physical Schema Entity set = a class An entity = an object Attribute Relationship city 5 6 1

CEO Keys in E/R Diagrams Every entity set must have a key buys employs ssn 7 8 What is a Relation(ship)? Multiplicity of E/R Relations 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= 1 2 3 B= is a subset of : a b c d 9 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 10 CEO Multi-way Relationships How do we model a purchase relationship between buyers, products and stores? buys What does this say? employs ssn 11 Can still model as a mathematical set (Q. how?) A. As a set of triples 12 2

Arrows in Multiway Relationships Q: What does the arrow mean? Arrows in Multiway Relationships Q: What does the arrow mean? 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] 13 A: A given person buys a given product from at most one store AND every store sells to every person at most one product 14 Converting Multi-way Relationships to Binary Of Converting Multi-way Relationships to Binary Of Of Of Arrows go in which direction? BuyerOf 15 Make sure you understand why! BuyerOf 16 What s wrong? 3. Design Principles Design Principles: What s Wrong? Country President Lesson: be faithful to the specifications of the app! personaddr Lesson: pick the right kind of entities. personname 17 18 3

Design Principles: What s Wrong? Dates From E/R Diagrams to Relational Schema Entity set relation Relationship relation Lesson: don t complicate life more than it already is. 19 20 Entity Set to Relation category N-N Relationships to Relations cust-id (, category, ) Orders Shipment Shipping-Co category Gizmo55 Camera 99.99 Pokemn19 Toy 29.99 21 Represent this in relations 22 N-N Relationships to Relations Orders cust-id Shipment Orders(, cust-id, ) Shipment(, cust-id,, ) Shipping-Co(, ) Shipping-Co cust-id Gizmo55 Joe12 UPS 4/10/2011 Gizmo55 Joe12 FEDEX 4/9/2011 23 N-1 Relationships to Relations Orders cust-id Shipment Represent this in relations Shipping-Co 24 4

N-1 Relationships to Relations cust-id play_id Play Ex: NFL Game DB yardline Team team_id city Orders Shipment Shipping-Co Orders(, cust-id, 1, ship_co, ship_) Shipping-Co(, ) Remember: many-one relationship becomes FK, not relation 25 PlayedIn receiving_yds PlayedIn(play_id, player_id, receiving_yds, ) Player player_id (Actually, the key of Play is not play_id. More on this later...) PlaysFor 26 Multi-way Relationships to Relations Modeling Subclasses Some objects in a class may be special define a new class better: define a subclass (, ssn, ) ssn Software products s Educational products 27 So --- we define subclasses in E/R 28 Subclasses Software platforms category Educational Age Group 29 Subclasses to Relations (one option) Software platforms category Educational Other ways to convert are possible Name Price Category Gizmo 99 gadget Camera 49 photo Toy 39 gadget Sw. Age Group Name Gizmo Ed. Name Gizmo Toy platforms unix Age Group toddler retired 30 5

Modeling Union Types with Subclasses FurniturePiece 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?) FurniturePiece Say: each piece of furniture is owned either by a person or by a company ownedby ownedbycomp. 31 32 Modeling Union Types with Subclasses Solution 2: better, more laborious Owner Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related. Team affiliation University ownedby sport number FurniturePiece Team(sport, number, universityname) University() 33 34 A B What Are the Keys of R? R H Game Ex: NFL Game DB S gsis_id Play GameOf DriveOf T C play_id yardline W U V Drive V D E F Q L G Z 35 K drive_id pos_team 36 6

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 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 38 Underline: Keys in E/R Diagrams category Single Value Constraints No formal way to specify multiple keys in E/R diagrams vs. ssn 39 40 Referential Integrity Constraints Other Constraints Each product made by at most one company. Some products made by no company Which one is FK? Each product made by exactly one company. 41 <100 Q: What does this mean? A: A entity cannot be connected by relationship to more than 99 entities 42 7

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.) Key Constraints (, category) CREATE TABLE ( CHAR(30) PRIMARY KEY, category VARCHAR(20)) OR: CREATE TABLE ( CHAR(30), PRIMARY KEY ()) 43 44 Keys with Multiple Attributes (, category, ) CREATE TABLE ( CHAR(30), INT, PRIMARY KEY (, category)) Name Category Price Gizmo Gadget 10 Camera Photo 20 Gizmo Photo 30 Gizmo Gadget 40 45 Other Keys CREATE TABLE ( productid CHAR(10), CHAR(30), INT, PRIMARY KEY (productid), UNIQUE (, category)) There is at most one PRIMARY KEY; there can be many UNIQUE 46 Foreign Key Constraints Foreign Key Constraints CREATE TABLE ( prodname CHAR(30) REFERENCES (), DATETIME) prodname is a foreign key to () must be a key in (i.e., PK or UNIQUE) Referential integrity constraints May write just if is PK Example with multi-attribute primary key CREATE TABLE ( prodname CHAR(30), DATETIME, FOREIGN KEY (prodname, category) REFERENCES (, category)) (, category) must be a KEY in 47 48 8

Name Gizmo Camera OneClick What happens when data changes? Types of ups: In : insert/up In : delete/up Category gadget Photo Photo ProdName Gizmo Camera Camera Wiz Ritz Wiz What happens when data changes? SQL has three options for maintaining referential integrity on changes: NO ACTION reject bad modifications (default) CASCADE after delete/up do delete/up 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 (pid INT DEFAULT 42) 49 50 Maintaining Referential Integrity CREATE TABLE ( prodname CHAR(30), DATETIME, FOREIGN KEY (prodname, category) REFERENCES (, category) ON UPDATE CASCADE ON DELETE SET NULL) Name Category ProdName Category Gizmo gadget Gizmo gadget Camera Photo Snap Camera OneClick Photo EasyShoot Camera 51 Constraints on Constraints on attributes: NOT NULL -- obvious meaning... CHECK condition -- any condition! Constraints on tuples CHECK condition 52 Constraints on Constraints on CREATE TABLE ( productid CHAR(10), CHAR(30), INT CHECK ( > 0), PRIMARY KEY (productid)) CREATE TABLE ( productid CHAR(10), CHAR(30), category VARCHAR(20) CHECK (category in ( toy, gadget, apparel )), INT CHECK ( > 0), PRIMARY KEY (productid)) 53 54 9

Constraints on Constraints on CREATE TABLE ( productid CHAR(10), CHAR(30) NOT NULL, category VARCHAR(20) CHECK (category in ( toy, gadget, apparel )), INT CHECK ( > 0), PRIMARY KEY (productid)) 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)) 55 56 Constraints on What does this constraint do? What is the difference from CREATE TABLE ( Foreign Key? prodname CHAR(30) CHECK (prodname IN (SELECT. FROM )), DATETIME NOT NULL) General Assertions CREATE ASSERTION myassert CHECK (NOT EXISTS( SELECT. FROM, WHERE. =.prodname GROUP BY. HAVING count(*) > 200) ) But most DBMSs do not implement assertions, because it is hard to support them efficiently. Instead, DBMSs provide triggers 57 58 10