Introduction to Database Systems. Mapping ER Models to Relational Schemas. Werner Nutt



Similar documents
CSC 742 Database Management Systems

2. Conceptual Modeling using the Entity-Relationship Model

COMP 378 Database Systems Notes for Chapter 7 of Database System Concepts Database Design and the Entity-Relationship Model

Databases and BigData

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Chapter 4. Entity: an object that is involved in the enterprise

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #5: En-ty/Rela-onal Models- - - Part 1

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

ER modelling, Weak Entities, Class Hierarchies, Aggregation

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

Relational Schema Design

IV. The (Extended) Entity-Relationship Model

Entity Relationship Diagram

three Entity-Relationship Modeling chapter OVERVIEW CHAPTER

Exercise 1: Relational Model

A Tool for Generating Relational Database Schema from EER Diagram

Introduction to normalization. Introduction to normalization

Database Design Methodology

Converting E-R Diagrams to Relational Model. Winter Lecture 17

CS 377 Database Systems. Database Design Theory and Normalization. Li Xiong Department of Mathematics and Computer Science Emory University

The Entity-Relationship Model

Entity-Relationship Model

Fundamentals of Database System

THE ENTITY- RELATIONSHIP (ER) MODEL CHAPTER 7 (6/E) CHAPTER 3 (5/E)

Chapter 3. Data Modeling Using the Entity-Relationship (ER) Model

not necessarily strictly sequential feedback loops exist, i.e. may need to revisit earlier stages during a later stage

Chapter 7 Data Modeling Using the Entity- Relationship (ER) Model

Chapter 2: Entity-Relationship Model. E-R R Diagrams

Foundations of Information Management

XV. The Entity-Relationship Model

Entity/Relationship Modelling. Database Systems Lecture 4 Natasha Alechina

Database Fundamentals: 1

Conceptual Design Using the Entity-Relationship (ER) Model

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

Why & How: Business Data Modelling. It should be a requirement of the job that business analysts document process AND data requirements

Database Design Process

Lecture 6. SQL, Logical DB Design

Data Analysis 1. SET08104 Database Systems. Napier University

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

Database Design Process

Database Design. Goal: specification of database schema Methodology: E-R Model is viewed as a set of

Normalization in OODB Design

Chapter 10. Functional Dependencies and Normalization for Relational Databases. Copyright 2007 Ramez Elmasri and Shamkant B.

Review: Participation Constraints

Unit 2.1. Data Analysis 1 - V Data Analysis 1. Dr Gordon Russell, Napier University

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Chapter 8 The Enhanced Entity- Relationship (EER) Model

The Entity-Relationship Model

IT2305 Database Systems I (Compulsory)

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Designing a Database Schema

Introduction to Database Systems. Normalization

BİL 354 Veritabanı Sistemleri. Entity-Relationship Model

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

IT2304: Database Systems 1 (DBS 1)

Entity - Relationship Modelling

Lecture 12: Entity Relationship Modelling

Chapter 5: Logical Database Design and the Relational Model Part 2: Normalization. Introduction to Normalization. Normal Forms.

ER & EER to Relational Mapping. Chapter 9 1

SAMPLE FINAL EXAMINATION SPRING SESSION 2015

AVOIDANCE OF CYCLICAL REFERENCE OF FOREIGN KEYS IN DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

Lecture 2 Normalization

COSC344 Database Theory and Applications. Lecture 9 Normalisation. COSC344 Lecture 9 1

CSCI-GA Database Systems Lecture 7: Schema Refinement and Normalization

DATABASE DESIGN. - Developing database and information systems is performed using a development lifecycle, which consists of a series of steps.

SQL DDL. DBS Database Systems Designing Relational Databases. Inclusion Constraints. Key Constraints

Chapter 2: Entity-Relationship Model

Relational Database Concepts

Foundations of Information Management

CIS 631 Database Management Systems Sample Final Exam

