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

Similar documents
Introduction to Data Management CSE 344

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

Database Design Process Entity / Relationship Diagrams

Introduction to Data Management CSE 344

Database Systems CSE 414

Database Systems CSE 414

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

CSE 344 MAY 11 TH 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

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

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Lecture 4. Lecture 4: The E/R Model

Lecture 5. Lecture 5: The E/R Model

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

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

Introduction to Data Management CSE 344

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

Introduction to Database Systems CSE 414

Lecture 04: SQL. Wednesday, October 4, 2006

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

CSC 261/461 Database Systems Lecture 8. Spring 2018

Lecture 04: SQL. Monday, April 2, 2007

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

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

SQL DATA DEFINITION LANGUAGE

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

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

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

SQL DATA DEFINITION LANGUAGE

High Level Database Models

CSC 261/461 Database Systems Lecture 7

The Relational Model. Week 2

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

High-Level Database Models (ii)

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

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

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

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

Translating an ER Diagram to a Relational Schema

The Relational Model 2. Week 3

SQL DATA DEFINITION LANGUAGE

Introduction to Data Management CSE 344

SQL: Data Definition Language

CS W Introduction to Databases Spring Computer Science Department Columbia University

The Relational Model. Chapter 3

CS 564 Midterm Review

The Relational Model

Database Applications (15-415)

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

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

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

Entity-Relationship Models: Good Design and Constraints

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

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

Database Applications (15-415)

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

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

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

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

The Relational Data Model

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

Database Management Systems. Chapter 3 Part 2

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

The Relational Model

OVERVIEW OF DATABASE DEVELOPMENT

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

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

Let s briefly review important EER inheritance concepts

Introduction to Database Systems. The Relational Data Model

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

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

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

CMPT 354 Database Systems I

Chapter 8 INTEGRITY 1

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

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

Database Systems CSE 414

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

Lecture 2: Introduction to SQL

CSE 344 JANUARY 8 TH SQLITE AND JOINS

CSC 261/461 Database Systems Lecture 8. Fall 2017

The Entity-Relationship Model. Overview of Database Design

Introduction to Databases CSE 414. Lecture 2: Data Models

The Relational Model

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

Objectives Definition iti of terms List five properties of relations State two properties of candidate keys Define first, second, and third normal for

Introduction to Data Management CSE 344

Transcription:

Announcements Introduction to Data Management CSE 344 HW5 due this Friday Please note minor up to the instructions WQ6 due next Wednesday Lecture 17: E/R Diagrams and Constraints 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 a very long time (years). Updating the schema while in production is very expensive (why?) 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 Database Design Process Entity / Relationship Diagrams Conceptual Model: Relational Model: Tables + constraints And also functional dep. product company Entity set = a class An entity = an object Attribute city Normalization: Eliminates anomalies Conceptual Schema Relationship Physical storage details Physical Schema 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? 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 a b c 3 B= is a subset of : 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 Can still model as a mathematical set (Q. how?) ssn 11 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 Converting Multi-way Relationships to Binary Of Of Of Of BuyerOf BuyerOf Arrows go in which direction? 15 Make sure you understand why! 16 What s wrong? 3. Design Principles Design Principles: What s Wrong? Country President Moral: be faithful to the specifications of the app! personaddr personname Moral: pick the right kind of entities. 17 18 3

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

N-1 Relationships to Relations prod-id cust-id Multi-way Relationships to Relations prod-id Orders Shipment Orders(prod-ID,cust-ID, 1,, 2) Shipping-Co(, ) Shipping-Co Remember: no separate relations for many-one relationship 25 ssn (prod-id, ssn, ) 26 Modeling Subclasses Subclasses category Some objects in a class may be special define a new class better: define a subclass s Software products Educational products So --- we define subclasses in E/R Software Educational 27 platforms Age Group Subclasses to Relations Software platforms category Educational Other ways to convert are possible Sw. Age Group Name Price Category 99 gadget 49 photo Toy 39 gadget Name Ed. Name platforms unix Age Group toddler Toy retired 29 Modeling Union Types with Subclasses FurniturePiece Say: each piece of furniture is owned either by a person or by a company 30 5

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?) Modeling Union Types with Subclasses Solution 2: better, more laborious Owner FurniturePiece ownedby ownedbycomp. 31 ownedby FurniturePiece 32 Weak Entity Sets A What Are the Keys of R? Entity sets are weak when their key comes from other classes to which they are related. B R H S Team affiliation University sport number T C W U V Team(sport, number, universityname) University() 33 V D E Q F L G Z 34 K What good schemas? 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 35 36 6

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. No formal way to specify multiple keys in E/R diagrams Other constraints: peoples ages are between 0 and 150. 37 ssn 38 Single Value Constraints Referential Integrity Constraints vs. Each product made by at most one company. Some products made by no company 39 Each product made by exactly one company. 40 Other Constraints Constraints in SQL <100 Constraints in SQL: Keys, foreign keys Attribute-level constraints Tuple-level constraints Global constraints: assertions simplest Most complex Q: What does this mean? A: A entity cannot be connected by relationship to more than 99 entities The more complex the constraint, the harder it is to check and to enforce 41 42 7

(, category) Key Constraints CREATE TABLE ( CHAR(30) PRIMARY KEY, category VARCHAR(20)) Keys with Multiple Attributes (, category, ) CREATE TABLE ( CHAR(30), INT, PRIMARY KEY (, category)) OR: CREATE TABLE ( CHAR(30), PRIMARY KEY ()) Name Category Price Gadget 10 Photo 20 Photo 30 Gadget 40 43 44 Other Keys Foreign Key Constraints 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 CREATE TABLE ( prodname CHAR(30) REFERENCES (), DATETIME) prodname is a foreign key to () must be a key in Referential integrity constraints May write just if is PK 45 46 Foreign Key Constraints Example with multi-attribute primary key CREATE TABLE ( prodname CHAR(30), DATETIME, FOREIGN KEY (prodname, category) REFERENCES (, category) (, category) must be a KEY in Name OneClick What happens when data changes? Types of ups: In : insert/up In : delete/up Category gadget Photo Photo ProdName Wiz Ritz Wiz 47 48 8

What happens when data changes? SQL has three policies for maintaining referential integrity: NO ACTION reject violating modifications (default) CASCADE after delete/up do delete/up SET NULL set foreign-key field to NULL SET DEFAULT set foreign-key field to default value need to be declared with column, e.g., CREATE TABLE (pid INT DEFAULT 42) 49 Maintaining Referential Integrity CREATE TABLE ( prodname CHAR(30), DATETIME, FOREIGN KEY (prodname, category) REFERENCES (, category) ON UPDATE CASCADE ON DELETE SET NULL ) Name OneClick Category gadget Photo Photo ProdName Snap EasyShoot Category 50 Constraints on Attributes and Tuples Constraints on attributes: NOT NULL -- obvious meaning... CHECK condition -- any condition! Constraints on tuples CHECK condition 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)) 51 52 Constraints on Attributes and Tuples CREATE TABLE ( productid CHAR(10), CHAR(30), INT CHECK ( > 0), PRIMARY KEY (productid), UNIQUE (, category)) Constraints on Attributes and Tuples 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) 53 54 9

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, they provide triggers 55 10