Modeling: Entity-Relationship Diagrams

Slides:



Advertisements
Similar presentations
High-Level Database Models Spring 2011 Instructor: Hassan Khosravi.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
BUSINESS DRIVEN TECHNOLOGY Plug-In T4 Designing Database Applications.
Ch5: ER Diagrams - Part 1 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Text-Book Chapters (7 and 8) Entity-Relationship Model
Design Principles: Faithfulness
Design Principles: Faithfulness
CS157A Lecture 3 ER Diagram Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Databases Revision.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
System Concepts and Architecture Rose-Hulman Institute of Technology Curt Clifton.
--The Entity Relationship Model(2)--1 Reflexive Rela1tionships  An entity may be related to another entity of the same type.  Example: A Customer could.
Lecture 2: Entity/Relationship modelling
1 Database Course. 2 General Information TAs: –Gideon Rothschild, office hours: Sun 16:00- 17:00 at Ross 109 –Aron Matskin, office hours: TBA Course Homepage:
The Entity-Relationship Data Model
1 Database Course. 2 General Information TAs: –Jonathan Mamou –Gideon Rothschild Course Moderated Newsgroup: local.course.db.ta.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Entity-Relationship Model Database Management Systems I Alex Coman, Winter.
1 The Entity-Relationship Data Model Chapter 2 (Database Design)
Chapter 2: Entity-Relationship Model (Continued)
APPENDIX C DESIGNING DATABASES
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
Database Systems Entity Relationship Diagrams.
CS411 Database Systems Kazuhiro Minami
Chapter 3 Data Modeling Using the Entity- Relationship (ER) Model Dr. Bernard Chen Ph.D. University of Central Arkansas.
The Entity-Relationship Model. 421B: Database Systems - ER Model 2 Overview of Database Design q Conceptual Design -- A first model of the real world.
Ch5: ER Diagrams - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Module Title? DBMS E-R Model to Relational Model.
Database Processing: Fundamentals, Design and Implementation, 9/e by David M. KroenkeChapter 2/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Concepts and Terminology Introduction to Database.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
Database Systems Lecture # 7 8 th Feb, Conceptual and Logical Design Person buys Product name pricenamessn Conceptual Model: Relational Model: (plus.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Entity-Relationship Model Entity Sets Relationship Sets Design Issues Mapping.
1 Relational Databases and SQL. Learning Objectives Understand techniques to model complex accounting phenomena in an E-R diagram Develop E-R diagrams.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts DB Schema Design: the Entity-Relationship Model What’s the use of the E-R model? Entity Sets.
Entity-Relationship Model Using High-Level Conceptual Data Models for Database Design Entity Types, Sets, Attributes and Keys Relationship Types, Sets,
Initial Design of Entity Types for the COMPANY Database Schema Based on the requirements, we can identify four initial entity types in the COMPANY database:
Entity-Relationship Modeling
CS 405G: Introduction to Database Systems Lecture 2 : Database Design I.
Chapter 2 : Entity-Relationship Model Entity Sets Relationship Sets Design Issues Mapping Constraints Keys E-R Diagram Extended E-R Features Design of.
 Entity-relationship models (ERM) Entity-relationship models (ERM)  Simple E-R Diagram Simple E-R Diagram  Weak Entity Weak Entity  Strong Entity.
