23
More Diagramming & Practice with Relationship Modeling

More Diagramming & Practice with Relationship Modeling

Embed Size (px)

Citation preview

Page 1: More Diagramming & Practice with Relationship Modeling

More Diagramming & Practice with Relationship Modeling

Page 2: More Diagramming & Practice with Relationship Modeling

ER Notation There is no standard for representing data objects in ER diagrams. Each modeling

methodology uses its own notation. The original notation used by Chen is widely used in academics texts and journals but rarely seen in either CASE tools or publications by non-academics. Today, there are a number of notations used, among the more common are Bachman, crow's foot, and IDEFIX.

All notational styles represent entities as rectangular boxes and relationships as lines connecting boxes. Each style uses a special set of symbols to represent the cardinality of a connection. The symbols used for the basic ER constructs are: Entities are represented by labeled rectangles. The label is the name of the entity.

Entity names should be singular nouns. Relationships are represented by a solid line connecting two entities. The name of

the relationship is written above the line. Relationship names should be verbs. Attributes, when included, are listed inside the entity rectangle. Attributes which are

identifiers are underlined. Attribute names should be singular nouns. Cardinality of many is represented by a line ending in a crow's foot. If the crow's

foot is omitted, the cardinality is one. Existence is represented by placing a circle or a perpendicular bar on the line.

Mandatory existence is shown by the bar (looks like a 1) next to the entity for an instance is required. Optional existence is shown by placing a circle next to the entity that is optional.

Page 3: More Diagramming & Practice with Relationship Modeling

Steps In Building the Data Model While ER model lists and defines the constructs required to build a data

model, there is no standard process for doing so. Typically, the entities and relationships are modeled first, followed by key attributes, and then the model is finished by adding non-key attributes. The sequence used for this course are:

Identification of data objects and relationships Drafting the initial ER diagram with entities and relationships Refining the ER diagram Add key attributes to the diagram Adding non-key attributes Validating the model through normalization Adding integrity rules to the Model

Page 4: More Diagramming & Practice with Relationship Modeling

Single Entity Data Model

CUSTOMER

Name

Address

City

State

Zip Code

Telephone

*Account Number

Page 5: More Diagramming & Practice with Relationship Modeling

One-to-Many Relationship 1:m

CUSTOMER

Name

Address

City

State

Zip Code

Telephone

*Account Number

ORDER

*OrderNumberOrderDatePromiseDate

Page 6: More Diagramming & Practice with Relationship Modeling

Many-to-Many Relationship (m:m)

PRODUCT

*ProductNo

Description

UnitPrice

Room

Finish

QtyOnHand

CUSTOMER

Name

Address

City

State

Zip Code

Telephone

*Account Number

?

ORDER

*OrderNumberOrderDatePromiseDate

Page 7: More Diagramming & Practice with Relationship Modeling

Many-to-Many Relationship (m:m)To depict the m:m relationship, a third

entity is created forming two 1:m relationships

This entity is known as an intersection entity or the junction table

The vertical bar added to the “chicken foot” indicates the unique identifier is concatenated from ORDER and PRODUCT

Page 8: More Diagramming & Practice with Relationship Modeling

Many-to-Many Relationship (m:m) An order has many requested items, but each requested item refers to only one

order A product can be requested many times, but each requested item refers to only

one productCUSTOMER

Name

Address

City

State

Zip Code

Telephone

*Account Number

PRODUCT

*ProductNo

Description

UnitPrice

Room

Finish

QtyOnHand

ORDER

*OrderNumberOrderDatePromiseDate

REQUEST

QtyOrdered

LinePrice

Page 9: More Diagramming & Practice with Relationship Modeling

Many-to-Many Relationship (m:m)

Page 10: More Diagramming & Practice with Relationship Modeling

Many-to-Many Relationship (m:m)

Foreign key

Foreign keysTogether they make up the primary key for this table

Remember the primary keys and foreign keys must be the same data type and length

Page 11: More Diagramming & Practice with Relationship Modeling

The final part of the ERDiagramThe final part of the ERDiagram is the

