CSC 261/461 Database Systems Lecture 8. Spring 2018

Similar documents
CSC 261/461 Database Systems Lecture 8. Fall 2017

CSC 261/461 Database Systems Lecture 10

CSC 261/461 Database Systems Lecture 7

CSC 261/461 Database Systems Lecture 6. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

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

Lecture 4. Lecture 4: The E/R Model

Lecture 5. Lecture 5: The E/R Model

High-Level Database Models (ii)

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

High Level Database Models

MIS Database Systems Entity-Relationship Model.

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

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

Introduction to Database Design

Introduction to Database Systems CSE 414

The Relational Model 2. Week 3

Database Applications (15-415)

Introduction to Database Design

Conceptual Design. The Entity-Relationship (ER) Model

L11: ER modeling 4. CS3200 Database design (sp18 s2) 2/15/2018

CSIT5300: Advanced Database Systems

CSE 530A. ER Model. Washington University Fall 2013

The Entity-Relationship Model

3. Advanced E/R Concepts

OVERVIEW OF DATABASE DEVELOPMENT

Introduction to Databases

The Entity-Relationship Model. Overview of Database Design

Contents. Database. Information Policy. C03. Entity Relationship Model WKU-IP-C03 Database / Entity Relationship Model

Database Management Systems. Chapter 3 Part 2

COMP Instructor: Dimitris Papadias WWW page:

The Relational Model (ii)

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

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

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

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

The Entity-Relationship (ER) Model 2

Database Systems CSE 414

Introduction to Data Management CSE 344

Database Systems CSE 414

3. Advanced E/R Concepts

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Introduction to Data Management CSE 344

Conceptual Design. The Entity-Relationship (ER) Model

Entity-Relationship Diagrams

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

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

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

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

The Entity-Relationship (ER) Model

Entity/Relationship Modelling

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

The Entity-Relationship Model. Steps in Database Design

The Entity-Relationship Model

Database Applications (15-415)

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

Database Design Process Entity / Relationship Diagrams

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

Introduction to Database Systems CSE 414. Lecture 19: E/R Diagrams

The Relational Model. Chapter 3

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

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

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

Review The Big Picture

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

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

Translating an ER Diagram to a Relational Schema

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

CSE 344 JULY 30 TH DB DESIGN (CH 4)

Database Design & Deployment

Introduction to Data Management CSE 344

The Entity-Relationship Model

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

Database Applications (15-415)

Database Systems ( 資料庫系統 )

CSE 344 MAY 14 TH ENTITIES

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

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

Database Systems. Course Administration

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

Conceptual Design with ER Model

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

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Introduction to Data Management CSE 344

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Database Systems CSE 414

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

Database Management Systems. Chapter 2 Part 2

More on the Chen Notation

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

Introduction to Database Design

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

Database Design CENG 351

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

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

COMP 244 DATABASE CONCEPTS & APPLICATIONS

ER to Relational Mapping. ER Model: Overview

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

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

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

Overview of Database Design Process Example Database Application (COMPANY) ER Model Concepts

LECTURE 3: ENTITY-RELATIONSHIP MODELING

ENTITY-RELATIONSHIP MODEL

Transcription:

CSC 261/461 Database Systems Lecture 8 Spring 2018

Announcement Quiz No New Problem Set Study Chapter 5, 6, and 7 Go through the problem set

Announcement Project 2 Part 1 Already out. Workshop covered the basics Objective: Applying SQL queries on real data Secondary Objective: Get proficient with Database Design and ER diagram Project 1 Milestone 2 Structure is closely related to Project 2 Part 1 (though, No SQL coding involved) You should start from scratch (though, keep in mind what you have done in Milestone 1)

Start from Scratch!

