How to build ER diagrams

Slides:



Advertisements
Similar presentations
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Advertisements

Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
Week 5 Relational Database Design by ER- -to-Relational Mapping.
Functional Dependencies and Normalization for Relational Databases
Defined by Edgar Codd in 1970 Defined by Edgar Codd in 1970 Considered ingenious but impractical Considered ingenious but impractical Conceptually simple.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Normalisation The theory of Relational Database Design.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Functional Dependencies and Normalization for Relational Databases.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Review Database Application Development Access Database Development ER-diagram Forms Reports Queries.
1 Functional Dependency and Normalization Informal design guidelines for relation schemas. Functional dependencies. Normal forms. Normalization.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 7 Relational Database Design by ER- Mapping.
Database Systems Chapter 7 ITM 354. Chapter Outline ER-to-Relational Schema Mapping Algorithm –Step 1: Mapping of Regular Entity Types –Step 2: Mapping.
Chapter 7 Relational Database Design by ER- and EER-to-Relational Mapping Copyright © 2004 Pearson Education, Inc.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Relational Database Design by ER- and EER-to-Relational Mapping.
Chapter 10 Functional Dependencies and Normalization for Relational Databases Copyright © 2004 Pearson Education, Inc.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 6 NORMALIZATION FOR RELATIONAL DATABASES Instructor Ms. Arwa Binsaleh.
Logical DB Design 5. 1 CSE2132 Database Systems Week 5 Lecture Logical Database Design.
Review: Application of Database Systems
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 7 Relational Database Design by ER- to-Relational Mapping.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 7 Relational Database Design by ER- Mapping.
IS 230Lecture 8Slide 1 Normalization Lecture 9. IS 230Lecture 8Slide 2 Lecture 8: Normalization 1. Normalization 2. Data redundancy and anomalies 3. Spurious.
King Saud University College of Computer & Information Sciences Computer Science Department CS 380 Introduction to Database Systems Functional Dependencies.
Functional Dependencies and Normalization for Relational Databases.
Slide Chapter 7 Relational Database Design by ER- to-Relational Mapping.
Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive.
1 Functional Dependencies and Normalization Chapter 15.
Lecture 8: Database Concepts May 4, Outline From last lecture: creating views Normalization.
Chapter 6 Relational Database Design by ER- and EERR-to-Relational Mapping Copyright © 2004 Pearson Education, Inc.
1 Access: Create a Table from a Query, Eg 31 SELECT * INTO SmallCust3 FROM Customer WHERE CreditLimit
Chapter 7 Functional Dependencies Copyright © 2004 Pearson Education, Inc.
ER-TO-RELATIONAL MODEL MAPPING CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMS.
1 © Prentice Hall, 2002 ITD1312 Database Principles Chapter 4B: Logical Design for Relational Systems -- Transforming ER Diagrams into Relations Modern.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Logical Database Design and the Relational Model.
Mapping ER Diagrams to Tables
Deanship of Distance Learning Avicenna Center for E-Learning 1 Session - 7 Sequence - 1 Normalization DB Design Guidelines Presented by: Dr. Samir Tartir.
Lecture (8) 1. Relational Database Design by ER- and EERR- to-Relational Mapping 2.
Al-Imam University Girls Education Center Collage of Computer Science 1 st Semester, 1432/1433H Chapter 10_part 1 Functional Dependencies and Normalization.
Relational Database Design by ER- and EER-to-Relational Mapping
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 First Normal Form (1NF) Unnormalized table : Contains a repeating group –Eg: from multi-valued attributes –Eg: from many-many relationship Table in 1NF:
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Relational Database Design by ER- and EER- to-Relational Mapping.
Chapter 15 1 Functional Dependencies and Normalization for Relational Databases تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة من الادوات في.
Chapter 10 Functional Dependencies and Normalization for Relational Databases Copyright © 2004 Pearson Education, Inc.
Chapter 14 Functional Dependencies and Normalization Informal Design Guidelines for Relational Databases –Semantics of the Relation Attributes –Redundant.
1 Normalization David J. Stucki. Outline Informal Design Guidelines Normal Forms  1NF  2NF  3NF  BCNF  4NF 2.
Relational Database Design by ER- and EER-to-Relational Mapping
Chapter 7 Relational Database Design by ER- and EERR-to-Relational Mapping Copyright © 2004 Pearson Education, Inc.
Chapter 7 Relational Database Design by ER- and EERR-to-Relational Mapping Copyright © 2004 Pearson Education, Inc.
Relational Database Design by ER- and EER-to-Relational Mapping
Relational Database Design by ER- and EERR-to-Relational Mapping
Relational Database Design by ER-to-Relational Mapping
Chapter (9) ER and EER-to-Relational Mapping, and other Relational Languages Objectives How a relational database schema can be created from a conceptual.
Chapter (9) ER and EER-to-Relational Mapping, and other Relational Languages Objectives How a relational database schema can be created from a conceptual.
Translation of ER-diagram into Relational Schema
Mapping ER Diagrams to Tables
From ER to Relational Model
Company Requirements.
Relational Database Design by ER- and EER-to-Relational Mapping
Normalization DB Design Guidelines Presented by: Dr. Samir Tartir
Relational Database Design by ER- and EER-to-Relational Mapping
Relational Database Design by ER- and EER-to- Relational Mapping
Chapter (7) ER-to-Relational Mapping, and other Relational Languages
Relational Database Design by ER-to-Relational Mapping
Presentation transcript:

How to build ER diagrams CS 622/215 Databases How to build ER diagrams Will use an example of a bank database from Elmasri as an example Banks have branches Customers have accounts. May be joint Accounts Possibly multiple accounts. Eg: savings account, checking account Customers may also take out loans Winter 14, Week 7 7

How to build ER diagrams CS 622/215 Databases How to build ER diagrams Identify entities Including weak entities Identify attributes Identify relationships Some attributes might become relationships Determine structure of relationships Partial/total cardinality Winter 14, Week 7 7

How to build ER diagrams CS 622/215 Databases How to build ER diagrams Identify attributes of relationships Might move  attributes of entities Figure out keys Details of attributes Simple/composite Single valued/multi-valued Role names for recursive relationships Winter 14, Week 7 7

Elmasri FIGURE 7.21: ER DIAGRAM FOR A BANK DATABASE © The Benjamin/Cummings Publishing Company, Inc. 1994, Elmasri/Navathe, Fundamentals of Database Systems, Second Edition

ER to Relational: Regular entities CS 622/215 Databases ER to Relational: Regular entities ER to Relational: Given an ER diagram : high level view of database Build a Relational Schema For regular entity E, create a relation R. Attributes ? Will include the attributes of E. Eg: DEPT Primary key in R ? Pick one of the keys of E as the primary key for R. If the chosen key of E is composite ? Then R will also have the same composite key. Winter 14, Week 7 7

Regular entities What are other regular entities ? CS 622/215 Databases Regular entities CREATE TABLE DEPT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, PRIMARY KEY (DNUMBER), UNIQUE (DNAME)); What are other regular entities ? Create tables EMPLOYEE. PROJECT. Primary keys ? EMPLOYEE : SSN PROJECT : PNUMBER Winter 14, Week 7 7

Composite attributes Composite attribute: CS 622/215 Databases Composite attributes Composite attribute: Eg: Name: LName, MI, FName Have as different columns in table CREATE TABLE EMP ( FNAME VARCHAR(15) MINIT CHAR, LNAME VARCHAR(15) SSN CHAR(9)); Winter 14, Week 7 7

Weak Entity Weak entity WK with owner entity OW. How? CS 622/215 Databases Weak Entity Weak entity WK with owner entity OW. How? Create a relation R and include all attributes of WK as attributes of R. PK of R? The PK of R is the combination of the PK of OW and the partial key of the weak entity WK. In addition, include as a foreign key of R the primary key attribute of OW. If OW deleted? When OW is deleted, WK must also be deleted. Winter 14, Week 7 7