Theory of Relational Database Design and Normalization

There are five fields or columns, with names and types as shown above.

Database Design Methodology

Database Design and Normal Forms

3. Relational Model and Relational Algebra

DATABASE DESIGN: NORMALIZATION NOTE & EXERCISES (Up to 3NF)

Enhanced-ER Data Model

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

Databases -Normalization III. (N Spadaccini 2010 and W Liu 2012) Databases - Normalization III 1 / 31

The E-R èentity-relationshipè data model views the real world as a set of basic objects èentitiesè and

Database Design and Normalization

Data Modeling. Database Systems: The Complete Book Ch ,

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

How To Manage Data In A Database System

EECS 647: Introduction to Database Systems

In This Lecture. Security and Integrity. Database Security. DBMS Security Support. Privileges in SQL. Permissions and Privilege.

Normal Form vs. Non-First Normal Form

The Relational Data Model and Relational Database Constraints

Modern Systems Analysis and Design

The Relational Data Model: Structure

Theory of Relational Database Design and Normalization

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

CPS352 Database Systems: Design Project

Lesson 8: Introduction to Databases E-R Data Modeling

Transcription:

Introduction to Database Systems Mapping ER Models to Relational Schemas Werner Nutt 1

Conceptual and Logical Design Conceptual Model: PRODUCT BUYS PERSON price ssn Relational Model: 2

Mapping an E-R Diagram to a Relational Schema We cannot store date in an ER schema (there are no ER database management systems) We have to translate our ER schema into a relational schema What does translation mean? 3

Translation: Principles Maps ER schemas to relational schemas ER instances to relational instances Ideally, the mapping should be one-to-one in both directions not lose any information Difficulties: what to do with ER-instances that have identical attribute values, but consist of different entities? in which way do we want to preserve information? 4

Mapping Entity Types to Relations For every entity type create a relation Every atomic attribute of the entity type becomes a relation attribute Composite attributes: include all the atomic attributes Derived attributes are not included (but remember their derivation rules) Relation instances are subsets of the cross product of the domains of the attributes Attributes of the entity key make up the primary key of the relation given family no. of students courseno equip COURSE studno subject 5

Mapping Entity Types to Relations (cntd.) given family no. of students courseno equip COURSE studno subject (studno, given, family) COURSE (courseno, subject, equip) 6

Mapping Many:many Relationship Types to Relations Create a relation with the following set of attributes: N (degree of relationship) primary keys of each entity type participating in the relationship U primary_key(e i ) U {a 1,,a M } i=1 attributes of the relationship type (if any) given family labmark no. of students courseno equip ENROLLED COURSE studno exammark subject 7

Mapping Many:many Relationship Types to Relations (cntd.) given family labmark no. of students courseno equip ENROLLED COURSE studno exammark subject ENROL(studno, courseno, labmark, exammark) Foreign Key ENROL(studno) references (studno) Foreign Key ENROL(courseno) references COURSE(courseno) 8

Mapping Many:one Relationship Types to Relations studno given family slot roomno m 1 TUTOR STAFF Idea: Post the primary key Given E1 at the many end of relationship and E2 at the one end of the relationship, add information to the relation for E1 The primary key of the entity at the one end (the determined entity) becomes a foreign key in the entity at the many end (the determining entity). Include any relationship attributes with the foreign key entity E1 U primary_key(e2) U {a 1,,a n } relation for entity E1 primary key for E2, is now a foreign key to E2 attributes on the relationship type (if any) 9

Mapping Many:one Relationship Types to Relations: Example studno given family slot roomno m 1 TUTOR STAFF The relation (studno, given, family) is extended to (studno, given, family, tutor, roomno, slot) and the constraint Foreign Key (tutor,roomno) references STAFF(,roomno) 10

