Conceptual Design. The Entity-Relationship (ER) Model

Similar documents
Integrity constraints, relationships. CS634 Lecture 2

Conceptual Design. The Entity-Relationship (ER) Model

The Entity-Relationship Model. Overview of Database Design

From ER to Relational Model. Book Chapter 3 (part 2 )

Database Management Systems

The Entity-Relationship Model

High Level Database Models

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

High-Level Database Models (ii)

Lecture 9: Database Design. Wednesday, February 18, 2015

Database Management Systems. Chapter 3 Part 2

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

Database Applications (15-415)

The Entity-Relationship Model

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

The Entity-Relationship Model

MIS Database Systems Entity-Relationship Model.

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

Database Management Systems. Chapter 2 Part 2

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

The Relational Model. Chapter 3

Introduction to Database Design. Dr. Kanda Runapongsa Dept of Computer Engineering Khon Kaen University

OVERVIEW OF DATABASE DEVELOPMENT

The Relational Model 2. Week 3

The Relational Model (ii)

Introduction to Database Design

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

Introduction to Database Design

Database Applications (15-415)

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

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

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

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

The Entity-Relationship Model. Overview of Database Design. ER Model Basics. (Ramakrishnan&Gehrke, Chapter 2)

Introduction to Database Design

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

The Entity-Relationship (ER) Model

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

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

Database Systems ( 資料庫系統 )

Introduction to Data Management. Lecture #3 (Conceptual DB Design) Instructor: Chen Li

Overview of db design Requirement analysis Data to be stored Applications to be built Operations (most frequent) subject to performance requirement

Introduction to Data Management. Lecture #3 (Conceptual DB Design)

CIS 330: Applied Database Systems

Chapter 11 How to create databases, tables, and indexes

SQL Workshop. Database Design. Doug Shook

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

Outer Join, More on SQL Constraints

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

CSE 530A. ER Model. Washington University Fall 2013

The Entity-Relationship (ER) Model 2

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

Introduction to Data Management. Lecture #3 (Conceptual DB Design)

Entity-Relationship Diagrams

Database Applications (15-415)

CSC 261/461 Database Systems Lecture 10

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

ER Model Overview. The Entity-Relationship Model. Database Design Process. ER Model Basics

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

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

CSC 261/461 Database Systems Lecture 8. Spring 2018

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

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Database Systems. Course Administration

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

Database Applications (15-415)

Database Design CENG 351

Database Design & Deployment

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

The Entity-Relationship Model. Steps in Database Design

HNDIT 1105 Database Management Systems

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Modeling Your Data. Chapter 2. cs542 1

ER to Relational Mapping. ER Model: Overview

CSC 261/461 Database Systems Lecture 8. Fall 2017

Translating an ER Diagram to a Relational Schema

COMP Instructor: Dimitris Papadias WWW page:

CS 146 Database Systems

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

Introduction to Databases

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

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

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

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

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

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

VARDHAMAN COLLEGE OF ENGINEERING Shamshabad , Hyderabad B.Tech. CSE IV Semester (VCE - R11) T P C 3+1* -- 4 (A1511) DATABASE MANAGEMENT SYSTEMS

CSIT5300: Advanced Database Systems

Lecture 3: Continuing SQL. Monday, February 2, 2015

SQL Lab Query 5. More SQL Aggregate Queries, Null Values. SQL Lab Query 8. SQL Lab Query 5. SQL Lab Query 8. SQL Lab Query 9

CMPT 354 Database Systems I

Review The Big Picture

Administrivia. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Course Topics. Problem

Lesson 05: How to Insert, Update, and Delete Data. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

SQL Workshop. Introduction Queries. Doug Shook

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

Conceptual Design with ER Model

Introduction to Data Management. Lecture #4 E-R Model, Still Going

SQL Workshop. Joins. Doug Shook

COMP 244. ER-Diagram Notations. Entity-Relationship Diagrams DATABASE CONCEPTS & APPLICATIONS. Database Concepts & Applications 1.