Tallahassee, Florida, 2015 COP4710 Database Systems E-R Model Fall 2015.
© D. Wong Ch. 2 Entity-Relationship Data Model (continue)  Data models  Entity-Relationship diagrams  Design Principles  Modeling of constraints.
Entity-Relationship Model
Entity Relationship Diagram (2)
CPSC 603 Database Systems Lecturer: Laurie Webster II, M.S.S.E., M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 2 Introduction to a First Course in Database Systems.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan Lecture-03 Introduction –Data Models Lectured by, Jesmin Akhter.
Databases Illuminated Chapter 3 The Entity Relationship Model.
Home Work. Design Principles and Weak Entity Sets.
1 Database Course. 2 General Information TAs: –Sara Cohen –Jonathan Mamou Course Moderated Newsgroup: local.course.db.ta Students.
Computing & Information Sciences Kansas State University Friday, 26 Sep 2008CIS 560: Database System Concepts Lecture 13 of 42 Friday, 26 September 2008.
Database Management Systems Course Faculty of Computer Science Technion – Israel Institute of Technology Lecture 2: Entity-Relationship Diagrams.
advanced data modeling
CS 157B Database Systems Dr. T Y Lin. Updates 1.Red color denotes updated data (ppt) 2.Class participation will be part of “extra” credits to to “quiz.
CPSC 603 Database Systems Lecturer: Laurie Webster II, M.S.S.E., M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 4 Introduction to a First Course in Database Systems.
The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006.
1 The Entity- Relationship Model Instructor: Mohamed Eltabakh Part-2.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Entity-Relationship Model Entity Sets Relationship Sets Mapping Constraints Keys.
©Silberschatz, Korth and Sudarshan7.1Database System Concepts - 6 th Edition Chapter 7: Entity-Relationship Model.
Lecture 26 Enterprise Systems Development ( CSC447 ) COMSATS Islamabad Muhammad Usman, Assistant Professor.
Database Management Systems Course Faculty of Computer Science Technion – Israel Institute of Technology Lecture 2: Entity-Relationship Diagrams.
CS422 Principles of Database Systems Entity-Relationship Model Chengyu Sun California State University, Los Angeles.
High-level Database Models Prof. Yin-Fu Huang CSIE, NYUST Chapter 4.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
The Entity-Relationship Diagram for Data Modeling
1 What is a Database Management System? Lecture slides by Dr. Sara Cohen Spring 2008.
ER Diagrams and Relational Model CS 174a (Winter 2015)
Databases Winter 2011.
COP Introduction to Database Structures
Entity-Relationship Model
Presentation transcript:

Modeling: Entity-Relationship Diagrams

Scenario http://www.imdb.com wants to store information about movies and has chosen you to help them Four steps: Requirements Analysis: Discover what information needs to be stored, how the stored information will be used, etc. Taught in course on system analysis and design Conceptual Database Design: High level description of data to be stored (ER model) Logical Database Design: Translation of ER diagram to a relational database schema (description of tables) Physical Database Design: Done by the DB system

Requirements For actors and directors, we want to store their name, a unique identification number, address and birthday (why not age?) For actors, we also want to store a photograph For films, we want to store the title, year of production and type (thriller, comedy, etc.) We want to know who directed and who acted in each film. Every film has one director. We store the salary of each actor for each film Etc…

ER-Diagrams: General Information ER-diagrams are a formalism to model real-world scenarios There are many versions of ER-diagrams that differ both in their appearance and in their meaning We will use the version appearing in the book Database Systems: The Complete Book ER-diagrams have a formal semantics (meaning) that must be thoroughly understood, in order to create correct diagrams Goal of modeling is to translate informal requirements to a precise diagram. This diagram can then be translated into to the desired data model, to allow data to be stored in a database

Basic Concepts: Entities, Attributes, Relationships

Entities, Entity Sets Entity (ישות): An object in the world that can be distinguished from other objects Examples of entities: Examples of things that are not entities: Entity set (קבוצת ישויות): A set of similar entities Examples of entity sets:  Entity sets are drawn as rectangles Examples of entities: Bill Gates, Database course in HUJI Examples of entity sets: People, Courses Actor

Attributes Attributes (תכונות): Used to describe entities All entities in the set have the same attributes A minimal set of attributes that uniquely identify an entity is called a key An attribute contains a single piece of information (and not a list of data)

Attributes (2) Examples of attributes: Examples of things that cannot be attributes:  Attributes are drawn using ovals  The names of the attributes which make up a key are underlined Attribute example: age, birthday, id number, name Not attribute: spouse (may be missing), children (can be multiple)

Example birthday id Actor name address

