Mapping ER Diagrams to Relations

Similar documents
Chapter 10. Functional Dependencies and Normalization for Relational Databases

Chapter 10 Functional Dependencies and Normalization for Relational Databases

Theory of Relational Database Design and Normalization

ER & EER to Relational Mapping. Chapter 9 1

Theory of Relational Database Design and Normalization

Functional Dependency and Normalization for Relational Databases

Chapter 5: FUNCTIONAL DEPENDENCIES AND NORMALIZATION FOR RELATIONAL DATABASES

Relational Database Design Theory

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

Relational Normalization: Contents. Relational Database Design: Rationale. Relational Database Design. Motivation

Figure 14.1 Simplified version of the

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

CSC 742 Database Management Systems

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

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

Chapter 15 Basics of Functional Dependencies and Normalization for Relational Databases

Schema Refinement, Functional Dependencies, Normalization

Database Management System

Functional Dependencies and Finding a Minimal Cover

Database Design and Normalization

Limitations of E-R Designs. Relational Normalization Theory. Redundancy and Other Problems. Redundancy. Anomalies. Example

Normalization for Relational DBs

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

CSC 443 Data Base Management Systems. Basic SQL

Why Is This Important? Schema Refinement and Normal Forms. The Evils of Redundancy. Functional Dependencies (FDs) Example (Contd.)

Functional Dependencies and Normalization

Relational Schema Design

Schema Design and Normal Forms Sid Name Level Rating Wage Hours

Week 11: Normal Forms. Logical Database Design. Normal Forms and Normalization. Examples of Redundancy

Design of Relational Database Schemas

Relational Database Design

Relational Database Design: FD s & BCNF

DATABASE NORMALIZATION

2. Conceptual Modeling using the Entity-Relationship Model

10CS54: DATABASE MANAGEMENT SYSTEM

Relational Schema. CS 4700/6700 A Sample of Small Database Design Using Microsoft Access

CS143 Notes: Normalization Theory

Schema Refinement and Normalization

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

Database Management Systems. Redundancy and Other Problems. Redundancy

Chapter 7: Relational Database Design

Database Design and Normal Forms

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

Review: Participation Constraints

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

Normalization in OODB Design

Normalization in Database Design

Functional Dependencies

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

Lecture Notes on Database Normalization

RELATIONAL DATABASE DESIGN

Normalization of Database

Normalisation to 3NF. Database Systems Lecture 11 Natasha Alechina

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

Normalisation. Why normalise? To improve (simplify) database design in order to. Avoid update problems Avoid redundancy Simplify update operations

Limitations of DB Design Processes

3. Database Design Functional Dependency Introduction Value in design Initial state Aims

Database Constraints and Design

Lecture 6. SQL, Logical DB Design

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

Theory behind Normalization & DB Design. Satisfiability: Does an FD hold? Lecture 12

MCQs~Databases~Relational Model and Normalization

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

ER modelling, Weak Entities, Class Hierarchies, Aggregation

Introduction to normalization. Introduction to normalization

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

Lab Assignment Creating a Relational Database Schema from ER Diagram, Populating the Database and Querying over the database with SQL

Chapter 9: Normalization

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

Summary on Chapter 4 Basic SQL

Part 7: Object Oriented Databases

IV. The (Extended) Entity-Relationship Model

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

normalisation Goals: Suppose we have a db scheme: is it good? define precise notions of the qualities of a relational database scheme

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

DATABASE SYSTEMS. Chapter 7 Normalisation

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Lecture 2 Normalization

Chapter 8. Database Design II: Relational Normalization Theory

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

Chapter 6. Database Tables & Normalization. The Need for Normalization. Database Tables & Normalization

Database Design Methodology

DATABASE MANAGEMENT SYSTEMS. Question Bank:

C# Cname Ccity.. P1# Date1 Qnt1 P2# Date2 P9# Date9 1 Codd London Martin Paris Deen London

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

A. TRUE-FALSE: GROUP 2 PRACTICE EXAMPLES FOR THE REVIEW QUIZ:

The Relational Data Model: Structure

{ PreviousEducation ( CollegeName, StartDate, EndDate, { Degree (DegreeName, Month, Year) }, { Transcript (CourseName, Semester, Year, Grade) } ) }

Database Design Methodologies

Databases and BigData

The Entity-Relationship Model

