ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

Similar documents
Conceptual Design with ER Model

Entity/Relationship Modelling

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

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

Introduction to Database Systems CSE 414

Database Systems CSE 414

Lecture 4. Lecture 4: The E/R Model

Lecture 5. Lecture 5: The E/R Model

Database Systems CSE 414

Introduction to Data Management CSE 344

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

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

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

CSE 344 MAY 11 TH ENTITIES

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

Entity-Relationship Model. Purpose of E/R Model

CSC 261/461 Database Systems Lecture 7

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

Entity-Relationship Models: Good Design and Constraints

Entity-Relationship Model

2. E/R Design Considerations

Database Design Using E/R Model

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #5: Entity/Relational Models---Part 1

Conceptual Design. The Entity-Relationship (ER) Model

CS 405G: Introduction to Database Systems. Database Design II

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

Database Systems. Course Administration

CSE 344 MAY 14 TH ENTITIES

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

CMPT 354 Database Systems I

Conceptual Data Modeling

High Level Database Models

E/R Diagrams! Converting E/R Diagrams to Relations!

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

OVERVIEW OF DATABASE DEVELOPMENT

CS54100: Database Systems

MIS Database Systems Entity-Relationship Model.

CSE 530A. ER Model. Washington University Fall 2013

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

CSC 261/461 Database Systems Lecture 8. Spring 2018

Introduction to Database Systems CSE 414

CSE 344 AUGUST 1 ST ENTITIES

Entity-Relationship Model &

FUNCTIONAL DEPENDENCIES

High-Level Database Models (ii)

The Relational Model 2. Week 3

Translating an ER Diagram to a Relational Schema

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

The Entity-Relationship Model. Overview of Database Design

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

Database Applications (15-415)

CS W Introduction to Databases Spring Computer Science Department Columbia University

CS 146 Database Systems

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

Introduction to Data Management CSE 344

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

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

CS/INFO 330 Entity-Relationship Modeling. Announcements. Goals of This Lecture. Mirek Riedewald

The Next Step: Designing DB Schema. Chapter 6: Entity-Relationship Model. The E-R Model. Identifying Entities and their Attributes.

Information Systems Engineering

Information Systems Engineering. Entity Relationship Model

Introduction to Database Design

Database Applications (15-415)

CIS 330: Web-driven Web Applications. Lecture 2: Introduction to ER Modeling

Entity Relationship Data Model. Slides by: Shree Jaswal

The Relational Model. Chapter 3

Contents. Database. Information Policy. C03. Entity Relationship Model WKU-IP-C03 Database / Entity Relationship Model

Chapter 6: Entity-Relationship Model. The Next Step: Designing DB Schema. Identifying Entities and their Attributes. The E-R Model.

Introduction to Database Design

Chapter 2: Entity-Relationship Model

Database Design. ER Model. Overview. Introduction to Database Design. UVic C SC 370. Database design can be divided in six major steps:

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

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

Introduction to Database Design


Informatics 1: Data & Analysis

Relational Database Design Part I. Announcement. Relational model: review. CPS 116 Introduction to Database Systems

KEYS WEAK ENTITY SETS EXAMPLE: WEAK ENTITY SET EXAMPLE: WEAK ENTITY SET. Beers. Ales. Players. Teams ISA. and. Plays-on

Overview. Introduction to Database Design. ER Model. Database Design

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

The Relational Model (ii)

Database Applications (15-415)

Chapter 2 Conceptual Modeling. Objectives

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

Relational Model. CS 377: Database Systems

Represent entities and relations with diagrams

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

COMP Instructor: Dimitris Papadias WWW page:

Chapter 7: Entity-Relationship Model

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

Lecture 2: Introduction to SQL

Design Techniques. 1. Avoid redundancy 2. Limit the use of weak entity sets 3. Don t use an entity set when an attribute will do

Chapter 6: Entity-Relationship Model

A database can be modeled as: + a collection of entities, + a set of relationships among entities.

Chapter 7: Entity-Relationship Model

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

Transcription:

ENTITY-RELATIONSHIP MODEL CS 564- Spring 2018