Another Option for a Key? birthday id Actor name address

Another Option for a Key? birthday id Actor name address

Relationships, Relationship Sets Relationship (קשר): Association among two or more entities Examples of Relationships: Relationship Set (קבוצת קשרים): Set of similar relationships Examples of Relationship sets:  Relationship sets are drawn using diamonds Relationship example: Bill Gates is married to Melinda French, Shimon Levy is taking the HUJI DB course Relationship set example: Marriage, Studying

Example id Actor Film Produced title name

Recursive Relationships An entity set can participate more than once in a relationship In this case, we add a description of the role to the ER-diagram phone number manager id Employee Manages worker name address

n-ary Relationship An n-ary relationship set R involves exactly n entity sets: E1, …, En. Each relationship in R involves exactly n entities: e1 in E1, …, en in En Formally, R E1x …x En Actor id name Directed Film title Director

Example Suppose that there are: 2 Actors 3 Directors 4 Film How many triples can be in the relationship set “Directed”? How many pairs can be in the relationship set “Directed”? Actor id name Directed Film title Director No pairs! Directed contains triples At most 2*3*4 = 24 triples

Multiplicity of Relationships

Multiplicity of Relationships A member of E may be connected by R to any number of members from F, and vice versa This is called a many-many relationship

Many-to-Many A film is directed by any number of directors A director can direct any number of films id Director Film Directed title name Director Directed Film

Multiplicity of Relationships By adding arrows to the diagram, we can indicate constraints on the relationship An arrow towards F indicates that: A member of E may be connected by R to at most one members from F (Still, a member of F may be connected by R to any number of members from E) This is called a many-one relationship

One-to-Many A film is directed by at most one director A director can direct any number of films id Director Film Directed title name Director Directed Film

Multiplicity of Relationships An arrow towards F and towards E indicates that: A member of E may be connected by R to at most one member from F A member of F may be connected by R to at most one member from E This is called a one-one relationship

One-to-One A film is directed by at most one director A director can direct at most one film id Director Film Directed title name Director Directed Film

Example Where would you put the arrow? age id Student University StudiesAt name Towards University name

Another Example Where would you put the arrow? age father id Person FatherOf child On the top line name

Multiplicities in Multiway Relationships F1 E1 … R … En Fm For any 1<=i<=m For any tuple of entities e1,…,en,f1,…fi-1,fi+1,...,fm there is at most one fi, such that e1,…,en,f1,…,fm are connected by R

Example C A R B D What is the maximum number of 4-tuples in R? Suppose that there are a entities in A b entities in B c entities in C d entities in D Maximum number min(abc,abd) Minimum number = 0 What is the minimum number of 4-tuples in R?

Note that for many reasons, this is a bad modeling Example Man id name id Child Woman childOf id name name Each man and woman can have a single child together. (However, a man/woman can have many children, each with a different woman/man) Note that for many reasons, this is a bad modeling What does this mean?

Multiplicities in Multiway Relationships Man id name id Child Woman childOf id name name Each man and woman can have a single child together. (However, a man/woman can have many children, each with a different woman/man) Note that for many reasons, this is a bad modeling Each pair of a man and a woman can have at most one child

Referential Integrity and Degree Constraints

Referential Integrity So far, we can say that an entity participates at most one time, but cannot require it to participate at least one time The rounded arrow above indicates that each entity in E must participate exactly one time in an R-relationship with an entity in F

Degree Constraints StarsIn Actor Movie <=10 Actor Movie We can attach a bounding number to edges to indicate limits on the number of entities that can be connected to a single entity via a relationship set In the example above, a move has at most 10 stars Note: a regular arrow is the constraint <=1 Note: a rounded arrow is the constraint =1

Example (1) What does this mean? age id Student University StudiesAt name Every student studies in exactly one university name

Note that for many reasons, this is a terrible modeling Example (2) Man id name id Child Woman childOf id name name Every possible pair of man and woman have together exactly one child Note that for many reasons, this is a terrible modeling What does this mean?