Announcements. SQL is hot! Facebook. Goal. Database Design Process. IT420: Database Management and Organization. Normalization (Chapter 3)

Entity-Relationship Model

Graham Kemp (telephone , room 6475 EDIT) The examiner will visit the exam room at 15:00 and 17:00.

Design Theory for Relational Databases: Functional Dependencies and Normalization

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

Part 4: Database Language - SQL

Databases What the Specification Says

Conceptual Design Using the Entity-Relationship (ER) Model

Chapter 8 The Enhanced Entity- Relationship (EER) Model

Transcription:

CSC 742 Database Management Systems Topic #6: Database Design Spring 2002 CSC 742: DBMS by Dr. Peng Ning Mapping ER Diagrams to Relations Regular Entity Type Create a relation R Include simple attributes and simple components of composite attributes Choose one of the key attributes as the primary key Multi-valued attributes: Don t include in R To be discussed later. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 2

Exercise Fname MI Lname Name Salary SSN Employee Sex Bdate Spring 2002 CSC 742: DBMS by Dr. Peng Ning 3 Mapping ER Diagrams to Relations (Cont d) Weak Entity Type E Create a relation R Include all simple attributes and simple components of composite attributes. Include the primary key of the relation corresponding to the owner entity type of E. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 4 2

Exercise SSN Employee Bdate Dependents_of Dependent Name Sex BDate Spring 2002 CSC 742: DBMS by Dr. Peng Ning 5 Mapping ER Diagrams to Relations (Cont d) Binary : relationship R Suppose S and T are the relations corresponding to the entity types participating R Choose either S (or T), and include the primary of T (or S) as foreign key. Include the simple attributes and the simple components of composite attributes of R. Better to choose the one with total participation in R. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 6 3

Exercise SSN Employee Bdate Manages Name Location Department Since Spring 2002 CSC 742: DBMS by Dr. Peng Ning 7 Mapping ER Diagrams to Relations (Cont d) Binary :N relationship R Suppose S and T are the relations corresponding to the entity types participating R, and S is the N-side. Choose either S, and include the primary key of T as foreign key in S. Include simple attributes and simple components of composite attributes in S. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 8 4

Exercise SSN Employee Bdate Works_for Name Location Department Since Spring 2002 CSC 742: DBMS by Dr. Peng Ning 9 Mapping ER Diagrams to Relations (Cont d) Binary M:N relationship R Suppose S and T are the relations corresponding to the entity types participating R Create a relation U Include in U the primary keys of both S and T as foreign keys. They form the primary key of U. Include the simple attributes and the simple components of composite attributes of R. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 0 5

Exercise SSN Employee Bdate Works_on Hours Project Name Location Spring 2002 CSC 742: DBMS by Dr. Peng Ning Mapping ER Diagrams to Relations (Cont d) Multi-valued attribute A Suppose A is an attribute of the entity type corresponding relation S. Create a relation R Include an attribute corresponding to A and the primary key K of S as a foreign key. The primary key of R is the combination of A and K. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 2 6

Exercise Name Location Department Spring 2002 CSC 742: DBMS by Dr. Peng Ning 3 Mapping ER Diagrams to Relations (Cont d) The n-ary relationship R Create a new relation S Include the primary keys of all the relations corresponding to the participating entity types in R. They form the primary key of S Include the simple attributes and the simple components of composite attributes of R. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 4 7

Exercise SName Supplier PartNo Supply PartNo Part Project ProjName Spring 2002 CSC 742: DBMS by Dr. Peng Ning 5 Mapping EER Diagrams to Relations Subclass-superclass relationships: Commonly four options Assume there are a super-class C and m subclasses {S, S2,, Sm}. Assume the attributes of C are {k, a,, an}, and k is the key attribute. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 6 8

Mapping EER Diagrams to Relations (Cont d) Option Create a relation L for C with all its attributes, and have k as the primary key. For each subclass Si, create a relation Li with attributes k and all the attributes of Si. The primary key of Si is k. Intuition: keep the attributes of the individual classes separately. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 7 Mapping EER Diagrams to Relations (Cont d) Option 2 For each subclass Si, create a relation Li with all the attributes of C and all the attributes of Si. The primary key of Si is k. Intuition: replicate the attributes of the super-class in subclasses. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 8 9