Transcription:

Conceptual Design. The Entity-Relationship (ER) Model CS430/630 Lecture 12 Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke

Relationship Set Representation ssn name lot since did dname budget Employees Works_In Departments Representation Convention: - Relationship sets: diamonds - Edges connect relationship sets to entity sets, and relationship sets to relationship set attributes - E-R with this notation was published by P. Chen in 1976 (at M.I.T. at the time) - Other notations are also in use: Murach uses crow s foot notation 2

Key Constraints How many other entities can an entity have a relationship with? (case of binary relationships) 1-to-1 1-to-Many Many-to-1 N-1 Many-to-Many or N-N 3 Note: many practitioners lump one-to-many and many-to-one into one category many-to-one or N-1 for short, i.e. the many side is not specified by this name. On page 33, sometimes said to be one-to-many is said to be many-to-one

Example 1 Works_In relationship: an employee can work in many departments; a dept can have many employees. many-to-many name since dname ssn lot did budget Employees Works_In Departments 4

Example 2 Manages relationship: each dept has at most one manager one-to-many from Employees to Departments, or many-to-one from Departments to Employees No participation constraint holds here. name since dname ssn lot did budget Employees Manages Departments 5 Arrow for to-one direction of many-to-one, thin because total participation not specified.

Manages relationship visualized: one-tomany, no participation constraint e01 d01 e02 d02 e03 e04 d03 d04 6 Partial Participation Partial Participation Key constraint: each dept has at most one manager Many-to-one from Departments to Employees, so arrow goes right to left.

Participation Constraints Total vs Partial Participation Partial: not every employee is a manager Employees entity set has partial participation, thin line As shown in previous slides Total: every department must have a manager Departments entity set has total participation in relationship Represented as thickened line (there is a key constraint as well, represented by the arrow) ssn name lot since did dname budget Employees Manages Departments 7

Manages relationship visualized: case of total participation by Departments e01 e02 d01 e03 d02 e04 d03 8 Partial Participation Total Participation Key constraint: each dept has at most one manager With total participation by Departments, each dept has exactly one manager.

Example Problem 1 from last class Design a database for a bank, including information about customers and their accounts. Information about customers includes their name, address, phone and SSN. Accounts have numbers, types (e.g., savings/checking) and balances. 1. Draw the E/R diagram for this database. 2. Modify the E/R diagram such that each customer must have at least one account. This is a participation constraint. 3. Modify the E/R diagram further such that an account can have at most one customer, i.e., the relationship is many-to-one from Accounts to Customers. name phone type ssn address number balance Customers Has Accounts 9

Example Problem 2: Class Exercise Design a database for a drugstore, including info on customers and their prescriptions. Information about customers includes their name, phone and SSN. Prescriptions have numbers, drugname, doctor name, and dosage. 1. Draw the E/R diagram for this database. 2. Modify the E/R diagram such that each customer must have at least one prescription. This is a participation constraint. 3. Modify the E/R diagram further such that a prescription has exactly one customer. This is both a participation constraint and a key constraint. 10

Mapping ER to Relational Schemas For most part, process is mechanical Some special cases arise in the presence of constraints Translation from ER to SQL requires: Mapping entity sets to tables Mapping relationship sets to tables (or avoiding this in some cases) Capturing key constraints on relationships (like an account can have at most one customer ) Capturing participation constraints (like each customer must have at least one account ) 11

Entity Sets to Tables ssn name lot Employees CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) Each employee has a unique SSN: this is a key constraint on Employees, an entity table. 12

Relationship Sets to Tables (Sec. 3.5.2) No-constraints case follows simple rules This means primary keys for the entities are known, but no rules such as only one manager for a department (key constraint on relationship) or each customer must have at least one account (participation constraint) Relationship set becomes a relation, attributes include: Keys for each participating entity set (as foreign keys pointing to respective entity table) All descriptive attributes for relationship Primary key of relationship set table is the concatenation of primary keys for the entity sets (assuming no to-one relationships involved) Later we ll see that if the relationship is to-one, the PK can be smaller 13

