MS Visio 2007 tutorial detailed

advertisement
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
This paper describes the use of Microsoft Office’s Visio 7 to create Entity Relationship Diagrams (ERDs).
Objectives
1.
2.
3.
4.
5.
6.
Introduce Visio’s history and professional uses
Start a Visio session
Draw Entities (Tables)
Add Relationships between Entities
Refine the ERD relationships
Extended Entity Relationship Modeling (Supertypes / Subtypes)
Visio – History and Professional Uses
DBA 110 Database Concepts introduces the concept of database planning using Entity Relationship
Diagrams or ERDs. There are many tools which can be used to draw these ERDs. Even MS Word can
be used. The premier software tool on the market which can be used to draw the ERDs is Microsoft
Office’s Visio. Historically, Visio Corporation was founded in 1990 by ex-software developers from Aldus
Corporation of PageMaker fame with the first version of the Visio business drawing program sold in 1992
(Wikipedia). Microsoft acquired the Seattle-based company in 1999, and today the Vision 2010 product
line includes three major versions: Standard, Professional, and Premium Editions (Visio Versions). Visio
is used to create many types of drawings including building blueprints, street maps, project timelines,
program flowcharts, network diagrams, data flow / process flow diagrams, ER Diagrams, etc.
This paper will demonstrate the creation of crow's foot ER Diagrams with the Professional Edition of Visio
2007. Visio is available for Central Piedmont Community College IT students through ELMS at no cost to
the student. For additional help you may go to the webpage for the ITS Helpdesk or call at 704.330.5000.
Help with downloading the available Microsoft Corporation software products (free or low cost) via ELMS
is also generally available in the student labs.
Start a Visio Session
When you open Visio you are presented with a screen that is similar to the example on the next page
(with some variances depending on version, prior opened diagrams, etc.). For drawing to be done in this
course select a new drawing in the Database category using the Database Model Diagram template.
Once you have created a drawing, you may be opening that drawing again (usually listed in the right-side
pane). If you have used the Database template, it will be shown in the Getting Started work area in the
center of the screen. If you are opening Visio for the first time, you will need to click on the Software and
Database Template Category choice in the left pane of the screen.
NOTE: Diagrams produced in Visio can be copied and then pasted into Word. This will allow multiple
diagrams to be put into one document (with appropriate documentation added) and submitted for lab
purposes.
Page - 1
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Upon clicking on the Software and Database choice, you will see the following set of choices:
Page - 2
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Double click on the Database Modeling Template and you will see a screen similar to that on the following
page:
A template (sometimes called a stencil) is a set of common shapes and connectors commonly used in the
type of drawing specified. The number of shapes and connectors vary greatly depending on the chosen
template. Visio permits use of multiple templates on the same drawing if necessary. We will use only the
Database Modeling Template to draw our ERDs.
Notice that Visio has given you a blank worksheet (like graph paper) and the template containing the
basic shapes and connectors are in the pane on the left of the screen. Most crow's foot ER diagrams can
be created with just two of the seven basic database shapes and connectors: Entity and Relationship.
If your initial worksheet appears in the portrait orientation (as in this example), then you might want to
rotate it to landscape orientation to make more effective use of your rectangular screen. To do this, select
Page Setup option from the File drop-down menu and click on the Landscape choice button. In addition to
the worksheet orientation, Page Setup allows you to change the scaling, paper size, margins, etc.
Page - 3
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Entities
To begin, click on the Entity symbol in the Template pane on the left side of the screen, hold the click,
and drag the entity shape to your worksheet. You can reposition it at any time. You can delete any shape
by clicking it and pressing the delete key.
Click on the entity to view/edit its name, columns (fields), primary key, secondary indices, etc.
Page - 4
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Change the default name (Table1) of the entity to tblEMPLOYEE. Visio allows you to specify different
physical and conceptual names for each entity. Since the Sync Name property has been selected, you
only need to enter one new name for the table.
Page - 5
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
In the lower left corner you see a menu of options – click on the Columns (fields) category.
For the example we enter the names of the five fields in the table. The order of entry is not significant
because we can change the display order at will. Visio defaults to a data type CHAR(10) which is ten
characters of text for all new columns.
We will change the columns to their correct data types even though this will have no impact on the
diagram appearance. The default data types supported by Visio are Microsoft Access data types.
Page - 6
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Indicate the Required (Not Null) and Primary Key (PK) status for each field. Visio underlines the key field
and moves it to the top of the diagram. Visio also shows only the required fields in boldface. You may
change font sizes, font families, color, alignment, etc. as in almost all Microsoft Office Products.
Next we will use the same technique and create a second table called tblDEPENDENT.
Page - 7
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Relationships
Now that we have two tables drawn, we can define the relationship between them.
Page - 8
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Click the Relationship connector in the template, hold the click, and drag and drop it in any blank space in
the diagram.
.
The unattached relationship looks like a line bent at a 90 degree angle with an arrowhead at the top of the
line. The arrowhead will be changed to a crow’s foot later so don’t worry about it for now.
We must position the arrowhead FIRST on the parent table. tbl EMPLOYEE is the parent table. The
child table is tblDEPENDENT. It holds the foreign key (FK).
Page - 9
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Click the end of the relationship connector with the arrowhead. Drag it over tblEMPLOYEE until the
border turns red.
Drag the other end of the relationship connector over tblDEPENDENT.
To repeat: You must connect the tables in the proper order. The arrowhead must be on the one-side (i.e.,
parent-side) and the tail must be on the many-side (i.e., child-side) of every relationship.
In some instances where we did not use the same name (EMP_ID) in both tables, Visio would not be able
to discern the Foreign Key (FK). In that case we could click on the relationship to allow us to ‘tell’ Visio
about the FK.
Page - 10
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Highlight the key column on the left side (tblEMPLOYEE), highlight the key column on the right side
(tblDEPENDENT), and then click on Associate (no longer grayed-out) between the two table descriptions.
A nice feature of Visio is that we can change the positions of the tables (and relationships) by clicking on
the table to be moved, continue to hold the left mouse button down, and slide the table to the desired
position. The relationship line will move with the table.
Page - 11
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Refining the ERD
Many of the basic drawing style options can be accessed by selecting the Database Tab, the Options
drop-down menu choice, and the Document sub-choice. The Database Tab lets you select the
Relational symbols and it also lets you select which set of entity names to display. The Drivers subchoice lets you choose which common database upon which to base defaults such as Data Type.
Page - 12
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Under the Database tab, Options drop-down selection and Document choice we can now implement
Crow’s Foot diagramming. (Default settings are shown below.)
The Table Tab lets you select the items to display (e.g., PK, FK,
secondary indices, non-key fields, data types). You can also
elect to display the primary key fields at the top of each entity or
in their actual physical order.
The General Tab lets you select the IDEF1X or Relational
symbols. It also lets you select which set of entity names to
display.
The Relationship Tab lets you choose crow’s feet for the
relationship displays.
Page - 13
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Select the Crow's Feet option. Notice that the mother-child relationship is shown as mandatory on the
one-side and optional on the many-side. We will see how to change this later.
Visio now converts the arrowhead to the double hatch symbol for one and only one. The other end of the
arrow diagram becomes a crow’s foot symbol with the optional (zero) minimum cardinality.
Page - 14
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Consider the situation where we have both the father and mother as employees. We must add another
FK with a different name. Let’s use Father_ID and Mother_ID as FKs. Our refined table might look like:
Make sure that the first relationship is correct. Double click the relationship line, highlight EMP_ID on the
tblEMPLOYEE side and Mother_ID on the tblDEPENDENT side, and then click on Associate.
Page - 15
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Now add the second relationship by first dragging and dropping a new relationship line, associate it in the
correct order with the two tables, double click on the relationship, then make your choices (EMP_ID and
Father_ID, and click on Associate. The relationships below should appear.
Unfortunately, we now have two relationships that appear to be the same. Let’s label them. Visio lets you
add simple labels to individual relationships. To do this, double click a relationship, click on Name, and
then enter a descriptive name. The name is there, but not showing. Open the Database Tab, choose
Options, choose Document, choose Relationship, and then choose the Show Physical Name option.
We now have one name inserted properly, but the other relationship is displaying the formal name
supplied by Visio. Repeat the process for the second relationship.
Notice that the one-to-many mother and father relationships were created as mandatory on the one-side
and optional on the many-side. Change this by double clicking the relationship.
Page - 16
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
When you have double clicked on the relationship, choose Miscellaneous at the bottom of the screen.
For instance, you can force every tblEMPLOYEE record to have at least one child by changing the
mother relationship to ‘One or More’ Cardinality. You can force every parent to have between one and
nine children by selecting the Range cardinality then specifying the At Least and At Most frequencies.
Page - 17
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Initially, the mother and father relationships were defined as mandatory on the one-side because
Mother_ID and Father_ID were defined as required fields. If you want to allow a child to not have a father,
then de-select the Req'd checkbox for Father_ID. Now the relationship is optional in both directions.
Select the File/Save or File/Save As drop-down menu when you are finished with your diagram. Visio will
use .vsd as the extension for all drawing files. You can highlight a section of the work area containing
your diagram, right click and copy. You can then insert the copied diagram into a Word document. In this
fashion you can put numerous diagrams in one document with the appropriate documentation.
Extended Entity Relationship Modeling (Supertypes / Subtypes)
Examine the tables below. tblVEHICLE is the parent (super) table. tblAUTO and tblTRUCK are the child
(sub) tables. Advanced data modeling techniques allow a better level of understanding about the true
relationships between certain tables. In a more formal definition: an Entity Supertype is a generic entity
type that is related to one or more Entity Subtypes. The Entity Supertype contains the common
characteristics and the Entities Subtypes contain the unique characteristics of each Entity type. In the
following example, There is only one primary key (PK) – VEHVIN. All vehicles are identifiable by Make
and Model. All vehicles can be classified by Transmission Type. The subcategory of vehicles called
automobiles (tblAUTO) has categories and trunk information which is not needed by the subcategory of
vehicles called trucks (tblTRUCK). The Truck subcategory has information (bed size, bed liner (yes or
no), and towing capacity which is not needed by automobiles.
Page - 18
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
Auto and Truck are specializations of Vehicle. Both Autos and Trucks can inherit Make and Model from
Vehicle for instance. A Vehicle cannot be both a Truck and an Automobile. A way is needed to show
whether the sub tables are disjoint (the Vehicle can be found in only one of the sub tables) or
overlapping (the Vehicle can be found in one or more of the sub tables). We also want to be able to say
whether there is a partial constraint (the super type may or may not be in a sub type) or a complete
constraint (the super type must appear in a sub type). In the example on the previous page we can say
that a Vehicle must be either an Auto or a Truck (disjoint) and must be in a sub table (complete). The
partial constraint is represented with a circle on top of a single line and the total constraint is represented
by a circle on top of two lines. A lowercase ‘d’ in the circle represents disjoint and a lowercase ‘o’ in the
circle represents overlapping.
Look at the Entity Relationship symbols in Visio’s Database Template.
Drag and drop the Category symbol onto your drawing under the Parent
table.
Drag the Parent to category symbol to the drawing and drop it on the
Category symbol. You should see a small red box with a ‘x’ in it on the
Category circle. Click on the small green box with ‘+’ symbol at the top of
the connector and drag it onto the Parent table. Release the mouse
button when the Parent table is highlighted in red.
Drag the Category to child connector to the drawing and release the
mouse when the top of the symbol is highlighted in red on the Category
symbol (a note “Glue to Connection Point” briefly appears).
Click on the green end of the connector and drag it over one of the Child
Tables. Release the mouse when the Child Table turns red. Repeat this
procedure for each additional Child Table. You should now see a drawing
like the one below:
Page - 19
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
When you click on the Category (circle), you should see:
By default, None is the default setting for Discriminator. A Discriminator column is often used in the
Parent table to quickly point to the proper Child table.
Page - 20
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
We have such a column in the example (VDHType). The value ‘A’ will indicate that the VEHICLE is an
AUTO and conversely, the value ‘T’ will indicate that the VEHICLE is a TRUCK. Click on the radio button
for “This attribute”. The grayed-out column names will now darken. Click on VEHType to see:
Now click back on the drawing to see the Discriminator listing.
But notice that there is still a single line indicating a Partial constraint. In this case there should be a
Complete constraint. Look back to the complete Visio screen above. On the lower right side of the
Categories work area at the bottom of the screen you will see box (unchecked) titled “Category is
complete”. Click on the Category symbol again and click on the “Category is complete” box.
Page - 21
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
You should now see the Category symbol change.
All that remains is to get put the ‘d’ or ‘o’ into the circle. On the standard (main) tool bar click on Insert
and choose Text Box from the drop-down menu.
Click on or near the Category symbol and enter a small ‘d’ for disjoint (or an ‘o’ in the case of overlapping
child tables and move the text box carefully to center the letter in the Category symbol.
Page - 22
Ronald Williams / CPCC / DBA110
Entity Relationship Diagrams (ERDs) drawn with Microsoft Office Visio 7
The diagram is complete.
One additional thing that we can do to dress up the diagram is to show the discriminator values. Again
we can insert text boxes to do so.
In conclusion, Visio is an extremely powerful, business drawing tool. We have touched upon only a bare
minimum of its capabilities. As Visio have become more and more integrated into the Microsoft Office
suite of software, it is now compatible with the other suite software such as Word.
Page - 23
Ronald Williams / CPCC / DBA110
Download