Top Banner
Data Modeling
43

Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Mar 31, 2015

Download

Documents

Alberto Hellyer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Data Modeling

Page 2: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

What are you keeping track of?

• You begin to develop a database by deciding what you are going to keep track of. Each “thing” that you are want to keep track becomes an entity in your database.

• Example – A book dealer might want to keep track of books, authors, and publishers.

Page 3: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

What do you want to know about each entity.

• Decide what to know about each entity.• Each piece of information becomes an

attribute of the entity.• Example – for an author the book dealer

might want to keep track of the name (first, middle, and last), date of birth, and date of death.

Page 4: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Recap

• Entity - person, place, thing or event on which we maintain information.

• Attribute - A single piece of information describing a particular entity.

Page 5: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER – Diagram (1)

• It is often useful to use a diagram to visually represent a data model.

• A common diagramming tool is the Entity-Relationship (ER) Diagram.

• In an ER Diagram an entity is represented as a rectangle.

• The attributes associated with the entity can be listed by the rectangle.

Page 6: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (2)

Author Book

Publisher

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEdition

Page 7: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Book Dealer Entities

• The book dealer data model has three entities– Author– Book– Publisher

• Note that the name of each entity is – a noun– singular

Page 8: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Attributes

• Each entity has one or more attributes associated with it.

• If an attributed is underlined it is part of the primary key for that entity.

• Note that each entity has a primary key defined. Since a primary key cannot be null (blank) each entity exhibits entity integrity.

Page 9: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Implementing you data model in Access

• The Access DBMS uses the Relational Data Model

• In a Relational Data Model – each entity is represented as a table– each attribute is represented as a field in a table– every table has a primary key defined to ensure

entity integrity

Page 10: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Instances of an Entities

• Once you’ve created your tables you enter data into the rows.

• Each table represents an entity and each filled in row is an instance of that entity.

• Each entity has a primary key and primary key value must be unique so each row represent a unique occurrence (instance) of an entity.

• Note – don’t enter data into your table yet.

Page 11: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Matching Rows

• Now we have a data model with three entities (tables).

• But the entities are independent of each other.

• How do we know what row(s) in one entity match up with row(s) in the other entities.

• We need to add some relating fields.

Page 12: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Relating Fields & Relationships

• The relationship(s) between the entities must be defined to determine the relating field(s).

• In our model we have • a relationship between the author and book

entity entities, and • a relationship between the book and the

publisher entities.

Page 13: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Publisher-Book relationship

• The relationships between publisher and book are– A publisher publishes a book– A book is published by a publisher

• Relationships are usually verbs• Relationships are symmetric, you should be

able to define them in both directions.

Page 14: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

The order of the relationship

• The order of the relationship between two entities can be one of the following

• one-to-one 1-1• one-to-many 1-n• many-to-many n-n

Page 15: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Diagram the relationships

• Start by adding the relationship to your ER-Diagram

• Relationships are represent by diamonds

Page 16: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Our Relationships

• In our example we have two relationships• Write

– authors write books– books are written by authors

• Publish– publishers publish books– books are published by publishers

Page 17: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (3)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEdition

Page 18: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Determine the order (1)

• Look at the relationship from each direction• For example

– A (1) book can be published by one (1) publisher– A (1) publisher can publish many (n) books

• Put the values on your ER-Diagram

Page 19: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (4)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEdition

1

is published by

1

publishes

1

n

Page 20: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Determine the order (2)

• On each side of the relationship take the bigger value.

Page 21: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (5)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEdition

1

is published by

1

publishes

1

nn

1

Page 22: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Determine the order (3)

• You have a one-to-many (1-n) relationship between publisher and book.

• Now that you know the order of the relationship you need to represent the relationship in your relational data model (your Access tables)

Page 23: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (6)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEdition

n

1

Page 24: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Representing a 1-n Relationship

• To represent a 1-n relationship in a relational data model you

• Take the primary key from the one side of the relationship and make it the foreign key in the many side of the relationship.