Relationship Sets with Attributes

Relationship Sets Can have Attributes title birthday id Actor Film Acted In year name type address It is an attribute of the relationship. If we attached salary to actor, this would imply that actors have a set salary, regardless of the movie that they are playing in. If we attached salary to film, this would imply that all actors in a film have the same salary. salary Where does the salary attribute belong?

Important Note The entities in a relationship set must identify the relationship Attributes of the relationship set cannot be used for identification! Suppose we wanted to store the role of an actor in a film. How should we store the role of the actor? How would we store information about a person who acted in one film in several roles? If the actor can have several roles, then we must use a third entity set! id Actor Film Acted In title name

Subclasses

ISA Hierarchies ISA Relationships: Defines a hierarchy between entity sets ISA is similar to inheritance  ISA relationships are drawn as a triangle with the word ISA inside it. The "super entity-set" is above the triangle and the "sub entity-sets" are below

Implications of an ISA Relationship Every entity in B or in C belongs to A There may be entities in A that do not belong to B or to C There may be entities that belong to both B and C A isa B C

Example What are the keys of: Movie Person Actor Director address id birthday Movie Person name ISA The key of all of these is id picture Actor Director

Example Is this good method of modeling data parent woman ParentOf child Person man Married name id No! Same-sex marriages are allowed (outlawed by the rabanut, though). A person can have any number of parents. To fix: Create an entity set person, and use isa to define woman and man. Then, have married as a relationship between woman and man. Have a mother-of and father-of relationship from woman and man to person. Is this good method of modeling data for the רבנות database on marriage? How can you fix it?

Weak Entity Sets

Intuition Sometimes, entities cannot be identified by their own attributes. To identify such an entity, we need information about a “supporting relationship” Example: Given a bank account number, you cannot identify the actual bank account. For identification, you also have to know the name of the bank.

Example A פלוגה has a letter (א, ב, ...) To uniquely identify the פלוגה, one must know which גדוד it belongs to פלוגה is a weak entity set The relationship set שייכת ל is the supporting relationship for פלוגה גדוד מספר גדוד שייכת ל פלוגה אות פלוגה

Notation Weak entity set has a double line Supporting relationships have double lines Rounded arrow pointing into the identifying entity sets גדוד מספר גדוד שייכת ל פלוגה אות פלוגה

Example (1) Keys: גדוד: מספר גדוד פלוגה: אות פלוגה, מספר גדוד מחלקה: מספר מחלקה, אות פלוגה, מספר גדוד מספר גדוד שייכת ל פלוגה אות פלוגה שייכת ל מחלקה מספר מחלקה

Example (2) Same award can be given by several organizations (“Academy award for Best Actor 2007”), A year, award name and organization name uniquely define an award Weak entity set can participate in additional (non-supporting) relationships phone number name Organization Gives Won Award Actor year name

Example (3) Awards are now identified by organization and country Same award can be given by same organization in different countries (“Academy award for Best Actor Israeli 2007”) Weak identity set has 2 supporting relationships What is identifying key for award? phone number name name Organization Country Gives Location Award Year, award name, country name, organization name year name

Design Principles

Faithfulness The design should be accurate to the specifications This is ok only if each actor has a set salary, regardless of all movies Actor Film Acted In salary

Avoiding Redundancy The design should not model the same information in multiple ways Leads to fact repetition Leads to inconsistencies Movie Studio Owns studioName name

Simplicity Counts Avoid introducing elements that are not needed If we never need to store information about people that are not movie people, don’t put it in the diagram address id birthday Person name isa Movie Person isa picture Actor Director

Picking the Right Kind of Element Actor Studio Owned By name The bottom diagram is sufficient if Studio has no attributes other than name Actor sname

Summary Given a set of requirements, to translate the requirements into a diagram: Identify the entity sets Determine if there are hierarchies (ISA or weak relationships) among entity sets Identify the relationship sets Identify the attributes Determine constraints on relationship participation