WHAT IS THIS LECTURE ABOUT E/R Model: entity sets, attribute relation: binary, multi-way relationship roles, attributes on relationships subclasses (ISA) weak entity sets constraints design principles E/R to Relational Model CS 564 [Spring 2018] - Paris Koutris 2

HOW TO BUILD A DB APPLICATION CS 564 [Spring 2018] - Paris Koutris 3

HOW TO BUILD A DB APPLICATION Pick an application Figure out what to model (ER model) Output: ER diagram Transform the ER diagram to a relational schema Refine the relational schema (normalization) Now ready to implement the schema and load the data! CS 564 [Spring 2018] - Paris Koutris 4

RUNNING EXAMPLE We want to store information about: companies and employees Each company has a name, an address, Each company has a list of employees products manufactured by these companies Each product has a name, a description, CS 564 [Spring 2018] - Paris Koutris 5

E/R MODEL Gives us a visual language to specify what information the DB must hold what are the relationships among components of that information Proposed by Peter Chen in 1976 What we will cover: 1. basic stuff: entities, attributes, relationships 2. constraints 3. weak entity sets 4. design principles CS 564 [Spring 2018] - Paris Koutris 6

ENTITIES & ATTRIBUTES Entity an object distinguishable from other object Entity set a collection of similar entities represented by rectangles described using a set of attributes Attribute represented by ovals attached to an entity set Product name CS 564 [Spring 2018] - Paris Koutris 7

ENTITIES & ATTRIBUTES price name category name stockprice Product Company Entities are not explicitly represented in E/R diagrams! Person name address ssn CS 564 [Spring 2018] - Paris Koutris 8

RELATIONS A mathematical definition: if A, B are sets, then a relation R is a subset of A x B Example A = {1, 2, 3}, B = {a, b, c, d} R = {(1, a), (1, c), (2, c), (3, b)} A 1 2 3 a b c d B CS 564 [Spring 2018] - Paris Koutris 9

RELATIONSHIPS price name category name stockprice Product makes Company makes is a subset of Product x Company Person name address ssn CS 564 [Spring 2018] - Paris Koutris 10

RELATIONSHIPS price name category name stockprice Product makes Company buys Person employs name address ssn CS 564 [Spring 2018] - Paris Koutris 11

MULTIPLICITY OF RELATIONSHIPS one-one A 1 2 3 a b c d B many-one A 1 2 3 a b c d B many-many A 1 2 3 a b c d B CS 564 [Spring 2018] - Paris Koutris 12

MULTIPLICITY OF RELATIONSHIPS price name category name stockprice Product makes Company buys Person employs name address ssn CS 564 [Spring 2018] - Paris Koutris 13

NOTATION DIFFERENCE We use: The cow book uses (page 33): You should use the notation in the slides! CS 564 [Spring 2018] - Paris Koutris 14

MULTI-WAY RELATIONSHIPS How do we model a purchase relation between buyers, products and stores? Product purchase Store Person We can still model this as a mathematical set (ternary relation) CS 564 [Spring 2018] - Paris Koutris 15

ARROWS IN MULTI-WAY RELATIONSHIPS What does the arrow mean here? Product purchase Store Person A given person can purchase a given product from at most one store! CS 564 [Spring 2018] - Paris Koutris 16

ARROWS IN MULTI-WAY RELATIONSHIPS What about here? Product purchase Store Person A given person can purchase a given product from at most one store AND a given store sells to a given person at most one product CS 564 [Spring 2018] - Paris Koutris 17

ARROWS IN MULTI-WAY RELATIONSHIPS How can we say that a given person buys from at most one store? Product purchase Store Person Not possible, we can only approximate! CS 564 [Spring 2018] - Paris Koutris 18

ROLES IN RELATIONSHIPS What if we need an entity set twice in a relationship? Product purchase Store Person salesperson Person buyer CS 564 [Spring 2018] - Paris Koutris 19

ROLES IN RELATIONSHIPS Label the edges to indicate the roles Collapse the two entity sets into one Product purchase Store salesperson buyer Person CS 564 [Spring 2018] - Paris Koutris 20