Mapping Many:one Relationship Types to Relations (cntd.) studno given family tutor roomno slot s1 fred jones bush 2.26 12B s2 mary brown kahn IT206 12B s3 sue smith goble 2.82 10A s4 fred bloggs goble 2.82 11A s5 peter jones zobel 2.34 13B s6 jill peters kahn IT206 12A STAFF roomno kahn IT206 bush 2.26 goble 2.82 zobel 2.34 watson IT212 woods IT204 capon A14 lindsey 2.10 barringer 2.125 11

Mapping Many:one Relationship Types to Relations (cntd.) studno given family slot roomno m 1 TUTOR STAFF Another Idea: If the relationship type is optional to both entity types, and an instance of the relationship is rare, and there are many attributes on the relationship then create a new relation with the set of attributes: primary_key(e1) U primary_key(e2) U {a 1,,a m } primary key for E1, is now a foreign key to E1; also the PK for this relation primary key for E2, is now a foreign key to E2 attributes on the relationship type (if any) 12

Mapping Many:one Relationship Types to Relations (cntd.) studno given family slot roomno m 1 TUTOR STAFF TUTOR(studno, staff, rommno, slot) and Compare with the mapping of many:many relationship types! Foreign key TUTOR(studno) references (studno) Foreign key TUTOR(staff, roomno) references STAFF(, roomno) 13

Mapping Many:one Relationship Types to Relations (cntd.) studno given family s1 fred jones s2 mary brown s3 sue smith s4 fred bloggs s5 peter jones s6 jill peters TUTOR studno tutor roomno slot s1 bush 2.26 12B s2 kahn IT206 12B s3 goble 2.82 10A s4 goble 2.82 11A s5 zobel 2.34 13B s6 kahn IT206 12A STAFF roomno kahn IT206 bush 2.26 goble 2.82 zobel 2.34 watson IT212 woods IT204 capon A14 lindsey 2.10 barringer 2.125 14

Optional Participation of the Determined Entity ( one end ) A student entity instance must participate in a relationship instance of REG A school entity instance need not participate in a relationship instance of REG given family studno hons faculty REG 1 SCHOOL SCHOOL (hons, faculty) (studno, given, family,??? ) 15

Optional Participation of the Determined Entity studno given family hons s1 fred jones ca s2 mary brown cis s3 sue smith cs s4 fred bloggs ca s5 peter jones cs s6 jill peters ca SCHOOL hons ac is cs ce mi ma faculty accountancy information systems computer science computer science medicine mathematics hons cannot be NULL because it is mandatory for a student to be registered for a school not null constraint No student is registered for mi, so mi doesn t occur as a foreign key value (but that s no problem) 16

Optional Participation of the Determinant Entity ( many end ) studno given family slot roomno m 1 TUTOR STAFF A student entity instance need not participate in a relationship instance of TUTOR A staff entity instance must participate in a relationship instance of TUTOR 17

Optional Participation of the Determinant Entity 1. (studno, given, family, tutor, roomno, slot) STAFF(, roomno) Integrity constraint: π,roomno STAFF \ π tutor,roomno = 2. (studno, given, family) STAFF(, roomno) TUTOR(studno, tutor, roomno, slot) Do we also need an integrity constraint? 18

Optional Participation of the Determinant Entity (cntd.) studno given family tutor roomno slot s1 fred jones bush 2.26 12B s2 mary brown kahn IT206 12B s3 sue smith goble 2.82 10A s4 fred bloggs goble 2.82 11A s5 peter jones zobel 2.34 13B s6 jill peters kahn IT206 12A STAFF roomno kahn IT206 bush 2.26 goble 2.82 zobel 2.34 watson IT212 woods IT204 capon A14 lindsey 2.10 barringer 2.125 19

Optional Participation of the Determinant Entity (cntd.) studno given family tutor roomno slot s1 fred jones bush 2.26 12B s2 mary brown kahn IT206 12B s3 sue smith goble 2.82 10A s4 fred bloggs goble 2.82 11A s5 peter jones NULL NULL NULL s6 jill peters kahn IT206 12A STAFF roomno kahn IT206 bush 2.26 goble 2.82 zobel 2.34 watson IT212 woods IT204 capon A14 lindsey 2.10 barringer 2.125 20

