CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

Similar documents
High-Level Database Models (ii)

High Level Database Models

The Entity-Relationship Model

The Entity-Relationship Model. Overview of Database Design

Database Management Systems. Chapter 3 Part 2

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

The Relational Model (ii)

OVERVIEW OF DATABASE DEVELOPMENT

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

The Relational Model. Chapter 3

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

The Relational Model 2. Week 3

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

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

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

Introduction to Database Design

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

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

The Entity-Relationship Model

Database Applications (15-415)

Introduction to Database Design

The Entity-Relationship Model

Database Design & Deployment

The Entity-Relationship (ER) Model 2

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

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 Relational Model. Why Study the Relational Model? Relational Database: Definitions

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

Database Applications (15-415)

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

The Entity-Relationship (ER) Model

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

Introduction to Database Design

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

Database Management Systems. Chapter 2 Part 2

MIS Database Systems Entity-Relationship Model.

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

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

Database Systems ( 資料庫系統 )

Conceptual Design. 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 Design CENG 351

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

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

ER to Relational Mapping. ER Model: Overview

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

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

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

Translating an ER Diagram to a Relational Schema

CIS 330: Applied Database Systems

CSC 261/461 Database Systems Lecture 10

CSIT5300: Advanced Database Systems

Entity-Relationship Diagrams

CSC 261/461 Database Systems Lecture 8. Spring 2018

Database Systems. Course Administration

Conceptual Design. The Entity-Relationship (ER) Model

COMP Instructor: Dimitris Papadias WWW page:

CSE 530A. ER Model. Washington University Fall 2013

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

Database Applications (15-415)

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

The Entity-Relationship Model. Steps in Database Design

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

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

CSC 261/461 Database Systems Lecture 8. Fall 2017

Introduction to Databases

COMP 244 DATABASE CONCEPTS & APPLICATIONS

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

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

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

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

Integrity constraints, relationships. CS634 Lecture 2

Database Management Systems. Session 2. Instructor: Vinnie Costa

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

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

ER Model. Objectives (2/2) Electricite Du Laos (EDL) Dr. Kanda Runapongsa Saikaew, Computer Engineering, KKU 1

Modeling Your Data. Chapter 2. cs542 1

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Problem. Faloutsos - Pavlo CMU SCS /615

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

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

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

The Relational Model

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

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

Chapter 9: Relational DB Design byer/eer to Relational Mapping Relational Database Design Using ER-to- Relational Mapping Mapping EER Model

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

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

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

Database Applications (15-415)

LECTURE 3: ENTITY-RELATIONSHIP MODELING

Database Management Systems. Syllabus. Instructor: Vinnie Costa

The Relational Model

Database Design Process

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

ER to Relational Mapping

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

Roadmap of This Lecture. Weak Entity Sets Extended E-R Features Reduction to Relation Schemas Database Design UML*

Transcription:

RELATIONAL MODEL TO Data Model CONCEPTUAL DESIGN: ER TO RELATIONAL TO How to represent Entity sets, Relationship sets, Attributes, Key and participation constraints, Subclasses, Weak entity sets...? 2 PROBLEM SOLVING STEPS Understand the business rules/requirements Draw the ER diagram Draw the Model Write the and create the database 3

NOTATIONS 4 CROW S FEET Entities Relationships 1-N 1-1 N-N 5 Entity sets are translated to tables. ENTITY SETS ssn name age Employees CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)); 6

RELATIONSHIP SETS Relationship sets are also translated to tables. Keys for each participating entity set (as foreign keys). The combination of these keys forms a superkey for the table. All descriptive attributes of the relationship set. 7 RELATIONSHIP SETS 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); 8 KEY CONSTRAINTS Each dept has at most one manager, according to the key constraint on Manages. name ssn lot Employees since Manages dname did budget Departments Translation to relational model? one-to-one one-to-many many-to-one many-to-many 9

KEY CONSTRAINTS 2 choices Map relationship set to a table Separate tables for Employees and Departments. Note that did is the key now! Since each department has a unique manager, we could instead combine Manages and Departments. 10 KEY CONSTRAINTS Choice 1 Map relationship set to a table Separate tables for Employees and Departments. Note that did is the key now! CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) 11 KEY CONSTRAINTS Choice 2 Since each department has a unique manager Combine Manages and Departments!! since CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) 12

PARTICIPATION CONSTRAINTS We can capture participation constraints involving one entity set in a binary relationship, using NOT NULL. In other cases, we need CHECK constraints. CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, manager CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (manager) REFERENCES Employees, ON DELETE NO ACTION) 13 WEAK ENTITY SETS A weak entity set can be identified uniquely only by considering the primary key of another (owner) entity set. Owner entity set and weak entity set must participate in a one-to-many relationship set (one owner, many weak entities). Weak entity has partial key. It s primary key is made of Its own partial key Primary key of Strong Entity Weak entity set must have total participation in this identifying relationship set. Partial Key ssn name lot cost pname age Employees Policy Dependents 14 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) 15

