15e5265614cbab43004d4fe7345d2168.ppt
- Количество слайдов: 43
Entities and Relationships
What is a Relationship? • An association between or among entities. Can be binary, ternary, quartic, etc. • Because entities are “types” of things, relationships also have to be “types” of associations. • Entities have instances and relationships have occurrences. Entities are while relationships happen. • In sentences, relationships often show up as verbs. Most People own Automobiles • Is a relationship between the entities PERSON and AUTOMOBILE. Andrew Pletch owns a VW Passat with VIN: 1234567890987654321 is an occurrence of this relationship.
How do we picture a Relationship? ? OWNS PERSON AUTOMOBILE As a line joining the related entities with a diamond in the middle The name of the relationship goes in or on top of the diamond The relationship name tends to make grammatical sense in one direction only
Just to make it clear: OWNS PERSON AUTOMOBILE This is a relationship This is a collection of occurrences of this relationship
Can Relationships have Properties? • Yes! • They are also called attributes. • They appear in the diagram underneath the diamond. PERSON OWNS AUTOMOBILE ? Purchase. Date • Why can’t Purchase. Date be an attribute of AUTOMOBILE? – Reason 1: Because the same car can be sold several times and so have a list of Purchase. Dates – Reason 2: Because Purchase. Date only makes sense in the context of “what car? ” and “who bought it? ”
Attributes Don’t Get Copied. OWNS PERSON AUTOMOBILE SSN FName LName DOB VIN Owner. Name Make Model Year NOT permitted/recommended • The fact that a vehicle has an owner who has a name is modeled by the
Does a Relationship have a Key? • Yes, but they are not documented in the relationship. • Typically it is the combined keys of the participating entities. PERSON Person. ID OWNS AUTOMOBILE VIN Purchase. Date • The key to
Exercise: • • What does
What if you CAN
Exercise: • How do the two versions of
Relationship Cardinalities: • Read the notes: www. cs. newpaltz. edu/~pletcha/DB/Three. Questions. html • Enterprise Rules are used by the database designer determine what things are entities, what things are relationships and what things are attributes. Example: Every Employee earns one and only one salary EMPLOYEE ID. . . Salary single-valued fact about all employees so is modeled as an attribute of EMPLOYEE
Relationship Enterprise Rules: PERSON Person. ID OWNS AUTOMOBILE VIN purchase_date • How do we model the following rules? Rule 1: A person can own one and only one AUTOMOBILE Rule 2: A person can own many automobiles acquired at any time Rule 3: Some people do not own automobiles at all Rule 4: Every automobile has at least one and possibly many owners. • The answer seems to boil down to asking how many times each PERSON or each AUTOMOBILE can participate in the
How many vehicles can a person own? • Rule 1 and Rule 2 on the previous slide are mutually exclusive but our picture doesn’t allow us to say which of these is the rule that applies in this case. • That is because the picture is not complete. • We complete the picture by adding what we call Participation Numbers to each entities participation in each relationship.
Minimum Participation Numbers: OWNS PERSON Person. ID AUTOMOBILE VIN (1, ? ) (0, ? ) purchase_date • Does every PERSON own at least one AUTOMOBILE? no – min participation number (m. PN) is 0 yes – min participation number (m. PN) is 1 √ • Is every AUTOMOBILE owned by at least one PERSON? no – min participation number (m. PN) is 0 yes – min participation number (m. PN) is 1 √
Maximum Participation Numbers: OWNS PERSON Person. ID (0, n) AUTOMOBILE VIN (1, 1) purchase_date • Does any PERSON own more than one AUTOMOBILE? no – max participation number (MPN) is 1 yes – max participation number (MPN) is n √ • Is any AUTOMOBILE owned by more than one PERSON? no – max participation number (MPN) is 1 yes – max participation number (MPN) is n √
Interpreting Participation Numbers: OWNS PERSON Person. ID AUTOMOBILE VIN (0, n) (1, 1) purchase_date m. PN = 1: Every PERSON
Participation Numbers and Keys • Compare OWNS PERSON Person. ID AUTOMOBILE VIN (1, n) (0, n) Purchase. Date • and OWNS PERSON Person. ID (0, n) AUTOMOBILE VIN (1, n) Purchase. Date The first lets a PERSON own multiple AUTOMOBILES but not the same AUTOMOBILE twice. The latter allows both things to happen.
An Example: A library keeps records of current loans of books to borrowers. Each borrower has a borrower# and each copy of a book has an accession# (there may be several copies of the same book). The library keeps the name and address of each borrower so that overdue reminders can be sent if necessary. For each book, the library keeps the title, authors, publisher, publication date, ISBN, purchase price and current list price. Borrowers can have one of two statuses - junior and senior. There are restrictions on the number of books a borrower may take out at one time depending on his/her status. Books which are out on loan may be reserved by other borrowers. The library does not buy paperbacks. When a new edition of a book is acquired, all copies of earlier editions are removed from the shelves.
List all Nouns: A library keeps records of current loans of books to borrowers. Each borrower has a borrower# and each copy of a book has an accession# (there may be several copies of the same book). The library keeps the name and address of each borrower so that overdue reminders can be sent if necessary. For each book, the library keeps the title, authors, publisher, publication date, ISBN, purchase price and current list price. Borrowers can have one of two statuses - junior and senior. There is a loan limit on the number of books a borrower may take out at one time depending on his/her status. Books which are out on loan may be reserved by other borrowers. When a book is returned the person who made the earliest Reservation for he book is notified. Library Loan Book Borrowerid Copy Accession_no B_name B_address Reminder Title Author Publisher Pub_date ISBN P_price C_price B_status Loan_limit Reservation
Which Nouns are Entities and Which are Attributes? Library Loan Book Borrowerid Copy Accession_no B_name B_address Reminder Title Author Publisher Pub_date ISBN P_price C_price B_status Loan_limit Reservation Why is Author not an entity? (i) It certainly is a property of Book. (ii) All we know about authors are their name. (iii) Authors do nothing but author books. (iv) In a Publishing House database Authors would be entities. (v) In a Library database, Authors only exist as authors of Books so do not have “independent” existence.
List Entities and their Attributes: Loan l_date -- how else do we know if it is overdue Book ISBN Why is Library dropped altogether? Author Title (i) Although certainly an entity, there is only one instance. Pub_date (ii) All other entities have multiple instances. C_price (iii) No properties exist for Library other than its name. Borrower (iv) If our application was a library system with many Borrowerid libraries then it would make sense to make Library B_name an entity. B_address B_status Loan_limit Publisher Pub_name Reminder Copy Accession_no P_price Reservation R_date -- how else do we send a notice to the earliest reservation
First Look at a List of Entities: NOTES: 1: Things that have keys that belong to them are likely to be entities. 2: Nouns that are missing keys and seem to relate to other things are likely to be relationships and not entities.
Now Add Some Relationships 1: Reminder is more of a transaction or activity than a thing. All the info needed for a Reminder is found in other things. 2: Pub_name can be an attribute of Book if we drop the Publisher entity
Final Version (Library External View without m. Mpn):
Final Version (with m. Mpn) 1: Cardholder not Borrower (more accurate) Does every Cardholder reserve a book? (n=0) Does any Cardholder reserve more than one book? y=n Is every Book reserved by at least one Cardholder? (n=0) Is any Book reserved by more than one Cardholder? (y=n) Does every Cardholder borrow a book? (n=0) Does any Cardholder borrow more than (y=n) one book? Is every Copy borrowed by at least one Cardholder? (n=0) Is any Cardholder borrowed by more than one Cardholder? (n=1) [current loans only] Does every Book exist as a copy in the (y=1) Library? Does any Book have more than one copy in the Library? (y=n) (y=1) Is every Copy a copy of some Book? Is any Copy a copy of more than one (n=1) Book?
A Practical Approach to ER Design • Read the “Ten Steps to Database Design” web page. It shows how to complete the task of preparing an External View ER diagram in a systematic fashion. http: //www. cs. newpaltz. edu/~pletcha/DB/Ten. Steps 2 DBDesign. html
More on Participation Numbers: • Some relationships are ternary: Consider vaccines produced by different manufacturers and sold in different countries. VACCINE MANUFACTURER V_ID Man_ID distributed by, in COUNTRY Name • The problem is, how to ask the PN questions.
Asking the Right Question: VACCINE V_ID MANUFACTURER (1, n) (c, d) (a, b) (1, n) Man_ID distributed by, in (1, n) (e, f) COUNTRY a? : Does every VACCINE participate at least once in the
A Class Example: Part A: An Airline has planes that are used to fly scheduled flights. Flight segments are between two airports – departure and arrival. They are scheduled to depart at a certain time and arrive at a certain time. The type of plane to be used is also known. Scheduled flights are a sequence of flight segments. Actual flights occur on particular dates using a particular plane flown by a particular pilot and co-pilot. They take off at a given time (perhaps) different from the scheduled departure time and have an ETA. Pilots have names, SSNs, addresses, DOB. Planes have unique numbers, capacities – both 1 st class and economy This database doesn’t deal with passengers.
A Class Example: Part B: An Airline reservation system keeps information about customers, their reservations, tickets, seat assignments for scheduled flights. Scheduled flights are between two airports – departure and arrival. They are scheduled to depart at a certain time and arrive at a certain time. The type of plane to be used is also known. Scheduled flights have flight numbers. Customer info includes name, address, email address, phone number A reservation is for a certain customer on a sequence of scheduled flights. It has a locator number. Each reservation has a single source and single destination airport. A ticket is a paid reservation. It has a ticket number and a payment method Including credit card info
Weak Entities • An entity is weak if it depends on another entity for part of its key. • Remember, we can’t copy keys around an ER diagram. • We can’t do the following • But because the key to an Order Line is really the combination of (Order. Number, Line. Number), Line instances depend on the key to Order for part of their key.
Weak Entities (cont): • Instead we draw a double line around the weaker (dependent) entity and the relationship that it depends on. • This picture says that Line is a weak entity whose key consists of the pair (Order. Number, Line. Number). • Weak entities always have a (1, 1) participation number pair linking to the entity they depend upon. Why?
Weak Entity – Another Example • Some times a relationship from one department’s point a view is an entity in some other department. • For the Registrar’s Office, Enrollment as a relationship between two entities – Student and Course: • For the Bursar’s Office, Enrollment is an entity that relates to a Chargeable Item. • Why are
How to merge the two diagrams? NOTE: We converted a relationship into a weakentity relationship become Registrar’s View Bursar’s View
A New Model for an Old Idea • The previous example used to be called aggregation. It used to be modeled as: • The box labeled Enrollment turned the
IS_A • Before inheritance became popular because of OOP there was IS_A. • Both Professor and Student possess the attributes ID, Name and DOB by “inheritance”. • The key to Professor and the key to Student is the key to Person.
Modeling History: • The
Modeling History 2: • Suppose we wanted to model a complete loan history of all loans; past and present. • To allow more than one loan of the same copy we would first need to change the copy Max PN to n. • To allow the same Cardholder to borrow the same Copy more than once (but on different occasions) we need to add time to the key. To show that this is part of the key to
Self-Related Entities: • Some entities are related to themselves. • This models what kind of a data structure?
More on Participation Numbers: • Participation Numbers are concise representations of Enterprise Rules. • A database designer, viewing m. MPNs, can make positive declarations about the rules by which a business is run. • Participation numbers are flexible enough (16 different ways to model a binary relationship) to handle all we need to do.
How to merge two External Views Publisher external View BOOK PUBLISHER published_by isbn Name author. . . (1, 1) (1, n) title pub_date c_price
Library External View without m. Mpn:
Conceptual Model (1, 1)