What does it mean? This time, start with the ER diagram Feel free to add more entity sets or relations, add more attributes or remove attributes as required. Generate the resultant tables/relations from the ER diagram (NOTE: This tables/relations may be significantly different from what you had come up with in Project 1 Milestone 1. And that is absolutely fine!!! And, that is, in fact, the purpose.

Database Design Process 1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc. Milestone 1 Milestone 2

Q & A How are Project 2 part 1 and Project 1 Milestone 1 related? Project 2 Part 1 and Project 1 Milestone 2 both deals with ER diagram After this week, we will not talk about ER diagram a lot. You need to study ER diagram for the next week s quiz too. Both this projects give you a chance to apply ER diagrams in real world scenarios.

Q & A Piazza: Not many questions are asked! Please avoid marking questions as private

Q & A Reminder: To share any concern you have Please use Feedback form on course website: http://www.cs.rochester.edu/courses/261/spring2018/ -> Forms -> Feedback Form I personally will look into each of your concerns and try my best to resolve any issue

Quiz Collection From where can I collect my quizzes? Mailbox next to my office

Quiz Collection Are they sorted Yes. Sorted by ClassID

The ER (Entity-Relationship) Model

Agenda 1. High-level motivation for the E/R model 2. Entities 3. Relations 4. E/R Model

Mapping natural language Chen proposed the following "rules of thumb" for mapping natural language descriptions into ER diagrams: "English, Chinese and ER diagrams" by Peter Chen. Source: https://en.wikipedia.org/wiki/entity%e2%80%93relationship_mo del

Relationships and Attributes Relationships may have attributes as well. name category since name price Product Makes Company For example: since records when company started making a product Note: since is implicitly unique per pair here! Why?

TYPES OF RELATIONSHIP

Conceptual Crow s Foot Relationship Symbols

Many-Many Relationships Focus: binary relationships, such as Sells between Seller and Buyer. In a many-many relationship, an entity of either set can be connected to many entities of the other set. E.g., a seller sells many items; a buyer can buy many items too.

In Pictures: many-many

Many-One Relationships Some binary relationships are many -one from one entity set to another. Each entity of the first set is connected to at most one entity of the second set. But an entity of the second set can be connected to zero, one, or many entities of the first set. E.g.: One buyer can have multiple order number, but one order can be bought by only one buyer.

In Pictures: many-one

One-One Relationships In a one-one relationship, each entity of either entity set is related to at most one entity of the other set. Example: Relationship president between entity country and person. A person can be the president of only one country. One country can have only one president.

In Pictures

Maximum Cardinality Relationships are named and classified by their cardinalities, which is a word that means count (as in the number of items in a set) Each of the three types of binary relationship shown previously has a different maximum cardinality Maximum cardinality is the maximum number of entity instances that can participate in a relationship instance One, many, or some other positive fixed number

Minimum Cardinality Minimum cardinality is the minimum number of entity instances that must participate in a relationship instance These values typically assume a value of zero (optional) or one (mandatory)

Crow s Foot Symbols with Cardinalities

Cardinality Example Maximum cardinality is many for Order and one for Customer Minimum cardinality is one for both Customer and Order Each customer can place one or more orders Each order is associated with one and only one customer Crow s foot Notation Chen Notation

Entity-Relationship Diagrams The diagrams in previous slides are called entity-relationship diagrams Entities represented by rectangles Relationships represented by lines Cardinalities represented by Crow s Foot symbols

HAS-A Relationships The relationships in the previous slides are called HAS-A relationships The term is used because each entity instance has a relationship to a second entity instance An employee has a locker A locker has an employee There are also IS-A relationships

Different Representations Crow s foot Notation Chen Notation (min,max) constraint

Agenda More about ER model ER model to Relation (Table)

Special Symbols Used Multivalued Attribute Derived Attribute

Strong and Weak Entities A weak entity is an entity whose instances cannot exist in the database without the existence of an instance of another entity Any entity that is not weak entity is called a strong entity Instances of a strong entity can exist in the database independently The weak entity s identifier is a combination of the identifier of the owner entity and the partial key of the weak entity.

In E/R Diagrams name number name Players Playson Teams Double diamond for supporting many-one relationship with Weak Entity. Double rectangle for the weak entity set.

Example: Weak Entity Set name is almost a key for football players, but there might be two with the same name. number is certainly not a key, since players on two teams could have the same number. But number, together with the team name related to the player should be unique.

Partial vs Total Participation An entity set may participate in a relation either totally or partially. Total participation means that every entity in the set is involved in the relationship depicted as a double line. Partial participation means that not all entities in the set are involved in the relationship, e.g., not every professor guides a student depicted by a single line.

Weak Entity, Total Participation and Partial Key

(min, max) constraint Min = 0 implies partial participation Min > 0 implies total participation

Different Representations Crow s foot Notation Chen Notation (min,max) constraint

HAS-A Relationships The relationships in the previous slides are called HAS-A relationships The term is used because each entity instance has a relationship to a second entity instance An employee has a locker A locker has an employee There are also IS-A relationships

Modeling Subclasses Some objects in a class may be special, i.e. worthy of their own class Define a new class? But what if we want to maintain connection to current class? Better: define a subclass Ex: Products Software products Educational products We can define subclasses in E/R!

Modeling Subclasses name price Product isa Child subclasses contain all the attributes of all of their parent classes plus the new attributes shown attached to them in the E/R diagram Software Product Educational Product platforms agegroup

Understanding Subclasses Think in terms of records; ex: Product name price price name Child subclasses contain all the attributes of all of their parent classes plus the new attributes shown attached to them in the E/R diagram SoftwareProduct Product name price platforms EducationalProduct isa name price agegroup Software Product Educational Product platforms agegroup

Think like tables Product name price category Gizmo 99 gadget name Camera 49 photo price Toy 39 gadget Product Sw.Product name platforms isa Ed.Product Gizmo unix Software Product Educational Product name agegroup platforms agegroup Gizmo todler Toy retired

Summary Summary Identifying Relationship

Design Theory (ER model to Relations)

ER Models to Relations Please go through Chapter 9

Entity Sets to Tables name ssn name lot ssn lot 123-22-3333 Alex 23 234-44-6666 Bob 44 567-88-9787 John 12 Employees CREATE TABLE Employees ( ssn char(11), name varchar(30), lot Integer, PRIMARY KEY (ssn))

Relationship Sets (without Constraints) to Tables name since dname ssn lot did budget Employees Works_In Departments address Locations capacity

Relationship Sets (without Constraints) to Tables CREATE TABLE Works_in( ssn char(11), did integer (30), address varchar(30), since date, PRIMARY KEY (ssn, did, address), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (address) REFERENCES Locations, FOREIGN KEY (did) REFERENCES Departments, )

Relationship Sets (without Constraints) to Tables ssn supervisor name lot Employees Reports_To subordinate CREATE TABLE Reports_To( supervisor_ssn char(11), subordinate_ssn char(11), PRIMARY KEY (supervisor_ssn, subordinate_ssn), FOREIGN KEY (supervisor_ssn ) REFERENCES Employees(ssn), FOREIGN KEY (subordinate_ssn ) REFERENCES Employees(ssn) )

Relationship Sets (with key Constraints) to Tables name since dname ssn lot did budget Employees Manages Departments CREATE TABLE Manages( ssn char(11), did integer (30), since date, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments, )

Better way of doing it name since dname ssn lot did budget Employees Manages Departments CREATE TABLE Dept_Mgr( did integer (30), dname varchar(30), budget float(30), ssn char(11), since date, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, )

Relationship Sets (with Participation Constraints) to Tables name since dname ssn lot did budget Employees Manages Departments CREATE TABLE Dept_Mgr( did integer (30), dname varchar(30), budget float(30), ssn char(11), since date, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees ON DELETE NO ACTION )

Translating Weak Entity Sets name cost ssn lot pname age Employees Policy Dependents CREATE TABLE Dept_Policy( pname varchar(30), age integer, cost float, ssn char(11), PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees ON DELETE CASCADE )

Translating Class Hierarchies name ssn lot Hours_worked Employees Contract_id Hourly_wages isa Hourly_Emps Contract Emps

Two options 1. We can map each of the entity sets Employees, Hourly_Emps, and Contract_Emps to a distinct relation. 2. We can create just two relations, corresponding to Hourly_Emps and Contract_Emps Both have their pros and cons Redundancy Performance

E/R Summary E/R diagrams are a visual syntax that allows technical and non-technical people to talk For conceptual design Basic constructs: entity, relationship, and attributes A good design is faithful to the constraints of the application, but not overzealous

Scenario One customer can have at max 2 loans. One loan can be given to multiple customers. What it really means: One customer can have (0,2) loans One loan can be given to (1,n) customer This is a many to many scenario

Different Representations Crow s foot Notation Chen Notation

Acknowledgement Some of the slides in this presentation are taken from the slides provided by the authors. Many of these slides are taken from cs145 course offered by Stanford University.

ACTIVITIES

DRAW AN E/R DIAGRAM FOR FOOTBALL Use the following simplified model of a football season (concepts to include are underlined): Teams play each other in Games. Each pair of teams can play each other multiple times Players belong to Teams A Game is made up of Plays that result in a yardage gain/loss, and potentially a touchdown A Play will contain either a Pass from one player to another, or a Run by one player

Note that various ER diagrams could work, not just the following one! ACTIVITY

Note two copies of the Teams entity here! Team ID date location Teams PlayingAway PlayingHome Games Teams play each other in Games. Each pair of teams can play each other multiple times

Team ID name Player ID date location Teams MemberOf PlayingAway Players PlayingHome Games Players belong to Teams (assume no trades / changes)

Team ID name Player ID date location Teams MemberOf PlayingAway Players PlayingHome Games OccurIn Plays Play ID Yardage Diff Touchdown A Game is made up of Plays that result in a yardage gain/loss, and potentially a touchdown

Team ID name Player ID date location Teams MemberOf PlayingAway Players PlayingHome Games Participation Type Plays OccurIn ParticipateIn Play ID Yardage Diff Touchdown A Play will contain either a Pass from one player to another, or a Run by one player

Note that various ER diagrams could work, not just the following one! ACTIVITY 2

ENHANCE YOUR E/R DIAGRAM! Also make sure to add (new concepts underlined): A player can only belong to one team, a play can only be in one game, a pass/run..? Players can achieve a Personal Record linked to a specific Game and Play Players have a weight which changes in on vs. off-season

Team ID name Player ID date location Teams MemberOf PlayingAway Players PlayingHome Games Participation Type Plays OccurIn A player can only belong to one team, a play can only be in one game, a pass/run..? ParticipateIn Play ID Yardage Diff Touchdown

Team ID name Player ID date location Teams MemberOf PlayingAway Players Participation Type PlayingHome Games ParticipateIn OccurIn Plays PRIn Play ID Yardage Diff Touchdown Players can achieve a Personal Record linked to a specific Game and Play

Team ID name Player ID date location Teams MemberOf PlayingAway Player ID WeightOf Weight Weight Time Period Players PRIn ParticipateIn Participation Type Plays PlayingHome Play ID Yardage Diff Touchdown OccurIn Games Players might have different weights at different times Note: point here is that different players might have different numbers of training / weight phaseshence should represent as new entity!

Note that various ER diagrams could work, not just the following one! ACTIVITY 3

ADD IN: SUBCLASSES, CONSTRAINTS, AND WEAK ENTITY SETS Concepts to include / model: Teams belong to cities- model as weak entity sets Players are either on Offense or Defense, and are of types (QB, RB, WR, TE, K) All passes are to exactly one player; all runs include a player Make sure you have designated keys for all our concepts!

Team Name Teams LocatedIn Cities Name Teams belong to cities- model as weak entity sets

name Player ID Team Name Players MemberOf Teams PlaysPosition LocatedIn Position isa OffensePosition DefensePosition OffensivePosType DefensivePosType Type String Cities Name Players are either on Offense or Defense, and are of types (QB, RB, WR, TE, etc.)