Weak Entity CREATE TABLE DEPENDENT ( ESSN CHAR(9) NOT NULL, CS 622/215 Databases Weak Entity CREATE TABLE DEPENDENT ( ESSN CHAR(9) NOT NULL, DNAME VARCHAR(15), SEX CHAR, PRIMARY KEY (DNAME, ESSN), FOREIGN KEY (ESSN) REFERENCES EMPLOYEE (SSN), ON DELETE CASCADE) Winter 14, Week 7 10

Many-many relationship CS 622/215 Databases Many-many relationship many-many relationship R between entities E and F. Eg: WORKSON Can R be represented in the table for E? No: one E can be connected to many F. For same reason, can’t be in table for F. How to do? Create a new table to represent R. What will be the PK of this table? Composite PK: the PK from each table Eg: SSN, PNUMBER Winter 14, Week 7 7

Many-many relationship CS 622/215 Databases Many-many relationship Attributes of relationship type R? Will become columns of table R Eg: HOURS in WORKSON table CREATE TABLE WorksOn ( ESSN CHAR(9), PNO INTEGER, HOURS DECIMAL, PRIMARY KEY (ESSN, PNO), FOREIGN KEY (ESSN) REFERENCES EMPLOYEE (SSN), FOREIGN KEY (PNO) REFERENCES PROJECT(PNUMBER) Winter 14, Week 7 10

One-many relationship CS 622/215 Databases One-many relationship Eg: WORKSFOR 1:N relationship R, between E (1) and F (N). Eg: 1 for DEPT, N for EMP Options? Create a new table or Put R info in the table for F: advantage is we won’t have to look up 2 tables PK will stay the same. Foreign Key to E Include attributes of R in this table Store DNUMBER or DNAME in EMP table? Store PK from other table Winter 14, Week 7 7

One-many, recursive relationship CS 622/215 Databases One-many, recursive relationship CREATE TABLE EMP ( ESSN CHAR(9), BDATE DATE, DNO INTEGER, SUPERSSN CHAR(9), PRIMARY KEY (ESSN), FOREIGN KEY (DNO) REFERENCES DEPT (DNUMBER), FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE (SSN),); Recursive relationship: supervision: is having supervisor in supervisee table better than having supervisee in supervisor table? Yes: one-many Foreign Key to PK in same table Winter 14, Week 7 10

1-1 relationship 1:1 relationship R, between E and F CS 622/215 Databases 1-1 relationship 1:1 relationship R, between E and F Eg: MANAGES ,1 for EMP, 1 for DEPT Create a new table or Put R info in E table or in F table Could put in EMP or DEPT. Which is better? DEPT: every DEPT has a Manager, but not every EMP is a manager. Difference? Won’t need NULLS if in DEPT table Generally put in side of total participation Attribute of R (eg: MgrStartDate) also here Winter 14, Week 7 7

CS 622/215 Databases 1-1 relationship CREATE TABLE DEPT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGRSSN) REFERENCES EMP (SSN) ); Is it possible that even for a 1-1 relationship we might want a separate table? Yes, if only a few instances of relationship and not total on either side. Avoids NULLs. Disadvantage of separate table: lots of JOINS. Winter 14, Week 7 10

Multi-valued attributes CS 622/215 Databases Multi-valued attributes Eg: DEPT_LOCATIONS. How to do? Can it go in DEPT table? No: for one department can have multiple values Needs a separate table. Primary key? PK: DNUMBER, DLOCATION. Foreign key? FK: DNUMBER Multi-valued attributes need separate table Composite PK Winter 14, Week 7 7

FIGURE 3.7 Result of mapping the COMPANY ER schema into a relational schema.

Participation Constraints CS 622/215 Databases Participation Constraints Can we capture total participation in relational schema? Eg: every DEPT has to have a manager Eg: every EMP has to work in a DEPT Can this be done in relational schema ? No : can’t capture this in relational schema . In SQL ? By specifying NOT NULL CREATE TABLE EMP ( ESSN CHAR(9), BDATE DATE, DNO INTEGER NOT NULL); Winter 14, Week 7 7

Ternary relationships: Elmasri Eg CS 622/215 Databases Ternary relationships: Elmasri Eg Elmasri FIGURE 7.17 (a) SUPPLY relationship How to do? Create a new table. Primary keys? PK: composite: the PK of all the 3 entities. FK: to all 3 entities Winter 14, Week 7 7

Elmasri FIGURE 9.4: Mapping the n-ary relationship type SUPPLY

Correspondence between ER and relational models Table 9.1

Build relational schema for the bank database from Figure 7.21 © The Benjamin/Cummings Publishing Company, Inc. 1994, Elmasri/Navathe, Fundamentals of Database Systems, Second Edition