Relationship Sets to Tables ssn name lot since did dname budget Employees Works_In Departments CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) Binary N-N relationship: 2 entity keys in PK 14

Relationship Sets to Tables: ternary case ssn name lot since did dname budget Employees Works_In2 Departments address Locations capacity CREATE TABLE Works_In3( ssn CHAR(11), did INTEGER, address CHAR(20), since DATE, PRIMARY KEY (ssn, did, address), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments, FOREIGN KEY (address) REFERENCES Locations) Ternary relationship without to-one : 3 entity keys in PK 15

What if there are Key Constraints, e.g., a relationship known to be manyto-one? name since dname ssn lot did budget Employees Manages Departments Each department has at most one manager, according to the key constraint on Manages, shown by the arrow, which follows from the Departments-Employees relationship being many-to-one 16

Each dept has at most one manager : why it s called a key constraint The manages relationship table has (ssn, did) pairs specifying the related entities, and without this to-one constraint, (ssn, did) is the PK of Manages. Given this many-to-one key constraint, we see that one department has at most one (ssn, did) row, i.e., did by itself is a key of Manages. This constraint is called a key constraint because the manages relationship table now has a specified key (a more restrictive one). It s a key constraint on the relationship table. The needed key for Manages is the key of Departments, did. The arrow goes from Departments to Manages, and shows how the key constraint of Departments is now also the key constraint of Manages.

Case of Many-to-one/key constraint Recall from last time: Given a key constraint, or equivalently, its many-to-one expression, there are two major ways to design relational tables: Variant 1: use a relationship table with PK = PK of the "one" side of relationship Variant 2: no relationship table, but instead add column(s) for key attribute(s) of the one-side table and any attributes of the relationship to the many-side table, and make the added key column(s) be a foreign key to the one-side table. Let's look again at the example

Variant 1 for Each department has at most one manager key constraint on Manages Map relationship to its own table: Note that did is the key now! That means we can t have two rows here for one department, pointing to two managers for that department. Note this setup allows a department without a manager: it would simply be missing from this table. CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) 19

Variant 2 for Each department has at most one manager key constraint on Manages Since each department has at most one manager, we could instead combine Manages and Departments. And use a nullable FK on ssn (no participation constraint is given) A department without a manager would have a null ssn 20 CREATE TABLE Dept_Mgr( -- or simply Dept did INTEGER, dname CHAR(20), budget INTEGER, ssn CHAR(11), -- note nullable since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)

Review: Participation Constraints Does every department have a manager? If yes, the participation of Departments in Manages is total Turns out that it is NOT possible to capture this with Variant 1 The Dept_Mgr variant is the only way! 21

Participation Constraints in SQL: every department has one manager ssn name lot since did dname budget Employees Manages Departments CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget INTEGER, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees ON DELETE NO ACTION) 22

Participation Constraints Summary General case (not known to be many-to-one or one-to-many) Total participation cannot be enforced unless we use complex constraints not supported in Oracle What if there is also a key constraint in place? If the entity set with total participation also has a relationship key constraint deserving an outbound arrow from it, then it is possible to capture total participation i.e., if a FK can do the relationship key constraint, a not-null FK can enforce total participation of the entity with the FK in its table. But only if combined table construction is used! 23

Example 1 revisited Design a database for a bank, including information about customers and their accounts. Information about customers includes their name, address, phone and SSN. Accounts have numbers, types (e.g., savings/checking) and balances. 1. Draw the E/R diagram for this database. 2. Modify the E/R diagram such that each customer must have at least one account. This is a participation constraint. 3. Modify the E/R diagram further such that an account can have at most one customer, i.e., the relationship is many-to-one from Accounts to Customers. 4. Now specify tables. 24