series of notations at the end of the lines. It is their appearance that gives rise to the name "Crow's Foot," which is what this kind of ERD is called. These lines indicate the nature of the relationship, which is called cardinality.

Page 12: More Diagramming & Practice with Relationship Modeling

Cardinality Notation

Page 13: More Diagramming & Practice with Relationship Modeling

Applying these interpretations

In the relationship between Employee and Department, one or more employees belong to one department. In other words, each employee belongs to one and only one department, but each department has at least one and probably several employees in it. This is called a one-to-many relationship.

In the relationship between Employee and Class we see that zero or more employees can take zero or more classes. In other words, a class could be taken by no one or by several employees. Similarly an employee could take zero classes or several classes. This is called a many-to-many relationship.

Page 14: More Diagramming & Practice with Relationship Modeling

Applying these interpretations

The relationships between Employee and Salaried Employee and between Employee and Hourly Employee are both one-to-one relationships. Each person in the Salaried Employee entity will have a corresponding record in the Employee entity. Also, each person in the Hourly Employee entity will have a corresponding record in the Employee entity. Since an employee will not be both hourly and salary, it is obvious that the Salaried Employee and Hourly Employee entities will each have fewer records than the Employee entity. For instance, there might be 1,000 employees, 700 of whom are hourly and 300 of whom are salaried. So when you see entities in a one-to-one relationship, it doesn't necessarily mean that they have the same number of records. It simply means that each record in the one entity has no more than one corresponding record in the other entity.

Page 15: More Diagramming & Practice with Relationship Modeling

Try this:Scenario:

Our database has authors who have an authorid, firstname, and lastname. These authors have many books, and the books have many authors. The books have a bookid, and a booktitle.

Create the data model and show the relationships between the tables.

Page 16: More Diagramming & Practice with Relationship Modeling

Answer:

AUTHORS*AuthorIDFNameLName

BOOKTITLES*AuthorID*BookID

BOOKS*BookIDBookTitle

Page 17: More Diagramming & Practice with Relationship Modeling

Try this:Scenario: Languages spoken, an important (and likely a

multivalued) attribute for a job candidate, deserves elevation to entity status in this database. The original creator of this customer's database had realized this and created a separate table for languages. The problem then became that, if each candidate can speak many languages and each language can be spoken by many candidates, there is a many-to-many relationship between the two tables.

In the candidates entity we have a candidateid, salutation, fname, lname, address, and city.

In the languages entity we have a language Create the data model and show the relationships

between the tables.

Page 18: More Diagramming & Practice with Relationship Modeling

Answer:

Page 19: More Diagramming & Practice with Relationship Modeling

Try This:This database must track the members’

orders for merchandise. The info for each order must include the member who placed the order, amount paid, form of payment, order date, status of order, shipped date, products sold, and the quantity of each product.

Page 20: More Diagramming & Practice with Relationship Modeling

Try This:This database must store information about

the staff who work for the organization. The information must include the first names, last names, and middle initials, as well as the staff members’ addresses, phone numbers, and start dates. The information must also include their positions within the organization as well as who each one’s supervisor is.

Page 21: More Diagramming & Practice with Relationship Modeling

Creating the relationships in Visio or MySQL WorkbenchIn Visio or MySQL Workbench, it is only a

drawing to depict how your relationship will look once you get it into mySQL. It does not have an physical impact on the data itself.

Note: some of the newer programs allow you to directly import your data from their drawing program.

Page 22: More Diagramming & Practice with Relationship Modeling

Steps In Building the Data Model While ER model lists and defines the constructs required to build a data

model, there is no standard process for doing so. Typically, the entities and relationships are modeled first, followed by key attributes, and then the model is finished by adding non-key attributes. The sequence used for this course are:

Identification of data objects and relationships Drafting the initial ER diagram with entities and relationships Refining the ER diagram Add key attributes to the diagram Adding non-key attributes Validating the model through normalization Adding integrity rules to the Model

Page 23: More Diagramming & Practice with Relationship Modeling

Questions?