Mapping One:one Relationship Types to Relations year roomno YEAR 1 YEARTUTOR 1 STAFF Post the primary key of one of the entity types into the other entity type as a foreign key, including any relationship attributes with it or Merge the entity types together Which constraint holds in this case? YEAR year yeartutor 1 zobel 2 bush 3 capon STAFF roomno year kahn IT206 NULL bush 2.26 2 goble 2.82 NULL zobel 2.34 1 watson IT212 NULL woods IT204 NULL capon A14 3 lindsey 2.10 NULL barringer 2.125 NULL 21

Multi-Valued Attributes For each multi-valued attribute of E i, create a relation with the attributes primary_key(e i ) U multi-valued attribute The primary key comprises all attributes studno given family studno given family s1 fred jones s2 mary brown contact _CONTACT studno contact s1 Mr. Jones s1 Mrs Jones s2 Bill Brown s2 Mrs Jones s2 Billy-Jo Woods 22

Mapping Roles and Recursive Relationships STAFF roomno appraiser appraisee APPRAISAL How can the entity STAFF appear in both of its roles? STAFF(, roomno, appraiser, approomno ) 23

Multiple Relationships between Entity Types 1. Treat each relationship type separately 2. Represent distinct relationships by different foreign keys drawing on the same relation given family 1 SUPERVISE m studno STAFF 1 EXAMINE m roomno STAFF(, roomno) (studno, given, family,??? ) (studno, given, family,??? ) EXAMINER(??? ) SUPERVISOR(??? ) EXAMINER-SUPERVISOR(??? ) 24

Non-binary Relationship roomno given family m studno STAFF STAFF p TUTORS slot courseno equip n COURSE subject COURSE(courseno, subject, equip) (studno, given, family) STAFF(staff, roomno) TUTORS(??? ) 25

Weak Entities Strong entity type Identifying entity for ORDER Identifying entity for LINE_ITEM CUSTOMER 1 CUST-ORDER c- address Weak entity type Identifying entity for LINE_ITEM 1 m ORDER orderid date ORDER-MAKEUP Weak entity type m LINE_ ITEM lineno quantity 26

Mapping Weak Entities to Relations Create a relation with the attributes: n primary_key(e 0 ) U U discriminator(e i ) U {a 1,,a n } i=1 Primary key of identifying strong entity type Discriminators of identifying weak entity types Attributes of the weak entity type c- CUSTOMER 1 address CUST-ORDER m ORDER orderid date 27

Association Entity Types An entity type that represents a relationship type: given family courseno equip m studno COURSE 1 subject 1 STUD_ENROL m ENROL m COURSE_ENROL labmark exammark 28

Association Entity Types We have: COURSE(courseno, subject, equip) (studno, given, family) given family courseno equip m studno COURSE 1 subject 1 STUD_ENROL m ENROL m COURSE_ENROL labmark exammark Then: ENROL(courseno, studno, labmark, exammark) 29

Translation of the University Diagram studno given family REG 1 hons SCHOOL faculty (studno, given, family, hons, tutor, tutorroom, slot, year) labmark m YEARREG 1 year YEAR ENROL(studno, courseno, labmark,exammark) ENROL TUTOR 1 COURSE(courseno, subject, equip) exammark courseno subject n COURSE equip slot m TEACH YEARTUTOR 1 1 n STAFF 1 m appraiser APPRAISAL appraisee roomno STAFF(lecturer,roomno, appraiser, approom) TEACH(courseno, lecturer,lecroom ) YEAR(year, yeartutor, yeartutorroom) SCHOOL(hons, faculty) 30

