Topic 8 Erd Views Visio-1
Topic 8 Erd Views Visio-1
Multi-Valued Attributes
Characteristic Entities
ERD Case Studies
Views
Visio
1
Multi-Valued Attributes
An ERDs may contain a multi-valued attribute (MVA) EmpNo
An MVA is an attribute surrounded by {braces}. EmpName
{ Skill }
A multi-valued attribute is used when an attribute may
have more than one value for an entity instance.
Eg. An Employee might have multiple skills etc.
EMPLOYEE
'Forklift Driver'
'Crane Operator'
'Welder'
2
Multi-Valued Attributes
A multi-valued attribute, is a shortcut way of representing the fact that an Employee may
have many Skills.
• Below is an example of the same ERD after expanding the MVAs.
EmpNo SkillName
EmpName
And we know that a M:M relationship must be converted if converting to a relational schema
EmpNo SkillName
EmpName
3
Characteristic Entity
Characteristic Entity:
A Strong Entity with
EmpNo SkillName only key (identifier)
EmpName attributes
4
Characteristic Entity
Some texts suggest that characteristic entities should be eliminated from an ERD.
After all, why bother with a separate table named Skill, when the data in the intersection
entity already contains ALL the data stored in a single row of Skill data? The Skill table does
not add any new data to the database.
EmpNo SkillName
EmpName
The values in the Skill table are used as a foreign key constraint.
The foreign key constraint will force every SkillName value in the EmpSkill
table to match a Primary Key value in the referenced Table.
SKILL (SkillName)
PK (SkillName)
6
Characteristic Entity
• Example: Using the Characteristic entity to force referential integrity
EmpNo EmpName EmpNo SkillName SkillName
15 Jim 15 Welder Electrician
17 Frank 17 Electrician Welder
19 Peter 17 Welder
War Story 1
– Pallets
– Streets / Aisles
– Bays / Slots / Locations
War Story 2
– Trucks & Split Trucks
8
Pizza Online
Case Study 1
9
Case Study - Overview
10
Case Study - Customer Details
– Name
– Address
– Credit Card No
11
Case Study – Order Details
12
Case Study - Customer Details
• Add any delivery instructions that are required for the order:
13
Case Study – The completed Order
This Order is for
Customer C2045.
CustId ItemCode
Name DateTime Descripton
Address DeliveryInstructions ItemPrice
15
Case Study – Order Key
16
Where to place the Quantity attribute
• We need to place the Quantity attribute on the ERD
– It doesn't belong to Item
• It would mean an item always has the same quantity. Silly
– It doesn't belong to Order
• It would mean a cust orders the same quantity of each item. Silly
CustId ItemCode
Name DateTime Descripton
Address ItemPrice
DeliveryInstructions
Quantity
17
Pizza Online M:M
Again we have a M:M relationship
The M:M must be expanded
ItemCode
CustId
DateTime Descripton
Name
Delivery Quantity ItemPrice
Address Instructions
ORDERLINE ITEM
CUSTOMER ORDER for
has
makes
18
Pizza Online relational schema
– ORDER( CustId, DateTime, DeliveryInstructions)
PK: (CustId, DateTime)
FK: (CustId) references CUSTOMER
ORDERLINE ITEM
CUSTOMER ORDER for
has
makes
20
Review: Weak Entity is central to solution
ItemCode
CustId
DateTime Descripton
Name
Delivery Quantity ItemPrice
Address Instructions
ORDERLINE ITEM
CUSTOMER ORDER for
has
makes
21
Review: Can't Order & Orderline be merged?
• Many students ask if Order and OrderLine should be merged.
• Answer: No.
• If merged every orderline would contain the delivery instructions
– Causing redundancy and potential update anomaly
• If the delivery instructions changed, then every row of the order would need to be updated.
CustId DateTime ItemCode
Name Delivery Instructions Descripton
Address Quantity ItemPrice
• Within the ERD Model and Relational Model the Order is identified by
the CustID and DateTime. These are real world attributes.
• When you actually come to build the database, then you may choose to
introduce a surrogate key such as OrderNo.
Obviously, if the business already uses order numbers, then OrderNo is a real world
attribute and can be used within the ERD and Relational model.
23
Glenferrie Toy Library
Case Study 2
24
Glenferrie Toy Library System
Let's re-examine the Glenferrie Toy Library.
• Previously each loan was for one item only
25
Toy Library ERD modified
What changes are required ?
PatronNo
ToyNo
FirstName DateTimeBorrowed
ToyName
Surname
StaffName YearAquired
Gender
26
Toy Library ERD modified
PatronNo ToyNo
FirstName DateTimeBorrowed
ToyName
Surname
StaffName YearAquired
Gender
27
DateTime Returned
• Suppose that we want to include a DateTime Returned value
– For each toy as it is returned
– Toys may be returned on different days
PatronNo ToyNo
FirstName DateTimeBorrowed
ToyName
Surname
StaffName YearAquired
Gender
28
DateTime Returned
– All items for that loan must all be returned on the same date
PatronNo
ToyNo
FirstName DateTimeBorrowed
StaffName ToyName
Surname
Gender DateTimeReturned YearAquired
29
DateTime Returned
PatronNo
ToyNo
FirstName DateTimeBorrowed
StaffName ToyName
Surname
Gender DateTimeReturned YearAquired
30
Student Enrolment
Case Study 3
31
Student enrolment problem
• Reconsider the student enrolment problem from prior weeks…
Grade
Address Description
Name Title
StuID SubCode
ExamDate
ConvenorID Description
Semester Title
Year SubCode
35
Analysis of student enrolment problem
– Subject INF10002 has one instance.
• There is only one subject INF10002
SUBJECT
of a SUBJECT
OFFERING
ExamDate
ConvenorID Description
Semester Title
Year SubCode
36
Analysis of Enrolment problem
SUBJECT
• Sample Data OFFERING
of a SUBJECT
ExamDate
ConvenorID Description
Semester Title
Year SubCode
Subject
SubCode Title Description
INF10002 Database Students who complete this unit of study should be able to:
Analysis and Describe the steps involved in database design and database
Design systems development.
Subject Offering
SubCode Year Semester ConvenorID Exam Date
INF10002 2020 1 Daphne Fowler 12 June 2020
38
Analysis of Enrolment problem
ExamDate
StuId Convenor Description
StuName
Semester Title
Year SubCode
39
Analysis of Enrolment problem
of
has SUBJECT
STUDENT ENROLMENT of
OFFERING
ExamDate
Enrolment Data: StuID StuName Grade
Convenor
Semester
1234567, INF10002 , 2019, Sem2, N Year
Venue AtristName
TourName
Performance DateTime TourID
TheatreName WrittenBy
Performance DateTime PlayName
PlayID
41
Is an 'offering' always required?
DateTime
Agenda
Location
MeetingID
42
Business Narrative
• Consider this narrative:
– Hawthorn University has a number of subjects.
– Each subject is identified by a subject code and has a title
– Each subject may be run each semester of each year
– Each semester the subject may have a new convener
– A student is identified by student no.
43
ERD Rules
• There are no FIRM rules to building a good ERD
• Should you spend time ruling boxes & lines for your first draft of an ERD?
– No (See above)
45
Building the ERD
• Where do you start?
– Follow numbered steps from the last few lectures OR
– Start drawing by what 'feels' right
– Use 'patterns' that you have encountered before.
• Think of assignments / lab questions / sample exam questions
46
Dealing with obvious weak entities
– Should you draw only the STUDENT and SUBJECT entities with a M:M
relationship?
Year
Semester
STUDENT Enrols in SUBJECT
ConvenorName
Grade
Description
StuName Title
StuId SubCode
OR should you ….
47
Dealing with obvious weak entities
SUBJECT
STUDENT Enrols in of SUBJECT
OFFERING
Grade
ExamDate
Address Convenor Description
Name Semester Title
StuID Year SubCode
Grade
StuName
StuId
ASSIGNMENT
DueDate
Description
AssId
49
Sequence of expansions
• This ERD shows the assignment having a relationship with Subject Before
expanding the M:M relationship
SubCode
Title
Year
Description
Semester
STUDENT Enroils in SUBJECT
ConvenorName
Grade
has
StuName
StuId
ASSIGNMENT
Due Date
Description
AssId
50
Sequence of expansions
• This ERD shows the assignment having a relationship with Subject Offering after
expanding the M:M relationship
Year
SubCode
Semester
Title
Convenor
ExamDate Description
SUBJECT
STUDENT Enrols in of SUBJECT
OFFERING
Grade
Address
Has
Name
StuID
SUBJECT
STUDENT Enrols in of SUBJECT
OFFERING
Grade
Address
Has
Name
StuID
ASSIGNMENT
Due Date
Description
AssId
52
Sequence of expansions
• This ERD shows the assignment having a relationship with Student before expanding the M:M
relationship. This seems OK
• It seems to make sense to place it between Student & Assignment.
• A student may do many assignments.
• An assignment is attempted by many students
Year
SubCode
Semester
Title
Convenor
ExamDate Description
SUBJECT
STUDENT Enrols in of SUBJECT
OFFERING
Grade
Address
Has
Name
StuID
ASSIGNMENT
Attempts
Due Date
but wait...
Description
AssId
53
Sequence of expansions
• This ERD shows the assignment having a relationship with Enrolment after
expanding the M:M relationship. This is better.
• It makes more sense to only allow an enrolled Student to attempt assignments.
This matches business rules.
StuID
STUDENT Name
Address
Year
has SubCode
Semester
Title
Grade Convenor
ExamDate Description
ENROLMENT SUBJECT
In of SUBJECT
OFFERING
Has
AssId
ASSIGNMENT Description
Attempts Due Date
54
Sequence of expansions
• The ERD from a few slides previous allowed ANY student to attempt an INF10002
assignment.
• Students not enrolled in INF10002 this semester
must not be allowed to submit a INF10002 assignment
Year
SubCode
Semester
Title
Convenor
ExamDate Description
SUBJECT
STUDENT Enrols in of SUBJECT
OFFERING
Grade
Address
Has
Name
StuID
ASSIGNMENT
Attempts
Due Date
Description
AssId
55
Which way is the right way?
• While drawing the ERD for the enrolment problem there were many decision
points.
– Should I expand now or later?
– Should I draw a relationship now or later?
56
Test your model
• Could you write these SQL queries?
• Once you ERD is complete
• Which student had the best Ass1 score in
– Does it match Business Rules? INF30004 this semester?
– Can it be Navigated? • Who was convenor when student 1234567
was enrolled in INF10002?
– Does Test Data work?
StuID
STUDENT Name
Address
Year
has SubCode
Semester
Title
Grade Convenor
ExamDate Description
ENROLMENT SUBJECT
In of SUBJECT
OFFERING
Has
makes
Score
AssId
ASSIGNMENT Description
of ASSIGNMENT
ATTEMPT Due Date
57
Views
Storing Complex SQL Statements
58
Views
When discussing SQL views, think back to MS Access
– creating and storing queries in MS Access.
– You simply double clicked on a saved query name and
it retrieved data from tables.
• The rows in the result set generated by the view are not saved.
• Each time the view is referenced, the SQL code in the view is executed.
60
Views
A view behaves just like a table
– Select columns from a view
– Use Where clause
– Use Join to link other tables and/or views
62
Views & security
• Imagine we have table Employee. It has a mixture of private & public data
63
Views & security
• Create Multiple views
– Emp_Anyone_View (green columns from previous slide)
– Emp_Mgr_View (Employee_Anyone_View + blue columns …)
– Emp_Payroll_View (Emp_Mgr_View + red columns)
– Finally
• allocate all staff to AllUsers group
• allocate management staff to Mgr group
• allocate payroll staff to Payroll group
64
Read Only View
• A simple way to create a read only view is to add the clause:
With Read Only
If a user attempts to delete rows from the employee table via EmpView
DELETE FROM EmpView
Then:
Error at line 1:
ORA-42399: cannot perform a DML operation on a read-only view
65
Views – Warning regarding updating tables
• It is possible to
– Insert rows into a view (actually rows are inserted into
the table the view is based on)
– Update rows in a view (ditto)
• Care is needed
– A table may have constraints on some columns where that column
is not in the view
– Example. This table specifies valid values for student gender
Student Table
StuId number Primary Key,
StuName varchar(100),
Gender varchar(1)
CONSTRAINT check_gender CHECK ( Gender IN ('M', 'F', 'I') ) …
66
Updateable & Insertable Views
Create a view based on the Student table
CREATE VIEW STUVIEW
SELECT STUID, STUNAME FROM STUDENT
• Similar problem(s):
– Inserting data into a View based on joined tables also needs careful thought and
implementation. Tricky!
– Search the topic "Updatable and Insertable Views " for more info…
• This semester, we will avoid the Updatable and Insertable view topic
67
Visio
Drawing ERDs the quick way…
68
Visio – The Quick Way
• To make a simple & good looking ERD in Visio you only need to know
a few steps:
70
Visio – The Quick Way
71
Visio – The Quick Way
72
Visio – The Quick Way
7. To change the ends of the line:
• Choose Format / Line from menu or
click the line and press Shift F3
• The most common line ends
are 25 & 29.
• Use 20 for Attributes
• Use Jumbo for size
74
Visio – The Quick Way
Note: The offset icon is not visible via standard menus in Visio!!!
So do the following…:
75
Visio – The Quick Way
10. Use the Group tool to glue objects together. They can then be 'moved as one'.
Choose UnGroup to edit objects individually.
76
Visio – The Quick Way
To copy the entire ERD Drawing to MS Word:
79
Visio – Using Intelligent Shapes
80
Visio – Using Intelligent Shapes
4. Some rectangles may require
many connection points
(for attributes)
• https://support.office.com/en-au/article/Connect-shapes-by-using-AutoConnect-or-the-
Connector-tool-6122a82b-8c1b-4965-b6e1-b15d26f8aab6
81