ATTRIBUTES IN RELATIONSHIPS date Product purchase Store Person Relationships have attributes as well! CS 564 [Spring 2018] - Paris Koutris 21

MULTI-WAY TO BINARY RELATIONSHIPS Product purchase Store productof Product Person Purchase storeof Store Why do we need the arrows? buyerof Person CS 564 [Spring 2018] - Paris Koutris 22

RELATIONSHIPS: RECAP Modeled as a mathematical set Binary and multi-way relationships Converting a multi-way one into many binary ones Constraints on the degree of the relationship many-one, one-one, many-many limitations of arrows Attributes of relationships not necessary, but useful! CS 564 [Spring 2018] - Paris Koutris 23

E/R: ADDITIONAL FEATURES CS 564 [Spring 2018] - Paris Koutris 24

SUBCLASSES subclass = specialized case = fewer entities = more properties Example: Products Software products Educational products CS 564 [Spring 2018] - Paris Koutris 25

SUBCLASSES price name category Product Attributes are inherited by the subclasses! version ISA age group Software Product Educational Product CS 564 [Spring 2018] - Paris Koutris 26

CONSTRAINTS constraint := an assertion about the database that must be true at all times part of the database schema central in database design When creating the ER diagram, you need to find as many constraints as possible! CS 564 [Spring 2018] - Paris Koutris 27

TYPES OF CONSTRAINTS keys: SSN uniquely identifies a person single-value: a person can have only one father referential integrity: if you work for a company, it must exist in the database domain: age is between 0 and 150 other: e.g. at most 80 students enroll in a class CS 564 [Spring 2018] - Paris Koutris 28

WHY DO WE NEED CONSTRAINTS? Give more semantics to the data help us better understand it Prevent wrong data entry Allow us to refer to entities (e.g. using keys) Enable efficient storage and data lookup CS 564 [Spring 2018] - Paris Koutris 29

KEY CONSTRAINTS price name category name stockprice Product makes Company buys Person employs name address ssn CS 564 [Spring 2018] - Paris Koutris 30

KEY CONSTRAINTS Every entity set must have a key! A key can consist of more than one attribute There can be more than one key for an entity set one key will be designated as primary key No formal way to specify multiple keys in an ER diagram CS 564 [Spring 2018] - Paris Koutris 31

SINGLE-VALUE CONSTRAINTS An entity may have at most one value for a given attribute or relationship an attribute of an entity set has a single value a many-one relation implies a single value constraint CS 564 [Spring 2018] - Paris Koutris 32

REFERENTIAL INTEGRITY CONSTRAINT A relationship has one value and the value must exist Product makes Company CS 564 [Spring 2018] - Paris Koutris 33

WEAK ENTITY SETS Entity sets are weak when their key attributes come from other classes to which they are related Team affiliation University number sport name entities of an entity set need help to identify them uniquely! CS 564 [Spring 2018] - Paris Koutris 34

E/R: DESIGN PRINCIPLES CS 564 [Spring 2018] - Paris Koutris 35

1. BE FAITHFUL TO THE APP! What is wrong here? Product purchase Person Country president Person CS 564 [Spring 2018] - Paris Koutris 36

2. AVOID REDUNDANCY! What is wrong here? date Product purchase Store personname personaddress CS 564 [Spring 2018] - Paris Koutris 37

2. AVOID REDUNDANCY! Redundancy occurs when we say the same thing in two different ways Redundancy wastes space and encourages inconsistency The two instances of the same fact may become inconsistent if we change one and forget to change the other CS 564 [Spring 2018] - Paris Koutris 38

2. AVOID REDUNDANCY! What is wrong here? address Beer manfby Manufacturer manufacturer name CS 564 [Spring 2018] - Paris Koutris 39

3. KEEP IT SIMPLE! What is wrong here? Date date Product purchase Store Person CS 564 [Spring 2018] - Paris Koutris 40

4. ATTRIBUTES OVER ENTITY SETS An entity set should satisfy at least one of the following conditions it is more than the name of something; it has at least one non-key attribute it is the many in a many-one or many-many relationship Beer manfby Manufacturer name What is wrong here? name CS 564 [Spring 2018] - Paris Koutris 41

