Introduction and Overview Entity Relationship (ER)...

73
CS 461: Database Systems Introduction and Overview Entity Relationship (ER) Modeling supplementary material: “Database Management Systems” Ch. 1, 2.1-2.5 class notes Julia Stoyanovich ([email protected])

Transcript of Introduction and Overview Entity Relationship (ER)...

Page 1: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

CS 461: Database Systems

Introduction and OverviewEntity Relationship (ER) Modeling

supplementary material: “Database Management Systems” Ch. 1, 2.1-2.5

class notes

Julia Stoyanovich ([email protected])

Page 2: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Instructor: Julia Stoyanovich • Assistant Professor of Computer Science @ Drexel

• 2009-2012: Postdoc, Visiting Scholar @ UPenn

• 2009: PhD in CS @ Columbia University

• 1998-2003: engineer at start-ups in New York City

• 1998: BS in CS & Math @ UMass Amherst

Research: data and knowledge management

• Data, responsibly: Fairness, accountability and transparency in data science

• Portal: Querying and analysis of evolving graphs: the Web, social networks

• DB4Pref: Managing preference data: political polls, movie rating etc

Page 3: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Logistics• Website: https://www.cs.drexel.edu/~julia/cs461

• BBLearn: discussion board, lecture videos, grade center

• This is a fast-paced course, I cannot accept late assignments

• My office hours: W 4-5pm (UC 100E) of by appointment

• Accounts: tux, PostgreSQL, gitlab

• You must have at least a basic familiarity with Linux, see https://www.cs.drexel.edu/~julia/cs461/resources.html for tutorials, contact the TA with any questions

Page 4: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

TA: Haoyue Ping

• Office Hours: Thursdays 2-4pm in CLC (UC 152) or by appointment

• Email: [email protected]

• Schedule a meeting to discuss: ssh, git, PostgreSQL, Linux, project set-up

Page 5: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Textbook

3rd Edition (earlier editions will not do)

ISBN-10: 0072465638

ISBN-13: 978-0072465631

Page 6: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Questions / Feedback

If something is bothering you - let me know early!

• Ways to get in touch with me (phone is not great)

• Ask questions in class

• Come to office hours

• Send me an email

• Post to BBLearn, for the benefit of other students

Page 7: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Course outline

• Modeling and design: ER and relational models, normalization

• Querying: relational algebra, SQL, Datalog

• Query execution: indexing, query optimization

• Responsible data mining

Page 8: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

What is a DBMS?

• A database is a large integrated collection of data

• Models a real-world enterprise

• Augments raw data with metadata, to give meaning to the data

• A Database Management System (DBMS) is a software package designed to store and manage databases

• Our focus: Relational Database Management Systems (RDBMS), centered around the relational model

• Entities (e.g., students and courses)

• Relationships (e.g., students taking courses)

8

Page 9: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

The role of a DBMS

• Serves as an intermediary between the user and the database

• Enables the sharing of data

• Supports multiple alternative views of the data

9

Page 10: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

A traditional database application• Build a system to store and access information about

• students• courses• professors• who takes what, who teaches what

• Functionality

• record enrollment information

• compute GPA for each student after each term

• analyze student enrollment and performance in different courses, majors, departments etc

10

Page 11: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Can we do this without a DBMS?

Sure we can! We can store data in files:

students.txt courses.txt professors.txt

11

… then write a C or Java program to implement specific tasks.

Different users can execute these tasks (run the programs)

Page 12: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Without a DBMS

Task: Enroll “Mary” in “CS461”

write a Java program that does the following:

12

read students.txtread courses.txtfind & update record “Mary”find & update record “CS461”write students.txtwrite courses.txt

Page 13: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Without a DBMS

• System crashes

• Large datasets (say, 500GB)

• Simultaneous access by many users

13

read students.txtread courses.txtfind & update record “Mary”find & update record “CS461”write students.txtwrite courses.txt

crash

Page 14: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Without a DBMS

• Format of students.txt changes

14

read students.txtread courses.txtfind & update record “Mary”find & update record “CS461”write students.txtwrite courses.txt

• Certain users should only be allowed to see parts of the file courses.txt

Page 15: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Enter a DBMS• When in doubt - introduce a level (or levels) of abstraction!

• Many views, one conceptual (logical) schema, one physical schema

• Views describe how users see the data.

• A logical schema defines the logical structure of the data.

• A physical schema describes the files and indexes used.

15

Physical Schema

Conceptual Schema

View 1 View 2 View 3

