17
ERD Cardinality OOD Inge Powell

Erd cardinality

Embed Size (px)

Citation preview

Page 1: Erd cardinality

ERD Cardinality

OOD Inge Powell

Page 2: Erd cardinality

ERD Cardinality

When we show relationships between tables we need to show the cardinality.

Depending on the data we are storing there are several options to choose from.

If using Visio to create a diagram, it takes its initial value from the database store.

Page 3: Erd cardinality

Erd Cardinality

The cardinality relationship is shown with lines between two entities to indicate a relationship.

This line denotes that there is a relationship between the entities.

Entity A Entity B

Page 4: Erd cardinality

ERD Cardinality

The ENDS of these lines denote the cardinality to that entity.

This is a ‘One to One’ relationship. This means that Entity A will relate to only one

of Entity B. And Entity B will relate to only one of Entity A

Entity A Entity B

Page 5: Erd cardinality

Erd Cardinality

Here is an example..

One Driver drives just one Bus. One Bus has just one Driver. The records might be held like this:

Bus Driver

BusPK BusID Colour NoOfSeatsFK DriverID

BusPK BusID Colour NoOfSeatsFK DriverID

DriverPK DriverID Name PointsFK BusID

DriverPK DriverID Name PointsFK BusID

Page 6: Erd cardinality

ERD Cardinality

The use of ‘Crows Feet’.

This is a ‘One to Many’ relationship. This means that Entity A will relate to many of

Entity B. And Entity B will relate to only one of Entity A.

Entity A Entity B

Page 7: Erd cardinality

Erd Cardinality

Here is an example.

One Customer may have many orders. One Order is for just one Customer. The records might be held like this:

Customer Order

CustomerPK CustID Name Address TelNo

CustomerPK CustID Name Address TelNo

OrderPK OrderNo DateFK CustID

OrderPK OrderNo DateFK CustID

Page 8: Erd cardinality

ERD Cardinality

The use of ‘Crows Feet’.

This is a ‘Many to One’ relationship. This means that Entity A will relate to only one

of Entity B. And Entity B will relate to many of Entity A.

(We still tend to refer to as a one to many, just the other way round from B to A)

Entity A Entity B

Page 9: Erd cardinality

ProductPK ProdID PName PriceFK SuppID

ProductPK ProdID PName PriceFK SuppID

Erd Cardinality

Here is an example.

One Product will have just one Supplier. One Supplier will supply many Products. The records might be held like this:

Product Supplier

SupplierPK SuppID Name Address TelNo

SupplierPK SuppID Name Address TelNo

ProductPK ProdID PName PriceFK SuppID

ProductPK ProdID PName PriceFK SuppID

ProductPK ProdID PName PriceFK SuppID

ProductPK ProdID PName PriceFK SuppID

SupplierPK SuppID Name Address TelNo

SupplierPK SuppID Name Address TelNo

ProductPK ProdID PName PriceFK SuppID

ProductPK ProdID PName PriceFK SuppID

Page 10: Erd cardinality

ERD Cardinality

The use of ‘Crows Feet’.

This is a ‘Many to Many’ relationship. This means that Entity A will relate to many of

Entity B. And Entity B will relate to many of Entity A.

Entity A Entity B

Page 11: Erd cardinality

ERD Cardinality

Many to Many - This can cause problems with data, as we can’t pinpoint the link.

Imagine this, a Lender can borrow many Books. But a Book can be borrowed by more than one Lender.

Entity A Entity B

Lender Book

Page 12: Erd cardinality

BookPK BookID Title AuthorFK LenderIDFK LenderIDFK LenderIDFK LenderID

BookPK BookID Title AuthorFK LenderIDFK LenderIDFK LenderIDFK LenderID

ERD Cardinality

How do we know who has each individual book?

Also when we look at the data, how will we store this?

Lender Book

LenderPK LenderID Name AddressFK BookIDFK BookIDFK BookIDFK BookID

LenderPK LenderID Name AddressFK BookIDFK BookIDFK BookIDFK BookID

Page 13: Erd cardinality

ERD Cardinality

To manage the many to many relationship, the data would need to keep growing.

This still causes problems, Who has the book now?

This is silly!!

We can resolve this By adding a link table.

BookPK BookID Title AuthorFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderID FK LenderID

BookPK BookID Title AuthorFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderIDFK LenderID FK LenderID

Page 14: Erd cardinality

ERD Cardinality

To resolve a many to many relationship.

We resolve it by adding a link table.

This means we now have two manageable one to many relationships instead.

Entity A Entity BEntity C

Entity A Entity B

Page 15: Erd cardinality

ERD Cardinality

The resolved many to many relationship.

One lender can have several LendRecords One LendRecord is for just one Lender

One Book can have several LendRecords One LendRecord is for just one Book

Lender BookLendRecord

Page 16: Erd cardinality

ERD Cardinality

Here is the resolved example.

The data for this system might look something like this:

Lender BookLendRecord

BookPK BookID Title Author

BookPK BookID Title Author

LendRecordPK RecordIDFK BookID FK LenderID

LendRecordPK RecordIDFK BookID FK LenderID

LenderPK LenderID Name Address

LenderPK LenderID Name Address

Page 17: Erd cardinality

ERD Cardinality

The LendRecord record could be deleted when the book is returned.

Or fields added to identify the current loan.

Lender BookLendRecord

BookPK BookID Title Author

BookPK BookID Title Author

LendRecordPK RecordIDFK BookID FK LenderID OnLoanYN Date

LendRecordPK RecordIDFK BookID FK LenderID OnLoanYN Date

LenderPK LenderID Name Address

LenderPK LenderID Name Address