31
EntityRelationship Models Entity Relationship Models Zaki Malik September 23, 2008 September 23, 2008

Lecture 09

Embed Size (px)

Citation preview

Page 1: Lecture 09

Entity‐Relationship ModelsEntity Relationship Models

Zaki Malik

September 23, 2008September 23, 2008

Page 2: Lecture 09

Exercise #1Exercise #1A university database contains information about professors

(id tifi d b i l it b ) d(identified by social security number) and courses(identified by courseid).  Professors teach courses; each of the following situations concerns the Teaches relationship set.  For each situation, draw an ER diagram that describes it.

• Professors can teach the same course in several semesters• Professors can teach the same course in several semesters, and each offering must be recorded.

2

Page 3: Lecture 09

Exercise #2Exercise #2

Professors can teach the same course in several semesters, and only the most recent such offering needs to be recorded.  

3

Page 4: Lecture 09

Exercise # 3 and 4• Every professor teaches exactly one course (no more, no 

less)

• Every professor teaches exactly one course (no more, no less), and every course must be taught by some professor

4

Page 5: Lecture 09

Practice

• Professors have an SSN, a name, an age, a rank, and a research specialty.

( )• Projects have a project number, a sponsor name (e.g., NSF), a starting date, an ending date, and a budget.

5

Page 6: Lecture 09

• Graduate students have an SSN, a name, an age, and a degree program• Each project is managed by exactly one professor (known as PI)Each project is managed by exactly one professor (known as PI)• Each project is worked in by one or more professors (known as Co‐PIs)• Each project is worked on by one or more graduate students (known as RAs)

6

Page 7: Lecture 09

• When graduate students work on a project, a professor must supervise their work on the project.  Graduate students can work on multiple projects, in which case they will have a potentially different supervisor for each project.

• Departments have a department number, a department name, and a main office.

• Department has a professor (known as Chairman) who runs the department.

7

Page 8: Lecture 09

• Professors work in one or more departments, and for each department that they work in, a time percentage is associated with their job

• Graduate students have one major department in which they are working on their• Graduate students have one major department in which they are working on their degree.

• Each graduate student must have another, more senior graduate student as an advisoradvisor.

8

Page 9: Lecture 09

9

Page 10: Lecture 09

Exercise # 5Exercise # 5

• A company database needs to store information about employees(identified by ssn, with salary and phone as attributes), departments(identified by dno, with dname and budget as attributes), and children ofemployees (with name and age as attributes). Employees work ind h d d b l h lddepartments; each department is managed by an employee; a child mustbe identified uniquely by name when the parent (who is an employee;assume that only one parent works for the company) is known.

• Draw an ER diagram that captures this information.

Page 11: Lecture 09

• A company database needs to store information about employees(identified by ssn, with salary and phone as attributes), departments(id tifi d b d ith d d b d t tt ib t ) d hild f(identified by dno, with dname and budget as attributes), and children ofemployees (with name and age as attributes).

Page 12: Lecture 09

• Employees work in departments;

• each department ismanaged by an employee;

Page 13: Lecture 09

• a child must be identified uniquely by name when the parent (who is anemployee; assume that only one parent works for the company) is known.

Page 14: Lecture 09

Exercise # 6• You set up a database company, ArtBase, that builds a product for art

galleries. The core of this product is a database with a schema that capturesall the information that galleries need to maintain.g

• Galleries keep information about artists, their names (which are unique),birthplaces, age, and style of art. For each piece of artwork, the artist, theyear it was made, its unique title, its type of art (e.g., painting, lithograph,y , q , yp ( g , p g, g p ,sculpture, photograph), and its price must be stored. Pieces of artwork arealso classified into groups of various kinds, for example, portraits, still lifes,works by Picasso, or works of the 19th century; a given piece may belong tomore than one group. Each group is identified by a name (like those justgiven) that describes the group. Finally, galleries keep information aboutcustomers. For each customer, galleries keep that person’s unique name,dd t t l t f d ll t i th ll ( i t t!) d thaddress, total amount of dollars spent in the gallery (very important!), and theartists and groups of art that the customer tends to like. Draw the ER diagramfor the database

Page 15: Lecture 09

• Galleries keep information about artists, their names (which are unique), birthplaces, age, and style of art. F h i f t k th ti t th it d it i titl it t• For each piece of artwork, the artist, the year it was made, its unique title, its type 

of art (e.g., painting, lithograph, sculpture, photograph), and its price must be stored. 

Page 16: Lecture 09

• Pieces of artwork are also classified into groups of various kinds, for example, portraits, still lifes, works by Picasso, or works of the 19th century; a given piece may b l t thbelong to more than one group. • Each group is identified by a name (like those just given) that describes the group. 

Page 17: Lecture 09

• Finally, galleries keep information about customers. For each customer, galleries keep that person’s unique name, address, total amount of dollars spent in the gallery ( i t t!)(very important!), • and the artists and groups of art that the customer tends to like

Page 18: Lecture 09
Page 19: Lecture 09

Exercise # 7• Notown records has decided to store information on musicians who 

perform on their albums (as well as other company data) in a database. The company has chosen to hire you as a database designer.– Each musician that records at Notown has an SSN, a name, an address and a 

h b l d f h h dd dphone number. Poorly paid musicians often share the same address, and no address has more than one phone.

– Each instrument that is used in songs recorded at Notown has a name (e.g. guitar synthesizer flute) and a musical key (e g C B‐flat Eflat)guitar, synthesizer, flute) and a musical key (e.g., C, B‐flat, Eflat).