• In our example the primary key of Publisher (Name) becomes a foreign key in Book.

Page 25: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (7)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEditionName (foreign key)

n

1

Page 26: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Representing a 1-1 Relationship

• But what if the relationship had been 1-1?• You follow the same principle, take the

primary key from one side of the relationship and make it the foreign key in the other side of the relationship.

• It doesn’t matter which side you take the primary key from

• You only go in one direction• In our example we do not have a 1-1 relationship.

Page 27: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Author – Book Relationship

• Determine the order of the relationship between Author and Book

• Look at the relationship from each direction– An (1) author can write many (n) books– A (1) book can be written by (n) autors

• Put the values on your ER-Diagram

Page 28: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (8)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEditionName (foreign key)

n

1

1 writes n

1written byn

Page 29: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Determine the order (4)

• On each side of the relationship take the bigger value.

Page 30: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (9)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEditionName (foreign key)

n

1

1 writes n

1written byn

n n

Page 31: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Determine the order (5)

• You have a many-to-many (n-n) relationship between author and book.

• Now that you know the order of the relationship you need to represent the relationship in your relational data model (your Access tables)

Page 32: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (10)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEditionName (foreign key)

n

1

n n

Page 33: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Representing a n-n Relationship

• To represent a n-n relationship in a relational data model you need to create a table between the two entities to represent the relationship. To do this you– Take the primary key from one entity and make it a

foreign key in the new table.– Then take the primary key from the other entity and

make it a second foreign key in the new table.

Page 34: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Representing a n-n Relationship

• In our example we create a new table named Write.

• The primary key (ID) from Author becomes a foreign key in Write.

• The primary key (Title) from Book becomes a foreign key in Write.

Page 35: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (11)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEditionName (foreign key)

n

1

n n

ID (foreign key from Author)Title (foreign key from Book)

Page 36: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Comments of the Associative Entity(the n-n relationship)

• Notice that the relationship write now looks like an entity, and is shown with a dash outline. It is still technically a relationship.

• The relationship entity has two foreign keys but no primary key. We would like every table to have a primary key.

• One solution would be to add an “assigned primary key” to the new table.

Page 37: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (12)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

TitleDateEditionName (foreign key)

n

1

n n

IDAID (foreign key from Author)Title (foreign key from Book)

Page 38: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

A possible concern

• To create an instance of an author writing a book we would enter an ID and Title value on a line in the write table.

• So if one book had three authors then we would add three rows to the write table, each row would have a different Author ID but they would have the same Title.

• But book titles can be very long and it would be a waste of space to repeat a long title several times in the database.

Page 39: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Assigned Primary Keys

• To address this concern we will add an “assigned primary key” to the Book entity and then let the Title become a non-key attribute.

• The Book ID is then the primary key that becomes the foreign key in the write relationship.

Page 40: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ER-Diagram (13)

Author Bookwrite

Publisher

publish

IDLast_NameFirst_NameMiddle_NameDOBDOD

NameAddressPhone

IDTitleDateEditionName (foreign key)n

1

n n

IDAID (foreign key from Author)BID (foreign key from Book)

Page 41: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

A picky technical point

• When we first create the write table it only had foreign key attributes and was technically just a relationship.

• Once we added a non-foreign key attribute (in this case, the assigned primary key) it became an “associative entity.”

Page 42: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

ID attributes everywhere

• Note that our ER-Diagram now has three different ID attributes and they each have different meanings.– Author.ID identifies an instance of the Author

entity– Book.ID identifies an instance of the Book

entity– Write.ID identifies and instance of the write

associative entity.

Page 43: Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.

Attribute names must be unique

• Attribute names must be unique within a table but different tables can use the same attribute names for different things.

• So in the write table we have three ID attributes but each has a different name– ID is the primary key for the write associative entity– AID is the foreign key the write associative entity that

matches the ID attribute in the Author entity.– BID is the foreign key the write associative entity that

matches the ID attribute in the Book entity.