5. DON T OVERUSE WEAK ENTITY SETS Beginner database designers often doubt that anything could be a key by itself They make all entity sets weak, supported by all other entity sets to which they are linked In reality, we create unique IDs for entity sets Examples: SSN, ISBN, CS 564 [Spring 2018] - Paris Koutris 42

E/R TO RELATIONAL MODEL CS 564 [Spring 2018] - Paris Koutris 43

ER MODEL VS RELATIONAL MODEL ER model many concepts: entities, relations, attributes, etc. well-suited for capturing the app requirements not well-suited for computer implementation Relational model has just a single concept: relation world is represented with a collection of tables well-suited for efficient manipulations on computers CS 564 [Spring 2018] - Paris Koutris 44

TRANSLATION Basic cases: entity set E -- > relation with attributes of E relationship R -- > relation with attributes being keys of related entity sets + attributes of R Special cases: combining two relations weak entity sets is-a relationships CS 564 [Spring 2018] - Paris Koutris 45

ENTITY SET TO RELATION Person name address ssn Person(ssn, name, address) CREATE TABLE Person (ssn CHAR(11) PRIMARY KEY, name CHAR(40), address CHAR(50)) CS 564 [Spring 2018] - Paris Koutris 46

ENTITY SET TO RELATION price name category Product Product(name, category, price) CREATE TABLE Product (name CHAR(40), category CHAR(20), price REAL, PRIMARY KEY(name, category)) CS 564 [Spring 2018] - Paris Koutris 47

RELATIONSHIP TO RELATION price name category ssn name Product buys Person date address Product(name, category, price) Person(ssn, name, address) Buys(prodname, prodcategory, ssn, date) CS 564 [Spring 2018] - Paris Koutris 48

MANY-ONE RELATIONSHIPS price name category name stockprice Product makes Company No need for a Makes relation; instead modify Product: Product(name, category, price, company_name) Company(name, stockprice) CS 564 [Spring 2018] - Paris Koutris 49

MANY-MANY RELATIONS price name category ssn name Product buys Person date address Product(name, category, price, ssn) What is wrong here? CS 564 [Spring 2018] - Paris Koutris 50

RELATIONSHIP TO RELATION: SQL CREATE TABLE Buys (prodname CHAR(40), prodcategory CHAR(20), ssn CHAR(11), date DATE, PRIMARY KEY(prodname,prodcategory,ssn) FOREIGN KEY (ssn) REFERENCES Person, FOREIGN KEY (prodname, prodcategory) REFERENCES Product(name, category)) CS 564 [Spring 2018] - Paris Koutris 51

WEAK ENTITY SETS Team affiliation University number sport name Team(number, affiliated-university, sport) Affiliation does not need a separate relation! Attribute name needed as part of the key CS 564 [Spring 2018] - Paris Koutris 52

WEAK ENTITY SETS The relation for a weak entity set must include: attributes for its complete key (including those in other entity sets) its own, non-key attributes A supporting (double-diamond) relationship is redundant and produces no relation CS 564 [Spring 2018] - Paris Koutris 53

SUBCLASSES: OPTION 1 price name category Product version ISA age group Software Product Educational Product Product(name, category, price) SoftwareProduct(name, category, price, version) EducationalProduct(name, category, price, age-group) CS 564 [Spring 2018] - Paris Koutris 54

SUBCLASSES: OPTION 2 price name category Product version ISA age group Software Product Educational Product Product(name, category, price) SoftwareProduct(name, version) EducationalProduct(name, age-group) CS 564 [Spring 2018] - Paris Koutris 55

SUBCLASSES: OPTION 3 price name category Product version ISA age group Software Product Educational Product Product(name, category, price, version, age-group) Use NULL to denote that the attribute makes no sense for a specific tuple CS 564 [Spring 2018] - Paris Koutris 56

SUBCLASSES RECAP Three approaches: 1. create a relation for each class with all its attributes 2. create one relation for each subclass with only the key attribute(s) and attributes attached to it 3. create one relation; entities have null in attributes that do not belong to them CS 564 [Spring 2018] - Paris Koutris 57