Exercise: Supervision of PhD Students A database needs to be developed that keeps track of PhD students: For each student store the and matriculation number. Matriculation numbers are unique. Each student has exactly one address. An address consists of street, town and post code, and is uniquely identified by this information. For each lecturer store the, staff ID and office number. Staff ID's are unique. Each student has exactly one supervisor. A staff member may supervise a number of students. The date when supervision began also needs to be stored. 31

Exercise: Supervision of PhD Students For each research topic store the title and a short description. Titles are unique. Each student can be supervised in only one research topic, though topics that are currently not assigned also need to be stored in the database. Tasks: a) Design an entity relationship diagram that covers the requirements above. Do not forget to include cardinality and participation constraints. b) Based on the ER-diagram from above, develop a relational database schema. List tables with their attributes. Identify keys and foreign keys. 32

Translating of Hierarchies: Options To store information about these classes, We have to define appropriate relations. given family For each relation, we have to define: set of attributes primary key m In principle, there are three options: year d studno thesis title UNDERGRADUATE POSTGRADUATE A. Create a relation for each entity type in the schema, i.e., for both, superclass and subclasses B. Create only relations for subclasses C. Create only one relation, for the superclass 33

Translation into Relations: Option A 1. Create a relation for the superclass 2. For each subclass, create a relation over the set of attributes primary_key(superclass) U attributes of subclass The key for each subclass relation is: primary_key(superclass) Inclusion constraint (foreign keys): π key (superclass) π key (subclass i ) Covering constraint ( n = number of subclasses): n U π key (subclass i ) π key (superclass) i=1 Disjointness constraint: year given family m d studno thesis title π key (subclass i ) π key (subclass j ) = for i j UNDERGRADUATE POSTGRADUATE 34

Translation into Relations: Option B Create only relations for subclasses. Each subclass becomes a relation over the set of attributes: attributes of superclass U attributes of subclass The key for each subclass relation is: primary_key(superclass) Works only if coverage is total and disjoint Partial coverage: entities that are not in a subclass are lost given family m Overlapping classes: redundancy Recovery of the superclass: OUTER UNION on the subclass relations year d studno thesis title Codd: union that extends the schema to all common attributes UNDERGRADUATE POSTGRADUATE 35

Translation into Relations: Option C Create a single relation over the set of attributes n attributes of superclass U attributes of subclass i U { class } i=1 The key is: primary_key(superclass) given family Drawback: many not-applicable nulls Benefit: No need for joins Disjoint coverage: one attribute class which indicates the subclass the tuple represents Overlapping coverage: class has to represent a set of classes Partial coverage: class is null entity is from superclass year UNDERGRADUATE m studno thesis title POSTGRADUATE 36 d

Applying the Three Translations (Overlapping Coverage) payroll no STAFF A. STAFF(payrollno,, lengthofservice) ACADEMIC(payrollno, level) TECHNICAL(payrollno, project) ADMIN(payrollno, grade) length of service level o ACADEMIC TECHNICAL grade ADMIN B. ACADEMIC(payrollno,, lengthofservice, level) TECHNICAL(payrollno,, lengthofservice, project) ADMIN(payrollno,, lengthofservice, grade) C. STAFF(payrollno,, lengthofservice, level, project, grade, class1, class2, class3) or project STAFF(payrollno,, lengthofservice, level, project, grade, class) 37 class = powerset of classes

Specialisation Lattice with Shared Subclass payroll no Staff dept d no_hours title d task Manager salary Hourly Staff Academic Technical Admin Salaried Staff expertise phone Admin Manager Exercise: For each of the approaches A, B, C, decide Which tables need to be created? Which are the attributes? And which are their possible values? 38

References In preparing these slides I have used several sources. The main ones are the following: Books: A First Course in Database Systems, by J. Ullman and J. Widom Fundamentals of Database Systems, by R. Elmasri and S. Navathe Slides from Database courses held by the following people: Enrico Franconi (Free University of Bozen-Bolzano) Carol Goble and Ian Horrocks (University of Manchester) 39