– Each album that is recorded at the Notown label has a title, a copyright date, a format (e.g., CD or MC) and an album identifier.

– Each song recorded at Notown has an id, title and an author.Each song recorded at Notown has an id, title and an author.

– Each musician may play several instruments, and a given instrument may be played by several musicians.

– Each album has a number of songs on it, but no song may appear on more g g y ppthan one album.

– Each song is performed by one or more musicians, and a musician may perform a number of songs.

– Each album has exactly one musician who acts as its producer. A producer may produce several albums.

• Draw an ER diagram for Notown.

Page 20: Lecture 09

• Each musician that records at Notown has an SSN, a name, an address anda phone number. Poorly paid musicians often share the same address, and

dd h th hno address has more than one phone.

Page 21: Lecture 09

• Each instrument that is used in songs recorded at Notown has a name (e.g. guitar, synthesizer, flute) and a musical key (e.g., C, B‐flat, Eflat).

Page 22: Lecture 09

• Each song recorded at Notown has an id, title and an author.

Page 23: Lecture 09

• Each musician may play several instruments, and a given instrument may be played by several musicians.

Page 24: Lecture 09

• Each album that is recorded at the Notown label has a title, a copyright date, a format (e.g., CD or MC) and an album identifier. E h lb h b f it b t•Each album has a number of songs on it, but no song may appear on more than one album.

Page 25: Lecture 09

• Each song is performed by one or more musicians, and a musician may perform a number of songs.

Page 26: Lecture 09

• Each album has exactly one musician who acts as its producer. A producer may produce several albums.

Page 27: Lecture 09
Page 28: Lecture 09

• The E/R Model describes a database about bands and their tours. A tour consists of a sequence of cities visited by a band. We assume that no city is visited twice on a single tour, and on one date, a band can visit only one city. Some of the entity sets are weak, and some of the relationships are supporting many‐one relationships, but all double rectangles and double diamonds are not shown. 

• Your task is to decide which of the entity sets are weak, and which relationships support them. If there is a choice, prefer to use the attributes of the entity set itself in the key, and minimize the number of supporting relationships. There may be more than one reasonable answer, especially since "tour#" can have several reasonable interpretations. You should pick from the list below the one that is most reasonable. 

Page 29: Lecture 09

Correct AnswersCorrect Answers

• Stops is weak, supported by On.p , pp y

• Tours is weak, supported by By; tour# is unique only for a given band.

• Tours is not weak; tour# is a globally unique ID• Tours is not weak; tour# is a globally unique ID.

• Cities is weak and its key is the city name and the name ofCities is weak, and its key is the city name and the name of the state it is In.

Page 30: Lecture 09

INCORRECT ANSWERS (with explanation)• Stops is weak, supported by both At and On; date is not a key attribute.Stops is weak, supported by both At and On; date is not a key attribute.

– We are told that a tour cannot stop at two cities on the same date. Thus, once we know the date and the tour that a Stop represents, we don't also need the City to have a key for Stops. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.

• Stops is weak, supported by both At and On; date is a key attribute.– We are told that a tour cannot stop at two cities on the same date. Thus, once we know 

the date and the tour that a Stop represents, we don't also need the City to have a key for Stops. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.for Stops. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.

• Stops is weak, supported by At.– A date and a city does not define a unique Stop (City/Tour pair). The reason is that 

several tours may stop at the same city on the same date. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.

• Cities is not weak.– The city name by itself does not determine a unique city. There can be cities of the 

same name in two or more different states e g Portland ME and Portland OR See Sectsame name in two or more different states, e.g., Portland ME and Portland OR. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.

• Bands is weak, supported by By.– A supporting relationship has to be many‐one from the entity set it supports. In orderA supporting relationship has to be many one from the entity set it supports. In order 

for this diagram to make sense, we have to assume that band names are unique, because By cannot help define a band uniquely using the weak entity set construct. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.

Page 31: Lecture 09

• States is weak, supported by In.– A supporting relationship has to be many‐one from the entity set it supports. Note also 

that state names are unique. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.

• Stops is weak and its key is the date and the name of the band making• Stops is weak, and its key is the date and the name of the band making the tour the stop represents.

– There is no many‐one relationship directly from Stops to Bands. Thus, an attribute of Bands can only contribute to the key of Stops if Tours is weak and the band name constributes to the key of Tours. But then, the tour# would also be in the key for Stops. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.

• Stops is weak, and its key is the date, the name of the associated city, and th i t d t #the associated tour#.

– Since it is ambiguous whether tour#'s are unique, it is possible that there are two bands that use the same tour number, and these bands stop in the same city on the same date. See Sect. 2.4 (p. 54) for the rules regarding weak entity sets.

• Tours is weak, supported by On and By.– A supporting relationship has to be many‐one from the entity set it supports. Thus, On 

couldn't possibly support Tours. See Sect. 2.4 (p. 54) for the rules regarding weak entity tsets.