Page 16: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Example

• Conceptual schema

• Students (sid:string, name: string, gpa: real)

• Courses (cid: string, name: string, credits: int)

• Enrolled (sid: string, cid: string, grade: real)

16

Physical Schema

Conceptual Schema

View 1 View 2 View 3

• View 1: Course_Info(cid: string, enrollment: int)

• View 2: Student_Info (sid:string, name: string, gpa: real)

• Physical schema

• Courses, Enrolled stored as unordered files

• Students stored sorted by sid

Page 17: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

A key concept: data independence• Applications (and users) are insulated from how

data is structured and stored

• Logical data independence: protection from changes in logical structure of the data

• Physical data independence: protection from changes in physical structure of the data

17

Physical Schema

Conceptual Schema

View 1 View 2 View 3

Page 18: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

So, why use a DBMS?• Technical reasons

• Data independence and efficient access

• Reduced application development time

• Data integrity and security

• Uniform data administration

• Concurrent access, recovery from crashes

• Business reasons

• Reusing existing approaches makes data management more cost-effective!

18

Page 19: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

A word of caution

• DBMS give us the tools to make data management more convenient, efficient, cost-effective

• But, like any technology, databases will only make our lives easier when used appropriately!

19

Page 20: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Why I love databases

• Beautiful

• Practical

• Never boring

Page 21: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

SetsA good overview at

http://en.wikipedia.org/wiki/Set_theory#Basic_concepts_and_notation

• A set is an unordered collection of objects

• An object belonging to a set is an element (or a member) of that set, written

• We denote sets with capital letters, elements with lowercase letters

If you are rusty on these concepts, it is your responsibility to get up to speed!

21

a∈A

Page 22: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

• Any set A a subset of itself. A proper subset?

• is a subset of any set. A proper subset?

• An empty set is a set that contains no elements.

• We say that A is a subset of B if all elements of A are also members of B. Then B is a superset of A.

• A is a proper subset of B if A is a subset of B, and there exists at least one element such that

22

A =∅

A⊆ BB ⊇ A

A =∅

b∈Bb∉A

A⊂ BB ⊃ A

Sets

Page 23: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Sets

• The size (aka cardinality) of a set is the number of elements in the set

• The size of an empty set is 0

• Some sets are of infinite size, e.g., the set of all integers, all prime numbers, etc

• In databases we usually work with large finite sets

23

A

Page 24: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Set operations

24

Venn diagrams from http://en.wikipedia.org/wiki/Venn_diagram

A∩ B

A B A B

A∪ B

Intersection Union

Difference

B \ A

A B

Page 25: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Set operations

25

How can you express symmetric difference using set operations on the previous slide?

A B

Page 26: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Cartesian product

Cartesian product of A and B, denoted is a set of ordered pairs (a, b), where

26

A × Ba∈A,b∈B

Example: A = {1,2,3} B = {3,4}A × B = {(1,3), (1,4), (2,3), (2,4), (3,3), (3,4)}

Cross-product is another name for this operations

Page 27: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

ER Modeling• The Entity Relationship (ER) model is used for logical design of

a database

• Introduced by Chen in 1976

• What are the kinds of entities and relationships in the enterprise?

• What information about these entities and relationships should we store in the database?

• What are the business rules that hold?

• What operations are required / frequent?

• Key insight: some designs are clearly wrong, but there are often multiple correct ones!

27

Page 28: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

ER model basics• Entity - a real-world object distinguishable from other

objects, e.g., Mary Jones, CS500

• Attribute - a characteristic of an entity, e.g,. name, age, social security number (ssn)

• Entity set - a collection of similar entities, where similar means (for now) that they share the same attributes

28

DEPARTMENTS*

did*name*

budget*