Mapping EER Diagrams to Relations (Cont d) Option 3 Create a single relation with the attributes of the super-class and all the subclasses plus a type attribute. The type attribute is used to indicate the subclass to which each tuple belong. Intuition: store all classes together. For disjoint specialization. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 9 Mapping EER Diagrams to Relations (Cont d) Option 4 Create a single relation with the attributes of the super-class and all the subclasses plus m type attributes. The type attributes boolean attributes indicating whether the tuple belongs to the corresponding subclasses. Intuition: Store all classes together. For overlapping specialization. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 20 0

Exercise Fname MI Lname Name Salary SSN Employee Sex Bdate d Secretary Technican Engineer TypingSpeed TGrade EngType Spring 2002 CSC 742: DBMS by Dr. Peng Ning 2 Mapping EER Diagrams to Relations (Cont d) Multiple inheritance: all superclasses have the same key Spring 2002 CSC 742: DBMS by Dr. Peng Ning 22

Design Guidelines Have schemas that are easy to explain. Keep different entities and relationships apart where possible at least in base relations Prevent anomalies in insertion deletion modification Spring 2002 CSC 742: DBMS by Dr. Peng Ning 23 Employee Name Smith Tom SSN -22-3333 222-33-4444 Bdate 0//7 02/4/68 DNumber Department DName Research DNumber MgrSSN -22-3333 EMP_DEPT Name Smith Tom SSN -22-3333 222-33-4444 Bdate 0//7 02/4/68 DNumber DName Research Research MgrSSN -22-3333 -22-3333 Spring 2002 CSC 742: DBMS by Dr. Peng Ning 24 2

Design Guidelines (Cont d) Avoid NULL values in base relations, although they may occur in views. NULLs should apply rarely and have well-defined meaning: Not applicable unknown absent (known but absent) Prevent spurious tuples Spring 2002 CSC 742: DBMS by Dr. Peng Ning 25 Registration StudentID 2 Name Smith Smith John Course CSC0 CSC02 CSC0 Location V 50 V 00 V 50 Reg StudentID 2 Name Smith Smith John Course CSC0 CSC02 CSC0 Loc StudentID 2 Location V 50 V 00 V 50 Spring 2002 CSC 742: DBMS by Dr. Peng Ning 26 3

Functional Dependencies A constraint R is treated as a set of attributes below For subsets X and Y of R, X Y means that For all relations r of R, (forall t, t2: t, t2 in r (t[x] = t2[x] t[y] = t2[y])) FDs depend on R and its meaning, not on r. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 27 Registration StudentID 2 Name Smith Smith John Course CSC0 CSC02 CSC0 Location V 50 V 00 V 50 Course Location StudentID Name X Y: The vales of the Y component of a tuple are determined by the values of the X component. Alternative statements The values of X functionally determines the values of Y. Y is functionally dependent on X. There is functional dependence from X to Y. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 28 4

Reasoning With FDs: FDs can be inferred from other FDs. Let F be a set of FDs X Y is inferred from F if X Y holds in every relation r that satisfies F (notation: F = X Y) F+ is the set of all FDs that can be inferred from F. Called the closure of F. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 29 Reasoning With FDs: 2 FDs can be inferred based on Their formal definition Armstrong's rules: Reflexivity: If X contains Y, then X Y Augmentation: {X Y} = XZ YZ Transitivity: {X Y, Y Z} = X Z which are provably complete. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 30 5

Reasoning With FDs: 3 Additional Inference Rules: Decomposition rule {X YZ} = X Y. Union rule {X Y, X Z} = X YZ Pseudo-transitive rule {X Y, WY Z} = WX Z. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 3 Find Additional FDs Given a set F of FDs, For each set of attributes X that appears as a left-hand side of an FD in F, Determine the set X + of attributes that are functionally determined by X based on F. X + : the closure of X under F. Algorithm to compute X + under F. X + :=X; Repeat OldX + := X + For each FD Y Z in F do If X + Y then X + := X + Z Until (X + = oldx + ) Spring 2002 CSC 742: DBMS by Dr. Peng Ning 32 6

