08-CA-M3C

advertisement
08 Computer Applications
M3C: Introduction to Database Design Methodology (3)
M3C: Introduction to Database Design Methodology (3)
Creating ER Diagrams
Softcopy of handouts can be found at http://www.clsmss.edu.hk/~whlau/08CA/
Simple ER Diagrams
The following examples are extracted and modified from textbook, pp.79-80. Create simple ER diagram without
attributes for the following examples.
1. Example 1:
In a school, students are allocated to different classes. Each student is allocated to one and only
one class and each class is formed by about 30 students. Each class must be managed by several
different students, namely, officials of class association and monitors. Draw an ER diagram for
the school, indicating cardinality.
a) Identify the entities. Use entity class for officials of class association and monitors.
b) Link the entities with relationships.
c) Determine the cardinality and optionality of the relationships.
2.
Example 2:
A customer hires a construction company with over 1000 employees for more than one project
Employees sometimes work on more than one project at a time. Each equipment is assigned to
one project only. A construction project may involve more than one employee and require more
than one equipment. Draw an ER diagram for the company.
[07.10.03] CLSMSS/0708/S7CA/M3C(03)
P. 1/6
08 Computer Applications
3.
M3C: Introduction to Database Design Methodology (3)
Example 3:
A library is going to be computerized.
Each user may borrow several books at the same time.
The check-out of each book is handled separately by a librarian, and the check-out record contains
data about the book, borrower, librarian, and dates of borrow and return. There are more than
one librarian at the circulation counter.
a) Draw an ER diagram to show the relationships between BOOK and USER.
b) There are two more other entities involved in this example, namely, LIBRARIAN and
CHECKOUT. Draw another ER diagram for all the entities.
ER Diagram with Attributes
4. A relationship is formed by linking the foreign key of a table to the primary key of another table.
If a relationship has been drawn to link two entities and the first entities has the key attribute, then
it is not necessary to draw the foreign key for the second entry. (Ref-1: pp.83-84)
a) Derived attributes can be indicated by dashed oval but they should not be stored. A
derived attribute is one which can be derived from other attributes. E.g. Age, which can be
derived from data-of-birth and the date today.
b) A multi-valued attribute may contain more than one value. For example, a teacher may
have obtained several qualifications. Multi-valued attribute is represented by double oval, and
should be resolved by normalization.
Age
Student_ID
Name
Address
Name
Qualifications
Name
Qualifications
STUDENT
1
Post_ID
Assigned_to
M
POST
Post_Name
Date_of_Birth
Teacher_ID
TEACHER
Teacher_ID
Class_ID
M
M
Teaches
Teaches
M
Class_teacher_ID
Class_ID
M
TEACHER
CLASS
1
[07.10.03] CLSMSS/0708/S7CA/M3C(03)
CLASS
Manages
1
P. 2/6
08 Computer Applications
M3C: Introduction to Database Design Methodology (3)
Attributes that Describe Relationship
5. In some cases, it may appear that some attributes are describing a relationship rather than an entity.
A relationship with attributes should be converted into an entity. (Ref-3, “Attributes”; Ref-1, pp.85-86)
6.
For example, a student may borrow some books from the library, and the checkout date and due
date should be recorded:
Checkout
_Date
Due_Date
Return_
Date
Student ID
Book_Number
STUDENT M
Name
M
Borrows
BOOK
Book Name
In this case, we have to reclassify the relationship as a new entity, which is
a child to both original entities. The newly created is called an associative entity.
Checkout
_Date
1
BOOK
Is_checked_
out
M
Due_Date
Return_
Date
CHECKOUT
M
Checked_
out_by
1
STUDENT
An associative entity
7.
Example 4: (modified from example 6 in p.86 of the textbook)
In school, students are encouraged to participate in some voluntary services. Teacher may adjust
the services chosen by the students, such that the number of participants in each service is about
the same. At the end of a school year, the teacher will calculate the score of each student based
on the number of total hours worked and the type of services.
Student ID
Name
STUDENT M
Total_hours
_worked
Participates_in
Service_ID
M
SERVICE
Service_Name
Redraw the ER diagram with an associative entity added to replace the relationship.
[07.10.03] CLSMSS/0708/S7CA/M3C(03)
P. 3/6
08 Computer Applications
M3C: Introduction to Database Design Methodology (3)
Splitting Many-to-many Relationships
8. Relational model cannot represent many-to-many relationships.
Therefore, they must be resolved
early in the modeling process by replacing the relationship by an associative entity.
(Ref-1, pp.87-90; Ref-2, Ch.2; Ref-3, “Refining the E-R”)
9.
For example, one or more employees may be assigned to one or more project:
EMPLOYEE
M
M
Assigned_to
PROJECT
There may be other problems. For example, there may be attributes such as the start date and
finish date of assignment associated to the relationship.
The many-to-many relationship should be replaced by entity “Assignment”:
EMPLOYEE
1
Are_given
M
ASSIGNMENT
M
Assigned_for
1
PROJECT
10. Example 5: (extracted from Fig.6.21 in pp.87-88 of the textbook)
A customer may have purchased more the one product, and a product may be purchased by more
than one customer.
Customer_ID
Name
CUSTOMER M
Date
Quantity
Orders
Product_ID
M
PRODUCT
Description
a)
Case I: Each order includes one product only, and assigned a unique order number.
b) Case II: Each order may include more than one product. However, no matter how many
products are involved in an order, each order is assigned one unique order number only.
[07.10.03] CLSMSS/0708/S7CA/M3C(03)
P. 4/6
08 Computer Applications
M3C: Introduction to Database Design Methodology (3)
11. There may be cases that a unary relationship is many-to-many.
relationship is similar.
12. Example: (modified from example 8 in p.93 of the textbook)
In a university, there are many courses. Some
Course_code
courses may allow a student to enroll in the
course only if she or he has finished some
Title
courses, called prerequisites.
The method to split the
M
COURSE
Requires
M
Resolved relationship may be:
Required_course_code
Couse_code
Title
COURSE
1
Requires
M
PREREQUISITE
Resolving Multi-valued Attributes
13. Multi-valued attributes is not allowed in relational model, and thus if a multi-valued attribute is
dependent upon the primary key, we have to reclassify it as a new child entity. (Ref-1, pp.90-91; Ref-3)
a) If the multi-valued attribute is unique within the new entity, it becomes the primary key.
b) If not, migrate the primary key from the original, now parent, entity.
14. Example 6: (modified from Fig6.30 in p.91 of the textbook)
A teacher may have obtained one or more qualifications, e.g. “Msc” (Master of Science). There
may be more than one teacher having obtained the same qualification.
Name
Qualifications
Teacher_ID
TEACHER
It can be resolved into:
Qualification_Title
Teacher_ID
Name
TEACHER
M
Is_certified
M
QUALIFICATION
Description
Date_of_certification
However, this is a many-to-many relationship. Resolve it with a new associate entity.
[07.10.03] CLSMSS/0708/S7CA/M3C(03)
P. 5/6
08 Computer Applications
M3C: Introduction to Database Design Methodology (3)
15. Example 7: (Ref-3)
Each project involves one or more tasks (e.g. “analysis”, “implementation”, “testing”, etc.).
Project_ID
Project_Name
Tasks
PROJECT
Resolve the multi-valued attributes into a new entity.
relationship.
Further resolve the new many-to-many
Drawing ER Diagram with Software
16. Toad™ Data Modeler is a freeware for drawing ER diagram. It is free for personal use. The
program is able to export ER diagrams in RTF, HTML and image formats. It can also produce
SQL commands for creating database and tables, according to the ER diagram drawn by the user.
http://www.casestudio.com/enu/download.aspx
References:
Ref-1. Textbook: Raymond W. N. CHAN, Database for A/AS Level, Radian, pp.72-82. (*)
Ref-2. Database eLearning – Database Analysis (*)
http://db.grussell.org/ch2.html
Ref-3. Introduction to Data Modeling (*)
http://www.utexas.edu/its/windows/database/datamodeling/index.html
Ref-4. Entity-Relationship Model
http://en.wikipedia.org/wiki/Er_diagram
Ref-5. Entity-Relationship Diagrams (ERD)
http://www.umsl.edu/~sauter/analysis/er/er_intro.html
Ref-6. The Entity-Relationship Model
http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter2/node1.html
Ref-7. Understanding Entity Relationship Diagrams (*)
http://folkworm.ceri.memphis.edu/ew/SCHEMA_DOC/comparison/erd.htm
Ref-8. Free Entity Relationship Diagrams Tutorial
http://www.getahead-direct.com/gwentrel.htm
Ref-9. Data Model Dictionary (*)
http://www.datamodel.org/DataModelDictionary.html
[07.10.03] CLSMSS/0708/S7CA/M3C(03)
P. 6/6
Download