Example 2 revisited Design a database for a drugstore, including info on customers and their prescriptions. Information about customers includes their name, phone and SSN. Prescriptions have numbers, drugname, doctor name, and dosage. 1. Draw the E/R diagram for this database. 2. Modify the E/R diagram such that each customer must have at least one prescription. This is a participation constraint. 3. Modify the E/R diagram further such that a prescription has exactly one customer. This is both a participation constraint and a key constraint. 4. Now specify tables. 25

Look at our standard tables Student(snum,sname,major, standing, age) Faculty(fid, fname, deptid) Class(cname, meets_at, room, fid) FK fid to faculty Enrolled(snum, cname) both FKs Enrolled fits the rules for a binary many-to-many relationship table: PK of two entity ids, FKs on them. Make sense: each class has many students, each student has many classes (i.e. can have multiple classes) Class has (nullable) fid column, with FK to faculty. This corresponds to a many-to-one relationship is_taught_by from class to faculty, with no participation constraint on class. Draw the ER diagram

More cases in our standard tables Emp-dept-works: similarly many-to-many relationship works. The FK on managerid in dept indicates a many-to-one relationship manages, and nullable managerid means there is no participation requirement on dept in this. Flights-aircraft-employees-certified: similar many-to-many relationship certified for between employees and aircraft. Suppliers-parts-catalog: similar many-to-many relationship supplies between supplier and part. Unusual prevalence of many-to-many relationships here

Murach Example: The relationships between the tables vendors invoices invoice_line_items vendor_id vendor_name vendor_address vendor_city vendor_state vendor_zip_code vendor_phone vendor_contact_first_name vendor_contact_last_name terms account_no invoice_id vendor_id invoice_number invoice_date invoice_total payment_total credit_total terms invoice_due_date payment_date account_no invoice_id invoice_sequence account_no line_item_description item_quantity item_unit_price line_item_amount This shows two many-to-one relationships (right to left in the diagram), with a crow s foot at the many end. The vendor_id in invoices is a not-null FK The invoice_id in invoice_line_items is a not null FK So there are two many-to-one relationships worthy of the thick arrow in R&G notation. Slide 28 2014, Mike Murach & Associates, Inc. Murach s Oracle SQL and PL/SQL, C9

Example of an entity that looks like a relationship Concept: a sailor reserves a boat for a certain day (pg. 102) Use of a verb as its name makes it sound like a pure relationship, but is it? create table reserves( sid int not null, --key value of sailor table bid int not null, -- key value of boat table day varchar(10), -- day of reservation foreign key (sid) references sailors(sid), foreign key (bid) references boats(bid) ); What is missing from this compared to the N-N relationship table? 29

Example of an entity that looks like a relationship, continued Concept: a sailor reserves a boat for a certain day. One sailor can reserve the same boat for different days. create table reserves( sid int not null, --key value of sailor table bid int not null, -- key value of boat table day varchar(10), -- day of reservation foreign key (sid) references sailors(sid), foreign key (bid) references boats(bid) ); What is missing from this compared to the N-N relationship table???? Answer: Primary key(sid, bid). What is the PK here? Answer: the set of all 3 columns, the PK of last resort. That means it should be a ternary relationship: sailors, boats, days, but days don t have their own entity table Could say days are simple or degenerate entities (no attributes) Another approach: call this table reservation or reservations, using a noun to go with its being an entity, not a pure relationship. 30

Pure relationships can have attributes Concept: a certain part supplied by a certain supplier has a certain cost. create table catalog( sid int, -- key value in suppliers pid int, -- key value in parts cost decimal(10,2), primary key(sid,pid), foreign key(sid) references suppliers(sid), foreign key(pid) references parts(pid) ); Here we see the pure relationship-table pattern, so we can classify this as a pure binary relationship table, N-N, in spite of its noun name. So what s cost doing in here? It s an attribute of the relationship: suppliers supply parts, each with its particular cost. Different suppliers can charge different amounts for the same generic part. The PK (sid,pid) means that for a certain pid and sid, we have just one cost. 31