SUBCLASSES declare A ISA B every A entity is also considered to be a B entity A is a specialization of B Attributes of B are inherited to A. Overlap constraints Can Joe be an Hourly_Emps as well as a Contract_Emps entity? depends Covering constraints Does every Employees entity either have to be an Hourly_Emps or a Contract_Emps entity? depends 16 SUBCLASSES One table for each of the entity sets (superclass and subclasses). ISA relationship does not require additional table. All tables have the same key, i.e. the key of the superclass. E.g.: One table each for Employees, Hourly_Emps and Contract_Emps. General employee attributes are recorded in Employees For hourly emps and contract emps, extra info recorded in the respective relations 17 SUBCLASSES CREATETABLE Employees( ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARYKEY (ssn)) CREATETABLE Hourly_Emps( ssn CHAR(11), hourly_wages REAL, hours_worked INTEGER, PRIMARYKEY (ssn), FOREIGNKEY (ssn) REFERENCES Employees, ON DELETECASCADE) Queries involving all employees easy, those involving just Hourly_Emps require a join to get their special attributes. 18

SUBCLASSES Alternative translation Create tables for the subclasses only. These tables have all attributes of the superclass(es) and the subclass. This approach is applicable only if the subclasses cover the superclass. Queries involving all employees difficult, those on Hourly_Emps and Contract_Emps alone are easy. Only applicable, if Hourly_Emps AND Contract_Emps COVER Employees 19 BINARY VS. TERNARY RELATIONSHIPS The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents. Participation constraints lead to NOT NULL constraints. CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER NOT NULL, PRIMARYKEY (pname, policyid). FOREIGNKEY (policyid) REFERENCES Policies, ON DELETECASCADE) CREATETABLE Policies ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARYKEY (policyid). FOREIGNKEY (ssn) REFERENCES Employees, ON DELETECASCADE) 20 SUMMARY High-level design follows requirements analysis and yields a high-level description of data to be stored. ER model popular for high-level design. Constructs are expressive, close to the way people think about their applications. Basic constructs: entities, relationships, and attributes (of entities and relationships). Some additional constructs: weak entities, subclasses, and constraints. ER design is subjective. There are often many ways to model a given scenario! Analyzing alternatives can be tricky, especially for a large enterprise. 21

SUMMARY There are guidelines to translate ER diagrams to a relational database schema. However, there are often alternatives that need to be carefully considered. Entity sets and relationship sets are all represented by relations. Some constructs of the ER model cannot be easily translated, e.g. multiple participation constraints. 22 Business Rules A Student can take many Courses A Course can be taken by many Students A Student can complete many Assessments An Assessment must be completed by at least one Student A Course must have at least one Assessment An Assessment is for only one Course 23 Want to track information about students Student {StudentId, LastName, FirstName, Sex, Email, HTel, WTel} Course {Code, ShortName, FullName, Description} Assessment {AssessmentNo, Description, Weighting} 24

Business Rules A Student can take many Courses A Course can be taken by many Students A Student can complete many Assessments An Assessment must be completed by at least one Student A Course must have at least one Assessment An Assessment is for only one Course 0:N 0:N 0:N 1:N 1:N 1:1 25 0:N 0:N 0:N 1:N 1:N 1:1 26 Group together tables (formerly entities) and their relationships that have a cardinality of 0:1 or 1:1 27

The remaining relationships whose cardinalities are N (1 :N or 0:N) on both sides become new tables in the new relational model. 28 remaining relationships whose cardinalities are 1:N or 0:N on both sides become new tables in the new relational model. new table usually has a name that is a combined form of the two original table names primary keys from the two tables involved in the relationship become a composite primary key in the new table 29 Create in specific order? 30

Create entities with no dependencies first CREATE TABLE Student ( StudentID BIGINT, LastName VARCHAR(100), FirstName VARCHAR(100), Sex CHAR(1), EMail VARCHAR(100), HTel VARCHAR(20), WTel VARCHAR(20), PRIMARYKEY (StudentID) ); 31 Create entities with no dependencies first CREATE TABLE Course( Code VARCHAR(20), ShortName VARCHAR(100), FullName VARCHAR(100), Description VARCHAR(8000), PRIMARYKEY (Code) ); 32 Create tables dependent on entities. Can we create StudentsAssessments? 33

CREATE TABLE StudentsCourses( Code VARCHAR(20), StudentID BIGINT, PRIMARY KEY (Code, StudentID), FOREIGNKEY (Code) REFERENCES Course, FOREIGNKEY (StudentID) REFERENCES Student); Data types must be identical in all tables referencing the same field! 34 CREATE TABLE Assessment( AssessmentNo INTEGER, Code VARCHAR(20), Weighting DECIMAL(4,2), Description VARCHAR(100), PRIMARY KEY (AssessmentNo), FOREIGNKEY (AssessmentNo) REFERENCES Assessment); 35 CREATE TABLE StudentsAssessments( AssessmentNo INTEGER, StudentID BIGINT, DateGive DATE, Grade DECIMAL(4,2), PRIMARY KEY (AssessmentNo, StudentID), FOREIGNKEY (AssessmentNo) REFERENCES Assessment, FOREIGNKEY (StudentID) REFERENCES Student); 36