Download as pdf
Download as pdf
You are on page 1of 28
An entity-relationship diagram (FR diagram) is a graphical representation of data for an organisation at conceptual level. " ER diagrams are frequently used by database designers to build data diagram is a graphical representation of data for an _—-madels in the process of database development (see Conceptual Data Modeling organ at conceptual in 5.2). A well-drawn E-R diagram can be transformed into relational database ee tables easily: Entities will be mapped into tables and attributes will be mapped into fields. ‘There are some software tools that help people produce E-R diagrams and generate database schema (see Chapter 7) automatically. The basic constructs of an E-R diagram are entities, attributes and relationship, with symbols shown in Fig.6.1. Entity Attribute | | "Key Attribute Relationship ___| Fig6.1 Symbols used in E-R diagrams Quick Check 6.1 When converted into logical database, entities are mapped into attributes are mapped into Chapter 6 E-R Diagrams 73 6.1 RELATIONSHIPS Recall that an entity is a person, place, device, event or concept and an | Ke points i attribute is a property of an entity class. The requirements for an entity are ‘An entity has one or more 1. one or more attributes attributes, andmany possible 2. many possible distinct instance: dlistiner instances. {relationship Is an ae association between two A relationship is an association between two entities based on a key attribute. | entities based on a key attribute Relationship is a glue that holds together the various components of an ER diagram. naz Jo not confuse relationship reap . with relation: A relation is RS, | alable. A relationship links “up two tables. In this book, an entity will be represented by single noun (e.g. STUDENT, CLUB, CERTIFICATE, etc.) and a relationship will be represented by a verb in present tense (e.g. Buys, Attends, Borrows etc). Fig.6.2 shows the entities and relationships identified for some daily life examples. In these examples, some entities and relationships are not obvious, and you need to filter out unwanted information. 1. Anemployee works in a EMPLOYEE, | Works_in department. DEPARTMENT 2. Astudent studies English, Chinese, | STUDENT, SUBJECT Studies Mathematies, Computer or Economies. ‘An employee has three children. | EMPLOYEE, CHILDREN Has Some teachers are assigned acar | TEACHER, CAR PARK Is assigned pk 5. Anevent in the sports day ofa | STUDENT, EVENT. Participates school will be cancelled if there are no participants 6. Acompany sells ten products. CUSTOMER, PRODUCT | Orders ‘Customers can order products by Le ae a Quick Check 6.2 7. Ateacher ealeulates the average | STUDENT, REPORT CARD | Has ee mark of each student. The marks An entity should are recorded in report cards which have one or more are sent to the parents. and many possible 2 Some daily-life examples of entities and relationship A relationship is an As you can see, identifying entities and relationships for a data model is between two a process of filtering and integrating. It requires certain common sense. based ona 74 PartB Database Design | 4. Guides to Identifying Entities The following hints may be helpful to you in identifying entities 1. Do not include specific data instances, like English, Chinese, etc. in the 2" example. Use an entity class instead. 2. Defer dealing with numbers, like “three children” in the 3% example, “some teachers” in the 4" example and so on. 3. Ignore those data that do not have instances. In the 5 example, you will not record the sports day, but you will record events in the sports day. 4. Ignore facts that can be derived. In the 6 example, “ten products ” can be derived using function COUNT. But, “product” should be recorded. Ignore calculated (derived) result, The average marks in the 7% example will be calculated by the computer. Ignore equipment needed to carry out a process. The “phone” in the 6 example can be ignored. Bear in mind that @ true entity will have many possible instances, each with a distinguishing characteristic. 2. Guides to Naming Relationships Defining a name for a relationship is the perhaps the most challenging task for students. The following are some guidelines: 1. A relationship represents an action being taken. Therefore, a relationship name is a verb phrase (see Fig.6.2) in present tense, Relationship in E-R diagram should be read from left to right or from up to down. Use passive voice if the action is not carried out by the entity on the left. e.g. If SUBJECT is on the left and STUDENT on the right, the name of the relationship should be Is_studied_by, eek Coes or simply Studied_by. 3. A relationship name states the action taken, not the result of ie Pee eC ES the action. eg. use Works_in, not Staffofi use Studies, not ata without _, data Student_of, use Is_Assigned, not Assignment, use Participates not that ean be and =o ‘A.participant_of, use Ori ‘equipment used fo carry out a : Lof. use Orders not business ete. 4. Avoid using vague terms, like Is_related_to. Ignore calculating processes. In the 7 example, calculating average ‘Quick Check 6A ‘marks will be done by a computer. In identifying relationships, 6. Ignore business activities like transfer, send, summarize. In the 7* ae ores Gare example, “sending report cards to parents” should be ignored, involves because this activity is irrelevant to the design of a database and business activities, like system. Remember that you should choose a relationship which is a and’ true association between two entities. $$ Chapters &-8Diograms 75 Cardinality specifies the maximum and minimum numbers of instances of an entity participating in an association with another entity. 1. Maximum Cardinality There are three basic types of relationships with the maximum number of instances specified: one-to-one (1:1), one-to-many (1:M) and many-to-many (MN). The typical example of one-to-one relationship is the relationship between HKID (HKID card) and PERSON (permanent resident in Hong Kong). Each HKID card is issued to one and only one permanent resident. Each permanent resident is issued one and only one HKID card. The typical example of one-to-many relationship is the relationship between PUBLISHER and BOOK. A publisher may publish more than one book. But, each book must be published by exactly one publisher. ‘The typical example of many-to-many relationship is the relationship between CUSTOMER and PRODUCT. A customer may purchase more than one type of product. A type of product may be purchased by more than one customer. Fig.6.3 shows the symbols used for specifying maximum cardinality. Fig.6.4 shows some more exampl Many-to-one M 1 ‘Many-to-many M M Cardinality specifies the ‘maximum and minimum rhumbers of instances of an entity participating in an association with another entity. Quick Check 6.5 AUTHOR and BOOK is ‘an___ relationship. BOOK and ISBN is an relationship. BOOK and ORDER is an relationship. 76 PartB Database Design § One-to-one ‘Asstudent may buy a lunch box. A lunch box. | Student : Lunch Box = 1:1 may be bought by one student. 2. Ateacher may be in charge of a class. Each | Teacher : Class class must be in charge of by one teacher, 3. Asstudent has one and only one student card. | Student: Student Car Each student card is owned by one and one only student. “One-to-many | 1. student may borrow some books from the library. A book in the library may be borrowed by a student. 2. Asummer course is attended by one or more | Summary Course : Student students. Each student may apply for at most EM one summer course. Tis | 3. A student may be assigned to some Student : Functional Post functional posts (monitor, perfect or club 1M chairman). Each functional post is assigned to one and only one student. in| 4. Aclass is formed by a group ofat least one | Class : Student student. A student is allocated to one and only one class. it “Many-to-many | 1. A student may apply for some scholarships. | Student : Scholarship = M:N Each scholarship may be applied for by some students 2. Anevent in the sport day is participated by | Event : Student at least one student (Otherwise the event wall be cancelled). Each student may participate in some event 3. A student takes at least atleast one course. A | Student : ‘course is taken by at least ‘ourse = M:N Fig6.4 Examples of three basic cardinality Besides the maximum number of instances in a relationship, cardinality also specifies the minimum number of instances, using terms: mandatory and optional. Mandatory Cardinality An instance is mandatory (symbol —}—) if there exists at least one instance in the relationship. Terms like must, at least one and one and only one are used to describe mandatory cardinality. For example, a class must have a class teacher. Therefore, TEACHER is mandatory in the relationship between CLASS and TEACHER. However, CLASS is not mandatory because some teachers may not be a class teacher. Another example is the relationship between HKID (HKID card) and PERSON (permanent resident in HK). The relationship is mandatory in both directions because every HKID card must have an owner and a permanent resident must have an HKID card. The above two examples illustrate mandatory cardinality in one-to-one relationships. The relationship between CUSTOMER and ORDER is a mandatory one-to-many relationship. CUSTOMER is mandatory because a sale order must be submitted by a customer. However, ORDER may or may not be mandatory, depending on the company policy, which is called business rule. Some companies treat potential customers who have asked for price quotation as customers. But, some do not. 3. Optional Cardinality An instance is optional (symbol —-) if the instance may or may not exist. Terms like may, or none and at most one are used to describe optional cardinality. Consider a school music concert in which students may buy any number of tickets. The relationship between STUDENT and TICKET in both directions are optional. A student may not buy any ticket. Also, a ticket may not be sold out at all In conclusion, there are four possible cardinalities: mandatory one, mandatory many, optional one and optional many, as shown in Fig.6.5. See more examples in Fig.6. Mandatory One : (One and only one _— Mandatory Many — M 1 Chapter 6 ER Diagrams 77 ‘There are four possible ‘ardinalties: mandatory one, mandatory many, optional ‘one and optional many. Quick Check 6.6 1. Inthe relationship between AUTHOR and. ~ Optional One _ . Optional Many M rE BOOK, both sides are 2. Inthe relationship between BOOK and ORDER, the mandatory side is = and the optional sie is, 78 Part Database Design ‘One-to-one relationship: Optional instances on both sides. Student may buy at most one lunch box. Each lunch box may or may not be sold toa student. ‘One-to-one relationship: Optional instance on ‘one side, mandatory instance on the other side. A teacher may or may not be in charge of a class. But, each class must be in charge of by cone teacher. ‘One-to-one relationship: Mandatory instances (on both sides. Each student must have a student card and each student card belongs to exactly one student ‘One-to-many relationship; Optional instances on both sides ‘A student may borrow some books from the library. A book inthe library may be borrowed by at mosta student. ‘One-to-many relationship; Optional instance on one side. ‘There must be at least one student for a summer course to exist, Enrollment by students is optional. But, a student may apply for at most fone summer course. ‘One-o-many relationship: Optional instance on one side, A sstudent may be assigned to some posts, like ‘monitor, perfect or club chairman. Each post must be responsible by one student. ‘One-to-many relationship; Mandatory instances on both sides Acclass is formed by a group of at least one student. Each student is allocated to one and only one class. “Many-to-many relationship: Optional insances on both sides ‘Asstudent may apply for more than one scholarship. Each scholarship may receive some applications from students, or none. “Many-to-many relationship; Optional instance ‘on one side. ‘An event in the sports day is participated by at least one student (Otherwise the event will bbe cancelled). Each student may parti some event. “Many-to-many relationship; Optional instance ‘on one side. ‘Astudent takes at least one course. A course is taken by at least one student. ig.66 Ten examples of relationships Example 1 Example 2 chapters ER Diagrams Ina school, students are allocated to different classes. Each student must be allocated to exactly one class, and a class is formed by at least 30 students. Each class must be managed by several different students, namely, prefect, 1* monitor, 2"! monitor and 3" monitor. Draw an E-R diagram for the school, indicating cardinality. The first step is to classify the prefect and monitors into an entity class, CLASS_POST. The relationship between CLASS and CLASS_POST is mandatory in both directions because each class must have several posts and these posts cannot exist without a class. The relationship between STUDENT and CLASS is also mandatory in both directions, because student must be allocated to one class and a class must consist of at least one student. A student may or may not be assigned to a post. Therefore, it Is optional on the side of CLASS_POST. A post must be assigned to a student. Therefore, it is mandatory on the side of STUDENT. Wing Lee Inc. is a construction company with over 1000 employees. ‘A customer can hire the company for more than one project, and employees sometimes work on more than one project at a time. Equipment is assigned to only one project. Draw an E-R diagram for ig Lee Inc., indicating cardinality. It is reasonable to assume that a construction project involves more than one employee and requires more than one equipment. 79 PartB Database Design Example 3 Solution A library is going to use a computer system to handle the circulation. Each user may borrow several books at each time. Each book is recorded separately by a librarian into a check-out record, which contains data about the book, borrower, librarian, and dates (borrow and return). There are three librarians working on three shifts. (a) Draw an E-R diagram to show the relationships between BOOK and USER, indicating cardinality. (b) Identify two other entities for the library information system and draw another E-R diagram to represent the relationships between these entities, showing the cardinality of each relationship. (@ ‘The relationship between BOOK and USER Is optional because a book may never be borrowed out, and a user may never borrow any book. (b) Since each check-out has attributes, like date, borrower, etc. check-out is an entity. Another entity is librarian. CHECK_OU ‘The direction from LIBRARIAN to CHECKOUT is optional because a librarian may never handle any check-out (e.g. newly employed or the system is just started). However, the direction from CHECK_OUT. to LIBRARIAN is mandatory because each check-out must involve one librarian, Similarly, each check-out must involve one book and one user. ee ee Choper 5 ip The degree of a relationship is the number of entities participating in that relationship. All the relationships mentioned above involve two different entities. Therefore, they are of degree 2 and called binary relationships. Binary relationships are the most common. Unary (degree 1) relationship involves one entity. The following are two examples of unary relationship: @ ) Fig.6.10 Unary relationship In Fig.6.10a, a person may (or may not) be married to another person. If they are married, they have exactly one spouse. Therefore, the relationship is one-to-one and optional in both directions. In Fig.6.10b (Read the relationship from top to bottom through the relationship name), an employee may supervise more than one staff. The term. “may” means that an employee may or may not supervise other employee at all. Therefore, optional-many is used below EMPLOYEE. When the statement is read in the reverse order, an employee must be supervised by another employee. In our example, each staff belongs to one department each of which must have a department head. Therefore, mandatory-one is used above EMPLOYEE. ‘Ternary (degree 3) relationship and higher degree relationships are beyond the scope of this book. Fig.6.11 shows the typical shape of ternary relationship. -R Diagrams 81 “The degree of a relationship Is the number of entity classes participating in that relationship, Binary relationships involve {two entities and are the most ‘common. Unary relationships Involve one entity Fig6.11 Ternary relationship terna Quick Check 6.7 ‘The numbers of entities involved in unaty, binary and ry relationships are__, and ___ respectively. 82 Part B Database Design \EeeMultiple Relationships In some situations, there are more than one relationship between two entities. Consider the following example: @ ©) Fig6.11 Two relationships between two entities Refer to Fig.6.L1a, A student may get more than one scholarship (or may not get any scholarship). A scholarship must be awarded to exactly one student. Refer to Fig.6.11b. A student may apply for more than one scholarship. A scholarship may be applied for by more than one student. ‘The two relationships can be drawn on the same E-R diagram as shown in Fig.6.12. Fig6.12 Multiple relationships Chapter 6 ER Diagrams 83 re AU A An attribute is shown using an oval connected to an entity. Key attributes are underlined. When an entity is transformed into a database table, the key attribute will be the primary key of the table. A relationship is formed by linking the foreign key of a table to the primary key of another table. In general, if a relationship has been drawn, to link two entities and the first entity has the key attribute, then it is not necessary to draw the foreign key for the second entity. In other words, the second entity is assumed to have an attribute with values matching the key attribute of the first entity. ‘The following are some guidelines in drawing attributes: 1. Use noun to represent attributes. Attributes must be unique. Every basic entity must have a key attribute. Basic entities are entities originally identified and represent fundamental objects in an organisation, like STUDENT, BOOK, LIBRARIAN etc. 4. Amattribute that represents the foreign keys of a relationship need not be drawn (see Example 4 below). 5. Attributes derivable from other attributes need not be drawn. For example, if Date_birth has been recorded, ace or Year_birth are derived attributes that can be ignored (see Example 4 below). 6. Clearly indicate multi-valued attributes. For example, Qvalification is a multivalued attribute for a teacher because a teacher may obtain several qualifications. Use double-oval to indicate multi-valued attributes (See Example 5 below). Example 4 In a school, a student may be assigned with one or more functional posts, like prefect, monitor or chairman. A post must be assigned to exactly one student. Draw a complete E-R diagram to represent the relationship between STUDENT and POST, indicating cardinality. Solution The entity POST has foreign key stud_tb. But, we don’t need to draw foreign key in E-R diagram, because the relationship has associated stud_ID with Post_ID. Also, we don't need to include the derived attributes Year_birth in STUDENT (see Fig.6.14). Foretgn keys are usually not ‘drawn in ER diagrams. Quick Check 6.8 Foreign keys need/need not® be drawn in E-R diagrams, Derived attributes need/need not* be drawn in E-R diagrams, attributes are represented by double-oval, 4, entities must have a key attribute. “delete inappropriate 84 Part B Database Design Fig.6.14 Stud_[D in POST isa foreign key; Year Birth in STUDENT is a derived attribute uble-oval is used to icate multi-valued Example 5 Solution In a school, a teacher teaches in one or more classes, each of which is taught by one or more teachers. A teacher may have more than one qualification. On the other hand, each class must be responsible (lead) by one class teacher. Sketch an E-R diagram to show the relationship between TEACHER and CLASS, indicating cardinality. Alternative 1 Single relationship Fig6.15b ‘The difference between the above diagrams is that Class teacher 1D is added to CLASS in Fig.6.15a. This is necessary because the relationship “Teaches” does not represent the class teacher. In Fig.6.15b, class_teacher_rp needs not be drawn explicitly because a new relationship is added to associate class_1D with Teacher_1D primarily to describe class teacher. Chapters ER diagrams 16 Arclationship with an attribute Some relationships may have attributes. This commonly occurs in many-to-many relationships. Let's consider the M:N relationship between WAREHOUSE and PRODUCT shown in Fig.6.16. A warehouse may store several kinds of products; and each kind of product may be stored in several warehouses. Attribute Quantity is added to the relationship to record the quantity of a kind of product in a warehouse. As a general rule, relationships with attributes should be converted into entities, The newly created entity is known as associative entity. The entity is associative because it is converted from a relationship which is an association between two entities. It is an entity because it has instances each with distinguishing attribute values (see 6.1 for requirements for an entity). A feature of an associative entity is that the primary key is formed by a combination of two foreign keys. As mentioned in 6.2, foreign keys are not shown in E-R diagrams. Therefore, associative entities are drawn without any key attribute. Fig.6.17 and 6.18 show two equivalent E-R diagrams in which a new entity STORAGE is created to replace the original relationship Stores. Relationships with attributes should be converted into entities. Entity converted from a relationship is called associative entity. The primary key of an associative entity is formed by a combination of the foreign keys. 56.17 Arelationship 6.18 Equivalent ER diagram converted into an entity Quick Check 6.9 An associative entity is converted from a ‘The primary key ofan associative cenity is 86 PartB Database Design. —__ - Example 6 A joint project is hold by a school and a voluntary service organisation. The organisation offers three types of voluntary services to students in the school. Students may participate in more than one such services. But, the teachers will adjust the services chosen by students so that the number of participants in each service is more or less the same. Score will be calculated for each student based on the number of hours worked and the type of services at the end of the school year. (a) Draw an E-R diagram to show the relationship between STUDENT and SERVICE and include the necessary attribute(s) that enable(s) the score to be calculated, indicating cardinality. (b) _ Re-draw the E-R diagram with a new entity converted from the relationship in (a). Solution (b) EHED ous werk) rr 6.4 RESOLVING E-R DIAGRAM FOR RELATIONAL DB Chapter 6 E-R Diagrams 87 ‘The ultimate purpose of drawing E-R diagrams is to design a database. Some entities can be transformed into tables directly, while others need some conversions. For instance, consider an entity with a multi-valued attribute. Since we do not know the number of attribute values in each instance (record), we have to use one row for each attribute value. As a result, an instance will have multiple rows. Or, a row will have multiple rows. This is not allowed in relational databases. Similarly, many-to-many (M:N) relationships cannot be implemented directly without conversion. In an M:N relationship, we cannot tell the number of rows for a foreign key field in each record. This ends up with multiple rows in a row, similar to the case of multi-valued attributes above. The process of converting an ER diagram into a form that makes it possible to transform into a relational database is called resolution. You will see that all resolutions involve creating new entities. In fact, the discussion in 6.3 is a resolution that converts an M:N relationship into multiple 1:M relationships by creating an associative entity We shall discuss the resolutions of three types of relationships: binary MN relationship, multi-valued attribute and unary M:N relationship. \csResolving inary MN Relationship As a general rule, an M:N relationship must be converted into multiple 1:M relationships. The technique is to create a new entity to replace the original MN relationship. shows a binary M:N relationship between CUSTOMER and PRODUCT: A customer may have purchased more than one product. On the other hand, a product may be purchased by more than one customer. Fig6.21 Many-to-many relationship Quick Check 6.10 1 Resolution is to convert an ER diagram into a form transformable into a relational database, ‘An MIN relationship must be Converted into multiple 1:M relationships, by creating a new entity. Multi-valued attributes cannot be represented in a ‘An MIN relationship must be resolved into ‘0 oF more: relationships. 38 [Park B |Dakabase Design |e cesses esses Case 1 Each sales order includes one product onl} Assume that in each sales order, a customer will purchase one and only product. We will add an entity ORDER to show the details of the purchase. The ER diagram is redrawn as follows: Quick Check 6.11 In resolving an M:N relationship, anew __ is created, where the primary key is formed by _ “Two One: many relationships In Fig.6.22, ORDER is converted from the relationship Orders in Fig.6.21. You need not create a new primary key for ORDER because you can make use of the unique combination of foreign keys, Cust_tD and Prod_rp, to form a primary key. Yet, in our example, tnvoice No is included just to make it more close to real business situations. Note: The many-relationship of CUSTOMER in Fig.6.21 has been changed into an one-relationship in Fig,6.22. The same happens to PRODUCT too. This is a rule of thumb which you can use to verily that your resolution is correct. Case 2 Each sales order includes more than one product If a customer can purchase more than one product in each sales order, the E-R diagram will still contain M-N relationship as shown in Fig.6.23. Details are removed for clarity purpose. It is necessary to further resolve the relationship between ORDER and PRODUCT. Since an invoice often contains several lines each describing one product, we add an entity called ORDER_LINE as shown in Fig.6.24. = Chapter 6 E-R Diagrams 89 Fig6.24 Resolved ER diagram Notice that ORDER_LINE does not need an extra primary key because the combination of Invoice No and Prod_ro forms a primary key. The attribute bate is placed in ORDER because all the products in the same sales order are sold on the same day. On the other hand, the attribute quantity is placed in ORDER_LINE because customers may require different quantities for different product Example 7 In a hospital, a group of physicians are responsible for admitting new patients, while all the physicians will treat patients. Each new patient is admitted by exactly one admitting physician. Once admitted, a patient must be treated by at least one physician, who may or may not be the admitting physician. Whenever a patient is treated by a physician, the hospital wishes to record the details of the treatment, including Date, Time, and Results. Draw an E-R diagram without M:N relationships for the hospital, indicating cardinality. Solution Step 1: sketching two relationships bet jg.6.25 Relationships between physicians and patients 90 Part B Daiabase Design Step 2: Resolving the M:N relationship into multiple 1:M relationships Fig.6.26 Resolved relationships between physicians and patients \BeseResolving Multivalued Attribute As explained before, multi-valued fields are not allowed in relational Multi-valued fields are database. The technique is to create a new entity to represent the multi- not allowed in relational i preatces valued attribute. ‘Anew entiy Is created 0 For example, a company may have several contact persons. The attribute represent the multi-valued Contact_Nane is multi-valued as shown in Fig.6.27. atibute Fig.6.27 Entity with a multivalued attribute To resolve multi-valued attribute for this situation, create a new entity CONTACT. PERSON as shown below: Quick Check 6.12 In resolving multi-valued attributes, a new created, where the primary key is formed by 28 Resolved relationship —____ chopter6 ER Diagrams 1 Another example of multi-valued attribute is the qualification(s) . Bachelor degree, master degree, teacher training) owned by a teacher (see Fig.6.29a), The attribute qualitication should be stored inside a new entity QUALIFICATION which describes the qualifications in general, Quali_1p is a unique code that represents a general qualification, like “B.Sc (Hons qualification (b) The relationship Is_certified between TEACHER and QUALIFICATION is many-to-many (see Fig,6.30). Fig.6.30 M:N relationship ‘The subsequent steps will be similar to converting an M-N relationship into two 1:M relationships. Fig.6.31 shows a new entity CERTIFICATE added. It is an entity converted from the relationship Is_certified. This entity does not require a new primary key, because it includes both foreign keys Teacher_1D and oua1i_zD. The combination of these two foreign keys is sufficient to uniquely identify records in CERTIFICATE. sible to add some extra ion, like Cert_ ID 1 CERTIFICATE 1 to make it more relationship The second example above Is more complicated than the first one because qualification is a general entity that can exist independently. However, the contact persons are dependent on the company. While the same qualification may be owned by different teachers, a contact person belongs to exactly one company. 92 PartB Database Design p Keypoints Unary MEN relationship must be resolved by creating a new entity. Fig.6.32 Anunary M:N relationship 356.33 Resolved relationship (Quick Check 6.13 In resolving an urnary M:N is created relationship, anew In a company, an employee always co-operates with other employees. ‘Therefore, we have the unary M:N relationship as shown in Fig.6.32. ‘As employees are working in groups while they co-operate with one another, we can create a new entity GROUP to resolve the relationship. Case 1 Each employ me group onl Case 2 -mployee may be assigned to Since the relationship Cooperates.in is an M:N relationship as shown in Fig.6.34a, we need to resolve it further into two 1:M relationships, as shown in Fig.6.34b. (2) MEN relationship (b) Final resolution Fig.6.34 Resolved relationship Chapters ER Diagrams 93 Example 8 A university has a large number of courses in its catalog. Each course may have one or more different courses as prerequisites, or may have no prerequisites. Similarly, a particular course may be a prerequisite of any number of courses, or may not be prerequisite of any other course. (@) Sketch an E-R diagram that includes COURSE as the single entity, indicating cardinality. (b) Sketch another E-R diagram that includes COURSE and PREREQUISITE as entities. State the necessary attribute in PREREQUISITE. Solution (a) Requires Fig.6.35 ER diagram with one single entity o Fig6.36 Resolved relationship 24 PartB Database Design Answers to Quick Check 6 Quick Check 6.1 tables, fields Quick Check 6.2 1. attributes, instances 2. association, entities, key attribute Quick Check 6.3 instances, derived, process Quick Check 6.4 calculation, transfer, send, summarize Quick Check 6.5 1, _I:M (An author could write more than one book; each book is written by one author) 2. 1:1 ach book has an ISBN; each ISBN identifies a book) 3. MIN (An order may involve more than one book; a book may appear in more than one order) Quick Check 6.6 1. mandatory (Books must have an author; An author must have written a book) 2. BOOK (An order cannot have no book; A book may never appear in any order) Quick Check 1253) Quick Check 6.8 1. need not 2. need not 3. Multi-valued 4. basic Quick Check 6.9 relationship, a combination of the foreign keys Quek Cheek 6.10 1, relational table 2. 1M Quick Check 6.11 entity, combining the foreign keys of two tables Quick Check 6.12 entity, combining the foreign key with its own key attribute Quick Check 6.13 entity Chapter 6 E-R Diagrams 95 Binney + Anentity-relationship (E-R) diagram is a graphical representation of data for an organisation at conceptual level + The requirements for an entity are 1. one or more attributes 2, many possible distinct instances. * Arelationship is an association between two entities based on a key attribute. + Cardinality specifies the maximum and minimum numbers of instances of an entity participating in an association with another entity. + There are three basic types of relationships that specify the maximum. number of instances: one-to-one (1:1), one-to-many (1:M) and many-to-many (MEN) + There are four possible cardinalities: mandatory one, mandatory many, optional one and optional many. + The degree of a relationship is the number of entity classes participating in that relationship, + Binary relationships (degree 2) involve two entities and are the most common. Unary relationships (degree 1) involve one entity. + Ifa relationship has been drawn to link two entities and the first entity has the primary key, then it is not necessary to draw the foreign key for the second entity. + Relationships with attributes should be converted into entities. ‘+ Entity converted from a relationship is called associative ent + For an associative entity, the primary key is usually formed by a combination of the foreign keys. + Resolution is a process of converting an ER diagram into a form that makes it possible to transform into a relational database. + An M:N relationship must be converted into multiple 1:M relationships. The technique is to create a new entity to replace the original M:N relationship. + Multi-valued fields are not allowed in relational database. The technique is to create a new entity to represent the multi-valued attribute. + Unary MN relationship must be resolved by creating a new entity 96 PortB Database Design © Peas ‘Multiple Choice Questions 1. An E-R diagram represents data model. ‘a database schema, an entity a relationship, pomp 2. Abinary relationship involves A. two relationships. B. _bwo different entities C. twoatributes. D. more than two entities, 3. When a resolution takes place, ‘A. anew entity is created, B. a newattribute is created. CC. atelationship is converted into an attribute, D. _anentity is removed. 4, Amassociative entity ‘A. has a primary key. B, is converted from a relationship. C. represents a fundamental object. D. 5. Which of the following types of relationships should be resolved? ‘A, one-to-one binary B. one-to-many binary C.—many-to-many binary D. relationship without any attribute 6. Which of the following types of attributes should be resolved? key attribute rnon-key attribute single-valued attribute multi-valued attribute pope 7. After resolution, which of the following should disappear from an E-R diagram? A. N:M relationship B. 1:1 unary relationship C. 1:1 binary relationship D. _1:M binary relationship Study the paragraph below to answer the following five questions: ‘A doctor owns a clinic located in Causeway Bay. There are approximately 100 clients. The doctor records the name, address and phone number of each client on a paper card. For cach interview, the doctor will retrieve the paper card of the client and record the date, symptoms, diagnostic results and medications on the paper card. Assume that there is a set of well-known symptoms used by doctors. An E-R diagram is drawn to find out the overall structure of data during the phase of conceptual data modeling. In the diagram, a relationship is set up between CLIENT and INTERVIEW. Chapter 6. E-R Diagrams 97 8. Which of the following can be included as an entity? A. date of interview B. paper card C. symptoms D. theclinie 9. Which of the following should NOT be included as an attribute? ‘A. name of client B. date of interview C. diagnostic results D. Causeway Bay 10. The type of relationship in the direction from CLIENT to INTERVIEW is ‘A. one-to-one B, one-to-many CC. many-to-one D. many-to-many 11. The cardinalities on the side of CLIENT and INTERVIEW are and respectively. ‘A. optional, optional B. optional, mandatory C._-mandatory, optional D. mandatory, mandatory 12, Which of the following should be an attribute included in INTERVIEW? A, name of client B. diagnostic results C. name of the doctor D. number of visits by a client Conventional Questions 1. Acredit-card center cooperates with some banks to offer credit cards to users. Fig.Q6.1 is ‘an incomplete E-R diagram shown three entities: banks, credit cards and users. Each bank issues a number of credit cards, each with a unique identification code and expiration date. A credit card must be issued by one and only one bank and belong to one and only ‘one user. Each user must own at least one credit card. Users are identified by HKID and ‘banks are identified by a unique code. Complete the E-R diagram. Attributes like user name, address, bank name should be included. = = 2. In an auction of paintings, all participants must be registered, recording their names, HKID, addresses and contact phone numbers. The paintings come from several famous painters. Each painting has a detailed description. Each painter has one or more paintings to be auctioned, Participants may purchase one or more paintings, or may not purchase any painting. Sketch an E-R diagram to represent auction, resolving all the N:M relationships. 98 PartB. Database Design 3. Peter is a database designer. He is going to develop an E-R diagram for a concert hall in ‘Tung Chung. Three entities have been identified as follows: ‘CONCERT Each concert represents a performance of one or more compositions ona given date. Identifier is Concert_ID. Each concert must be ‘conducted by one conductor. COMPOSITION Compositions to be performed at each concert. Identifier is Composition_ID. Other attributes include name of composer, name of composition. CONDUCTOR Person who will conduct the concert. Identifier is Conductor_ID. Other attributes include name of conductor. A conductor may conduct, ‘more than one concert, or may not conduct any concert An initial E-R diagram is drawn as follows: (2) Complete the E-R diagram to show the atributes and cardinalities (6) Resolve the E-R diagram to climinate any N:M relationship. 4, Aprivate tennis club has ten tennis courts that allow members to use. Booking from members is accepted within one week before the tennis court is used. In each booking, each member can reserve at most 3 tennis courts and the duration is a multiple of half-hour. Given that two entities are identified: MEMBER A member of the tennis club. Identifier is Member_ID, Other attributes are name of member and contact phone. couRT ‘A tennis court to be used by members. Identifier is Court_ID. (@) Sketch an initial E-R diagram to show the relationship and cardinality between MEMBER and COURT. (b) Redraw the E-R diagram to include an entity BOOKING with attributes including date and time of use and duration of booking, 5. Ina country, all children must receive one vaccination against VX1. In Kingman Hospital the job of vaccination is handled by a group of five nurses. Children who have received the vaccination must be recorded so that they would not receive vaccination more than once. The record includes the HKID of the parent. It also inchudes date andthe name of the nurse in charge of the vaccination (@) Explain why the HKID of parent cannot be a key atribute for entity children Hence, suggest a key attribute for the children (©) Draw an E-R diagram to represent the structure of data about vaccination in the hospital 6. The principal of a school wants to record the siblings (brothers/sisters) of students, studying in the same school currently. Siblings of students who are not studying in the ssame school currently need not be recorded. (a) Draw an initial E-R diagram with one entity to represent the data model about siblings of students (b) Resolve your E-R diagram so that it can be implemented in a relational database. Chapter 6 E-R Diagrams 99 7. Ina post secondary institute, each student takes at least four courses. A course must hhave at least one student and is taught by one and only one lecturer. Each student will be assigned a supervisor who is the lecturer of one of the subjects taken by the student, The diagram below shows three entities identified Fig.Q6.3, (@) Complete the diagram to build a data model that represents the institute, (b) Does your E-R diagram need resolution? If yes, state how resolution is carried out, but do not draw a new E-R diagram. 8. The following describes a real estate firm: ‘* The firm has a number of sales offices in several districts, Attributes of sales office include Office_ID and Location. ‘+ Each sales office is assigned one or more employees. Attributes of employee include Employee_ID and Employee_Name. An employee must be assigned to only one sales office, ‘+ Foreach sales office, there is always one employee assigned to manage that office. ‘+ The firm lists property for sale. Attributes of property include Property_ID and Location. ‘+ Each unit of property may be listed in one or more sales offices. (a) Draw an E-R diagram using the above entities to represent a data model for the firm, (b) Does your E-R diagram needs resolution? If yes, state how resolution is carried out, but do not draw a new E-R diagram, 9. Wing Kee Inc. is an engineering firm with approximately 300 employees. A database is required to keep track of all employees, projects assigned and departments worked in, + Attributes of each employee include a unique employee code, name, address and {jb ttle (like engineer, secretary, and so on). + There are 1] different departments each with a unique name and a phone number. ‘An employee can report to only one department. + Each department may deal with several vendors. A vendor typically supplies equipment to more than one department. It is necessary to store the name and address of each vendor and the date of last meeting between a department and a vendor. + Many employees can work on a project. An employee can only be assigned to at ‘most one project at atime. Projects are distinguished by project numbers and the ‘estimated cost of each project must be store. Using only four entities, draw an E-R diagram that models the data of the firm.

You might also like