Catalog: Entity or Relationship? Do we have to call catalog a relationship? No, it s just that we can call it that. Any N-N relationship with attributes can alternatively be considered an entity with two N-1 relationships outbound from it. This flexibility is often used in data modeling environments that don t support attributes of relationships. We can draw two E-R diagrams for this on the board anyway. 32

Weak Entities A weak entity can be identified uniquely only by considering the key of another (owner) entity. Owner entity set and weak entity set must participate in a one-to-many relationship set (one owner, many weak entities). Weak entity set must have total participation in this identifying relationship set. The weak entity set and the identifying relationship set are marked with thickened lines name Partial key ssn lot cost pname age Employees Policy Dependents 33

Translating Weak Entity Sets Weak entity set and identifying relationship set are translated into a single table. When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE) 34

Murach Example: line items are weak entities vendors invoices invoice_line_items vendor_id vendor_name vendor_address vendor_city vendor_state vendor_zip_code vendor_phone vendor_contact_first_name vendor_contact_last_name terms account_no invoice_id vendor_id invoice_number invoice_date invoice_total payment_total credit_total terms invoice_due_date payment_date account_no invoice_id invoice_sequence account_no line_item_description item_quantity item_unit_price line_item_amount This shows two many-to-one relationships, with a crow s foot at the many end. The vendor_id in invoices is a not-null FK The invoice_id in invoice_line_items is a not null FK The PK of invoice_line_items (in bold) contains invoice_id, so invoice_sequence is a partial key Slide 35 2014, Mike Murach & Associates, Inc. Murach s Oracle SQL and PL/SQL, C9

ISA (`is a ) Hierarchies Reasons for using ISA: To add descriptive attributes specific to a subclass. To identify entities that participate in a relationship. If A ISA B, every A entity is also considered to be a B entity and A inherits attributes from B ssn name lot Employees hourly_wages hours_worked ISA contractid Hourly_Emps Contract_Emps 36

ISA is hard to do in a DB Unlike in programming languages, there is no built-in support for inheritance in relational databases. We can use table-supported relationships and end up with multiple tables, hard to understand and query Simplest solution, most common in practice: Use one big table with all attributes of superclass and all subclasses, one PK. Use nullable attributes for attributes like hourly_wages and contractid, so they can be null if not appropriate. Modeling systems (object-relational mapping) can handle complexity of more sophisticated approaches.

Example of use of ISA where a subclass has its own relationship (FYI) First ER diagram OK if a manager gets a separate discretionary budget for each dept. What if a manager gets a discretionary budget that covers all managed depts? Manager has attribute dbudget, relationship to his/her departments. ssn ssn name Employees name Employees ISA lot lot since dbudget Manages1 since Manages2 did did dname budget Departments dname budget Departments Managers dbudget 38

Skipped Sections of R&G From home page, ER box: R&G: Chapter 2 - all except 2.4.5, 2.5.3, 2.5.4, 2.6-2.8; Chapter 3-3.5 (up to 3.5.5) So we re skipping ER aggregation (non-standard dashed box notation), sec. 2.4.5 We re skipping Binary vs. ternary relationships, sec. 2.5.3, 2.5.4. We are concentrating on binary relationships. We re skipping Sec. 3.5.6, Translation of ISA relationships. The book covers two methods, but there are more. It s an advanced topic.

Summary of ER Model Conceptual design follows requirements analysis Yields a high-level description of data to be stored ER model popular for conceptual design Constructs are expressive, similar to human thinking Basic constructs: entities, relationships, and attributes Additional constructs: weak entities, ISA hierarchies 40

Summary of ER Model (contd.) Several kinds of constraints: key constraints participation constraints Constraints play an important role in determining the best database design for an enterprise. ER design is subjective Many ways to model a given scenario! Entity vs. attribute, entity vs. relationship Binary vs. multi-way relationships Whether or not to use ISA hierarchies 41

Oracle SQL Developer Diagrams: Relational Model

Oracle SQL Developer Diagrams: ER Model in Bachman Notation No many-to-many relationships