Minimal Cover Equivalence of sets of FDs Two sets of functional dependencies E and F are equivalent if E + = F +. A set F of FDs is minimal if Every FD in F has a single attribute for the right-hand side We cannot replace any X A with Y A, where Y X, and still have a set of FDs equivalent to F. We cannot remove any FD from F and still have a set of FDs equivalent to F. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 33 Minimal Cover (Cont d) A minimal cover of a set F of FDs is a minimal set of FDs that is equivalent to F. Always exist. Algorithm 4.2 in textbook Step. Step 2. Change the FDs to those with one attribute on the right-hand side. Step 3. Try to remove attributes from the lefthand sides of FDs. Step 4. Try to remove redundant FDs. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 34 7

Normalization A process of cleaning up a schema by decomposing the relations in it to remove various anomalies but additional considerations apply A schema is in some normal form if it satisfies the specified mathematical properties and thereby avoids some potential anomalies Spring 2002 CSC 742: DBMS by Dr. Peng Ning 35 Keys S is a superkey of R = {A,..., An} iff R contains S (forall t, t2: t[s] = t2[s]?) K is a (candidate) key or identifier of R iff K is a superkey (forall L: K contains L?) The primary key is one of the candidate keys. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 36 8

Achtung! Prime attribute member of any key not just the primary key Spring 2002 CSC 742: DBMS by Dr. Peng Ning 37 NF Attributes must be atomic: they can be chars, ints, strings they can t be tuples sets relations composite multivalued Spring 2002 CSC 742: DBMS by Dr. Peng Ning 38 9

Obtaining NF NF is obtained by Splitting composite attributes splitting the relation and propagating the primary key to remove multivalued attributes Spring 2002 CSC 742: DBMS by Dr. Peng Ning 39 Full FD X Y is a full FD if (forall W: (X contains W & W Y) X = W) X Y is a partial FD, otherwise Spring 2002 CSC 742: DBMS by Dr. Peng Ning 40 20

2NF R is in 2NF if every nonprime attribute is fully functionally dependent on every key of R Note that every attribute must be functionally dependent on every key (by definition of a key) Spring 2002 CSC 742: DBMS by Dr. Peng Ning 4 Obtaining 2NF If a nonprime attribute is dependent only on a proper part of a key, then we take the given attribute as well as the key attributes that determine it and move them all to a new relation We can bundle all attributes determined by the same subset of the key as a unit Spring 2002 CSC 742: DBMS by Dr. Peng Ning 42 2

Exercise EMP_PROJ SSN PNumber Hours Ename Pname PLocation FD FD2 FD3 Spring 2002 CSC 742: DBMS by Dr. Peng Ning 43 3NF R is in 3NF if and only if if X A then X is a superkey of R, or A is a prime attribute of R Spring 2002 CSC 742: DBMS by Dr. Peng Ning 44 22

Transitive Dependency X Y is a transitive dependency if (exists Z: Z is not contained in any key of R & X Z and Z Y) Spring 2002 CSC 742: DBMS by Dr. Peng Ning 45 3NF: Alternative Definition R is in 3NF if every nonprime attribute of R is fully functionally dependent on every key of R, and nontransitively dependent on every key of R. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 46 23

Obtaining 3NF Split off the attributes in the FD that causes trouble and move them, so there are two relations for each such FD The determinant of the FD remains in the original relation Spring 2002 CSC 742: DBMS by Dr. Peng Ning 47 Exercise EMP_DEPT Ename SSN BDate Address DNumber DName DMgrSSN Spring 2002 CSC 742: DBMS by Dr. Peng Ning 48 24

BCNF R is in Boyce-Codd Normal Form iff if X A then X is a superkey of R more restrictive than 3NF preferable has fewer anomalies Spring 2002 CSC 742: DBMS by Dr. Peng Ning 49 Obtaining BCNF As usual, split the schema to move the attributes of the troublesome FD to another relation, leaving its determinant in the original so they remain connected not always attainable Spring 2002 CSC 742: DBMS by Dr. Peng Ning 50 25

Exercise Student Course Instructor Spring 2002 CSC 742: DBMS by Dr. Peng Ning 5 Universal Relation: A universal relation is a single giant relation containing the entire database has all attributes (renamed to be unique) has enough tuples with NULL values as appropriate not used in practice only a theoretical construct! Spring 2002 CSC 742: DBMS by Dr. Peng Ning 52 26

Universal Relation: 2 One way to think about DB design is to imagine that we begin with the universal relation and normalize the schema to whatever level we like theoretically interesting partially usable should not be the only tool in one s DB design Spring 2002 CSC 742: DBMS by Dr. Peng Ning 53 27