The Relational Model

Data Models A data model is a notation for describing data Conceptual structure of the data Operations on the data Constraints on the data In this course we focus on the relational data model

Conceptual Structure of the Data The basic element of the relational model is a relation (which is similar to a table) A relation has a schema, consisting of a Name List of attributes, possibly with domains A relation may also have an instance, which is a set of tuples (i.e., rows) in the relation

Example Schema: Movies(title, year, length, genre) Follow… 1985 90 children Who … 1987 mystery Example Schema: Movies(title, year, length, genre) Relation name: Movies Attributes: title, year, length, genre Possible tuple instance (“Follow that Bird”, 1985, 90, children) Scheme with domains: Movies(title: string, year: number, length: number, genre: string)

Operations on the Data Relational algebra Selection, projection, union, minus, join Stay tuned… Discussed in detail next week

Constraints on the Data We discuss complex constraints later on in the course For now, we introduce key constraints A set of attributes forms a key for a relation if there cannot be 2 different tuples with the same values for all attributes of the key Noted with underline Examples: Movies(title, year, length, genre) Actor(teudatZehut, name, address)

A Step Closer Once we have a set of relational schemas in the relational model, we are a step closer to storing data in a DBMS A DBMS has a data definition language (DDL), used to define tables in the database Once we have decided on the relational schemas, these can be directly translated into the database using the DDL

ER Diagrams to Relational Schemas

General Principles When converting ER diagrams to Relations, we should: Reduce duplicate information Constrain as tightly as possible Notes: Some scenarios can be represented in different ways. Sometimes we will not be able to fully represent constraints, or will be forced to represent information more than once.

Entity Set Translation birthday id Actor name address Actor (id, name, birthday, address) General Rule: Create a relation with the name of the Entity. There is a column for each attribute The key in the diagram is the primary key of the relation

Relationship Sets (without constraints) birthday title id Actor Film Acted In year name salary address type General Rule: Create a table with the name of the relationship set Relationship table attributes: its own attributes (salary) + all keys of the relating entities (title, id) What is the primary key of the table? Note: Do not define two attributes with the same name – instead rename one of them in the schema Id, title

Translating Recursive Relationship Sets (without constraints) manager id Employee Manages worker name address What are all the relations created for this diagram? Employee(id,name, address) – id is the key Manages(mid,wid) – mid,wid is the key

Translating relationships (one-to-many): Option 1 id Director Film Directed title name salary year Option 1: Same as without key constraints (3 tables), except that the primary key of Directed is now title (why?)

Translating relationships (one-to-many): Option 2 id Director Film Directed title name salary year Film (title, year, salary, id) Director(id, name) Option 2: Do not create a table for the relationship Add information columns that would have been in the relationship's relation to the relation of the entity which does not have the incoming arrow

Translating Weak Entity Sets phone number Key of relation for weak entity set includes its own keys and the keys of its supporting entity sets No relation is created for the supporting relationship sets Example: Translate diagram on the left to relations name Organization Gives Award Organization(name,phone) – name is key Award(year,name,oname) – all are key year name

Translating ISA: E/R Style Conversion address Translating ISA: E/R Style Conversion id Movie Person name ISA picture Actor Director A relation for each entity set. An entity may appear in more than one relation MoviePerson(id, address, name) Actor(id, picture) Director(id)

Translating ISA: Object-Oriented Approach address Translating ISA: Object-Oriented Approach id Movie Person name ISA picture Actor Director A relation for each possible combinations of how entities may appear in the entity sets. MoviePerson(id, address, name) MoviePersonActor(id, address, name, picture) MoviePersonDirector(id, address, name) MoviePersonActorDirector(id, address, name, picture)

Translating ISA: Null Value Approach address Translating ISA: Null Value Approach id Movie Person name ISA picture Actor Director A single relation for containing all values Possible only if NULL values (i.e., missing values) are allowed – not in the pure relational model MoviePerson(id, address, name, picture)