Informal Design Guidelines for Databases CS 622/215 Databases Informal Design Guidelines for Databases Relational database design Create "good" relation schema Criteria for "good" relation schema?  First we discuss informal guidelines. Then discuss formal concepts of functional dependencies and normal forms 1NF (First Normal Form) 2NF (Second Normal Form) BCNF (Boyce-Codd Normal Form) Winter 14, Week 7 7

Informal Guidelines for Relations Basic design principle: Design a schema such that What each table stands for should be easy to explain Semantics of attributes should be easy to interpret/explain. Each tuple in a relation should represent one entity or relationship instance. Attributes of different entities (EMPLOYEE, DEPARTMENT, PROJECT) should not be mixed in the same relation Only foreign keys used to refer to other entities

How to design a database CS 622/215 Databases How to design a database Build the ER diagram. Figure out what data is being stored The connections between different data Translate ER diagram to relational schema keeping informal guidelines in mind Run through normalization checks Winter 14, Week 7 7

Eg of bad relational design CS 622/215 Databases Eg of bad relational design Eg: Students pay same amount for same course Tuition across different courses varies Suppose store information in a single table: Which students take which courses. What is the tuition for a course Howmuch ( studentid, course# , tuition) Stid c# tuition 123 622 1100 234 622 1100 345 640 1200 Problems with this design ? Winter 14, Week 7 10

Eg of bad relational design CS 622/215 Databases Eg of bad relational design Redundancy: tuition repeated for students taking the same course. Problem with redundancy ? Wasted storage. Because of the duplication, can get inconsistency: If we make changes to tuition 622’s tuition 1150 in one place 1100 in another. Winter 14, Week 7 10

Modification Anomalies CS 622/215 Databases Modification Anomalies Modification anomalies – unexpected and undesirable behavior when making changes Update Anomaly: if 622’s tuition changed to 1140 ? Have to make this change in many places. Same information is in multiple rows Deletion Anomaly: if 345 leaves ? We lose tuition info for 640. Lose info we would like to keep Insertion Anomaly: Can insert 632’s fee is 1175 ? No : because no students registered for 632 Can’t store info we would like to store How to fix ? Winter 14, Week 7 7

Relational Design/Modification Anomalies CS 622/215 Databases Relational Design/Modification Anomalies Problem: too much info in one table Has to be split. How to split ? Whotakes( stid,course#), Cost( course#,tuition) Does this solve all the problems: Redundancy Update anomaly Insertion anomaly Deletion anomaly Winter 14, Week 7 10

Non-loss decomposition CS 622/215 Databases Non-loss decomposition Problem: how do we know when a table can be split into two tables without loss of information? Non-loss decomposition: no info loss if table split up what does this mean in specific terms If we break table up, should be able to recover original table with join without spurious tuples new rows which were not there in original table. Lossy decomposition : we lose info when we split table. Eg: spurious tuples when we do join Winter 14, Week 7 10

Non-loss/lossy decomposition eg CS 622/215 Databases Non-loss/lossy decomposition eg (S#,Status,City) broken up into (S#,Status) and (S#, City). Is this a lossy or non-loss decomposition Can recover original table with join. How do we know this ? S# is primary key Non-loss but no advantage of doing this. Disadvantage ? Need a join if we wanted to answer: Which cities have S with status 20 Winter 14, Week 7 10

Non-loss/lossy decomposition eg CS 622/215 Databases Non-loss/lossy decomposition eg (S#,Status,City) broken up into : (S#,Status) and (Status, City). S Status City S1 10 SF S2 20 NY S3 20 LA S Status S1 10 S2 20 S3 20 Status City 10 SF 20 NY 20 LA Will the join (over status) of the two new tables give back the original table? No: join will also have spurious tuples S2 20 LA S3 20 NY Winter 14, Week 7 10

Normalization Want to design/modify tables to control redundancy CS 622/215 Databases Normalization Want to design/modify tables to control redundancy To avoid modification anomalies and wasted storage. This process is called Normalization Look at a given table structure and see if it can be improved. Is there potential for anomalies because too much info in one table ? How to break up ? What do we want ? Non-loss decomposition Any downside to normalization? Winter 14, Week 7 10

CS 622/215 Databases Normalization By splitting up tables, may lead to loss of efficiency. Why ? Because more joins needed Normalization is a guide Don’t always have to follow But have to understand Works through stages, called Normal Forms each is stronger than the previous i.e. the higher the normal form, the stronger the guarantee of less redundancy. Winter 14, Week 7 10