EMPLOYEES(

ssn(name(

.tle(

Page 29: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Keys-keys-keys

Each entity set must have a key attribute (a.k.a. an identifier)

• a key uniquely identifies an entity within the entity set

• a key is either simple (1 attribute) or composite (multiple attributes)

Keys are important! They allow us to:

• unambiguously refer to an entity within a set

• link together entities across entity sets

29

Page 30: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Attributes

• Each attribute has a domain - a set of possible values (e.g., GPA, gender, name)

• Required vs. optional (e.g., last name vs. middle name)

• Simple vs. composite (e.g., age vs. address)

• Single-valued vs. multi-valued (e.g., car make vs. color)

• Stored vs. derived (e.g., dob vs. age) - advantages / disadvantages?

30

Page 31: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Relationships and relationship sets • A relationship is an association between two or more entities, e.g.,

Jane works in IT

• A relationship set is a collection of similar relationships, e.g., Employees work in Departments

31

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

work_in(

Page 32: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Descriptive attributesA relationship set can have descriptive attributes

32

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

work_in(

since(

Page 33: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

N-ary relationship sets

33

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

work_in(

since(

LOCATIONS(

address( capacity(

An n-ary relationship set R associates n entity sets E1, E2, ...., En. Each relationship r relates entities e1∈ E1, e2 ∈ E2, ..., en∈ En.

this is a ternary relationship set

Page 34: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Recursive relationship sets

34

The same entity set can take on different roles in the same relationship set. This is called a recursive relationship set.

EMPLOYEES(

ssn(name(

.tle(

reports_to(

subordinate(supervisor(

Page 35: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

An instance of a relationship set• A relationship belongs to (is an element of) a relationship

set, and so must be uniquely identifiable by participating entities.

35

EMPLOYEE' DEPARTMENT'works_in'

Meaning: each (employee, department) pair appears in the relationship set only once. Because it’s a set!

Page 36: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Examples

Draw ER diagrams representing entity sets and relationship sets described below.

Men(ssn, name, dob) Women(ssn, name, dob)

Currently_Married (marriage_date)

Cars(vin, plate, state) Parking(lot, address)

Assigned_To()

36

Page 37: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Examples (solution)Draw ER diagrams representing entity sets and

relationship sets described below.

Men(ssn, name, dob) Women(ssn, name, dob)

Currently_Married (marriage_date)

37

ssn name dob

Men

ssn name dob

Womencurrently_married

marriage_date

Page 38: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

ExamplesDraw ER diagrams representing entity sets and

relationship sets described below.

Cars(vin, plate, state) Parking(lot, address)

Assigned_To()

38

Page 39: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Examples (solution)Draw ER diagrams representing entity sets and

relationship sets described below.

Cars(vin, plate, state) Parking(lot, address)

Assigned_To()

39

vin plate state

Car

lot address

Parkingassigned_do

Page 40: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Entity vs. Attribute

• It is not always easy to tell whether an attribute warrants creating an entity set of its own

• Difficult cases

• multi-valued attributes, e.g., colors of a car (exterior / interior)

• composite attributes, e.g,. address

• Rule of thumb: composite attributes should be modeled as entity sets if we care about their structure

• Rule of thumb: multi-valued attributes should be modeled as entity sets

40

Page 41: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Entity vs. Attribute• Another tricky case: descriptive attributes of relationship

sets

• Business rule: An employee many manage the same department during different periods of time

41

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

manage(

from( to(

Page 42: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

What is the difference?

42

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

manage(

from( to(

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

manage(

from( to(PERIODS(

Page 43: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

ExamplesDraw ER diagrams representing entity sets and relationship

sets described below.

Courses are offered by departments. A course may be offered in multiple terms, and each offering must be recorded.

Courses are offered by departments. A course may be offered in multiple terms, and only the most recent offering must be recorded.

Courses are offered by departments. A course may be offered in multiple terms, and only the first offering must be recorded.

43

Page 44: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Examples (solution)Draw ER diagrams representing entity sets and relationship

sets described below.

Courses are offered by departments. A course may be offered in multiple terms, and each offering must be recorded.

44

cid name

Courses

did name

Departmentsoffered_by

term

Offering

Page 45: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Examples (solution)Draw ER diagrams representing entity sets and relationship

sets described below.

Courses are offered by departments. A course may be offered in multiple terms, and only the most recent offering must be recorded.

Courses are offered by departments. A course may be offered in multiple terms, and only the first offering must be recorded.

45

cid name

Courses

did name

Departmentsoffered_by

term

same answer for both!

Page 46: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

ER model: beyond the basics

• So far we saw the basic ER model

• From the next slide on we will look at additional features: annotating / highlighting edges to express more detailed business rules

• Our goal is to express the following concepts for relationships:

1. key constraints

2. participation constraints

46

Page 47: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

work_in(

since(

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

manage(

since(

Multiplicity (aka connectivity)

47

M 1 many to 1

1 1 1 to 1

M Nmany to manyEMPLOYEES(

ssn(name(

.tle(

SKILLS(

name(

have(

Page 48: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Key constraints• Business rule: A professor may teach 0, 1 or multiple

classes. Each class is taught by at most one professor.

48

• The restriction that each class is taught by at most one professor is an example of a key constraint.

• Intuitively: given a Classes entity, we can uniquely determine which Professors entity it maps to

• This gives a way to interpret the one side of one-to-one and one-to-many relationship sets

Page 49: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Key constraints

49

key constraint

PROFESSORS'

ssn'name'

teach' CLASSES'

cid'name'

term'

PROFESSORS' CLASSES'teach'

Business rule: A professor may teach 0, 1 or multiple classes. Each class is taught by at most one professor.

many-to-one

Page 50: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Key constraints

50

key constraint

PROFESSORS'

ssn'name'

teach' CLASSES'

cid'name'

term'

Business rule: A professor teaches at most one class. Each class is taught by at most one professor.

PROFESSORS' CLASSES'teach'key constraint one-to-one

Page 51: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Participation constraints

• Business rule: A professor may teach 0, 1 or multiple classes. Each class is taught by some professor.

51

• The restriction that each class is taught by some (read “at least one”) professor is an example of a participation constraint.

• Intuitively: each Classes entity participates in the relationship set teach

• Participation and key constraints are orthogonal: you can have one without the other

Page 52: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

PROFESSORS'

ssn'name'

teach' CLASSES'

cid'name'

term'

Participation constraints

52

total participation

partial participation

many-to-many

Business rule: A professor may teach 0, 1 or multiple classes. Each class is taught by some professor.

PROFESSORS' CLASSES'teach'

Page 53: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

PROFESSORS'

ssn'name'

teach' CLASSES'

cid'name'

term'

Participation constraints

53

total participation

total participation

many-to-many

Business rule: Each professor teaches some class. Each class is taught by some professor.

PROFESSORS' CLASSES'teach'

Page 54: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

PROFESSORS'

ssn'name'

teach' CLASSES'

cid'name'

term'

Participation constraints

54

key andtotal participation

total participation

many-to-one

Business rule: Each professor teaches some class. Each class is taught by exactly one professor.

PROFESSORS' CLASSES'teach'

Page 55: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

EMPLOYEES(

ssn(name(

.tle(

DEPARTMENTS(

did(name(

budget(

manage(

since(

work_in(

since(

Explain the business rules

55

Page 56: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

ExamplesDraw ER diagrams representing entity sets and

relationship sets described below. Clearly mark all key and participation constraints.

Employees have managers. An employee has at most one manager. A manager may manage any number of employees.

Professors advise students. Each professor advises at most one student. Each student has exactly one advisor.

56

Page 57: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Examples (solution)Draw ER diagrams representing entity sets and relationship sets

described below. Clearly mark all key and participation constraints.

Employees have managers. An employee has at most one manager. A manager may manage any number of employees.

57

EMPLOYEES(

ssn(name(

.tle(

report_to(

subordinate(supervisor(

(arrowhead, not bold)

orMANAGERS(

ssn(name(

.tle(

manage( EMPLOYEES(

ssn(name(

.tle(

(arrowhead, not bold)

Page 58: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Examples (solution)Draw ER diagrams representing entity sets and relationship sets described below. Clearly mark all key and participation

constraints.

Professors advise students. Each professor advises at most one student. Each student has exactly one advisor.

58

PROFESSORS'

ssn'name'

advise' STUDENTS'

ssn'name'

Page 59: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Example

•An author is described by a name and a date of birth (dob). No two authors have the same combination of name and dob.

•A book is described by an ISBN, a title and a year when it was written (year). No two books have the same ISBN number.

•A book is written by exactly one author.•An author is only included in the database is she has authored

at least one book.

59

Page 60: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Example (solution)• An author is described by a name and a date of birth (dob). No two authors have

the same combination of name and dob.• A book is described by an ISBN, a title and a year when it was written (year). No

two books have the same ISBN number.• A book is written by exactly one author.• An author is only included in the database is she has authored at least one book.

60

Page 61: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Example•A scientist is described by a name, a field of study, and a date

of birth (dob). No two scientists have the same combination of name and dob.

•A discovery has a name. No two discoveries have the same name.

•An award has a name, which uniquely identifies it.•Scientists make discoveries. A discovery is made by one or

several scientists. A scientist who made no discoveries is not tracked in our database.

•Scientists receive awards. A scientist may receive the same award more than once in different years. All years in which a scientist received a particular award must be recorded.

61

Page 62: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Example (solution)• A scientist is described by a name, a field of study, and a date of birth (dob). No

two scientists have the same combination of name and dob.• A discovery has a name. No two discoveries have the same name. • An award has a name, which uniquely identifies it.• Scientists make discoveries. A discovery is made by one or several scientists. A

scientist who made no discoveries is not tracked in our database.• Scientists receive awards. A scientist may receive the same award more than

once in different years. All years in which a scientist received a particular award must be recorded.

62

Page 63: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

ER modelingDraw an ER diagram that encodes the following business rules. Clearly mark all key and participation constraints.

Chefs work at restaurants. A chef is uniquely identified by an SSN, and is also described by a name and a cuisine in which she specialized. A restaurant is uniquely identified by a combination of name and city. Each chef works in at least one restaurant, and each restaurant must have at least one chef working at it. Some chefs own restaurants, and if a chef owns a restaurant - she is its sole owner.

63

Page 64: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

ER modelingDraw an ER diagram that encodes the following business rules. Clearly mark all key and participation constraints.

Chefs work at restaurants. A chef is uniquely identified by an SSN, and is also described by a name and a cuisine in which she specialized. A restaurant is uniquely identified by a combination of name and city. Each chef works in at least one restaurant, and each restaurant must have at least one chef working at it. Some chefs own restaurants, and if a chef owns a restaurant - she is its sole owner.

64

RESTAURANTS(

city(name(

CHEFS(

name(ssn(

work_at(

own(cuisine(

Page 65: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Key and participation constraints: recap

• Key constraint

• denoted by a line with an arrowhead towards the relationship set

• occurs only in 1-to-many or 1-to-1 relationship sets

• example: a course is taught by one / at most one / exactly one professor

• Participation constraint

• denoted by a bold line towards the relationship set

• can occur on the 1 or on the many side of a relationship set

• example: a course must be taught by a professor / a course is taught by at least one professor

65

Page 66: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

EMPLOYEES(

ssn(name(

.tle(

have( DEPENDENTS(

num(name(

Weak entities • An entity is weak (or existence-dependent) if it can exist

only when associated with an occurrence of the identifying owner

• Otherwise an entity is strong, of just “an entity”

• Important: weak entities cannot be identified without the identifying owner, and so they do not specify a complete key.

66

1 M

Page 67: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Weak entities • The identifying owner entity and the weak entity must

participate in a one-to-many relationship set. This relationship set is called the identifying relationship set of the weak entity.

• The weak entity must have total participation in the identifying relationship set.

67

EMPLOYEES(

ssn(name(

.tle(

have( DEPENDENTS(

num(name(

1 M

67

Page 68: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Summary of conceptual design

• Entity sets / entities / attributes

• Relationship sets / relationships

• Multiplicity: 1-to-1, 1-to-many, many-to-many

• Key constraints: given an entity, can we identify other entities participating in the relationship

• Participation: partial (not all entities in the set participate) vs. total (all do)

• Weak entities: cannot be identified without an identifying owner, participation must be total

68

Page 69: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

The Extended ER model: Entity Clustering

• ER diagrams can get large (tens or hundreds of entity sets and relationship sets)

• For readability we sometimes group together related entity sets and relationship sets into a single entity cluster

• We will see an example next

69

Page 70: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

A night at the opera

“I love the opera. Let me develop a useful application for this domain.”

What are the entities?

What are the relationships?

What are the business rules?

70

Page 71: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Opera: entity sets • Opera(title, year) - may have more than one opera by the

same title, but not in the same year (e.g., Otello - Verdi 1887 / Rossini 1816)

• Composer (name, birth, death) - may have more than one composer with the same name, but not also with the same birth and death years

• Librettist (name, birth, death)

• Company (name)

• Venue (name, city)

• Production - an entity set or a relationship set, linking Opera and Company?

• Performance - an entity set or a relationship set, linking Production and Venue

71

Page 72: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Opera: relationship sets

• Opera is written by Composer / Librettist

• each Composer composed at least one Opera

• each Librettist wrote the libretto for at least one Opera

• an Opera by the same name may be written by a different (Composer, Librettist) pair

• A piece is produced by a Company

• A production is performed at a Venue

72

Page 73: Introduction and Overview Entity Relationship (ER) Modelingjulia/cs461/documents/lectures/lecture1.pdf · Julia Stoyanovich ER Modeling • The Entity Relationship (ER) model is used

Julia Stoyanovich

Opera: the model

73

wri$en_by*OPERA&

name&COMPOSER&

name&birth&

death&

LIBRETTIST&

name&birth&

death&

season&

PIECE&

COMPANY&

name&

produced_by*

year&

PRODUCTION&

VENUE&

name& city&

performed_at*

date&