31
Chapter 6 Chapter 6 The Relational Database The Relational Database Model: Additional Concepts Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis Presentation by: Amita Goyal Chin, Ph.D. Virginia Commonwealth University John Wiley & Sons, Inc.

Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

Embed Size (px)

Citation preview

Page 1: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

Chapter 6Chapter 6The Relational Database Model: The Relational Database Model:

Additional Concepts Additional Concepts

Fundamentals of Database Management Systemsby

Mark L. Gillenson, Ph.D.

University of Memphis

Presentation by: Amita Goyal Chin, Ph.D.

Virginia Commonwealth University

John Wiley & Sons, Inc.

Page 2: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-22

Chapter ObjectivesChapter Objectives

Describe how unary and ternary Describe how unary and ternary relationships are implemented in a relationships are implemented in a relational database. relational database.

Explain the concept of referential integrity. Explain the concept of referential integrity.

Describe how the referential integrity Describe how the referential integrity restrict, cascade, and set-to-null delete restrict, cascade, and set-to-null delete rules operate in a relational database. rules operate in a relational database.

Page 3: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-33

General Hardware Co. General Hardware Co. including OFFICEincluding OFFICE

Page 4: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-44

General Hardware Co. General Hardware Co. including OFFICEincluding OFFICE

Page 5: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-55

Unary One-to-Many Unary One-to-Many RelationshipsRelationships

A salesperson reports to A salesperson reports to exactly one sales exactly one sales manager, but each manager, but each salesperson who does salesperson who does serve as a sales manager serve as a sales manager typically has several typically has several salespersons reporting to salespersons reporting to him.him.

There is a one-to-many There is a one-to-many relationship within relationship within salespersons. salespersons.

Salesperson (also a sales manager) Salesperson

Page 6: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-66

Unary One-to-Many Unary One-to-Many RelationshipsRelationships

A unary relationship because there is only one A unary relationship because there is only one entity type involved.entity type involved.

A one-to-many because among the individual A one-to-many because among the individual entity occurrences, that is, among the entity occurrences, that is, among the salespersons, a particular salesperson reports to salespersons, a particular salesperson reports to one salesperson who is his sales manager, one salesperson who is his sales manager, while a salesperson who is a sales manager while a salesperson who is a sales manager may have several salespersons reporting to her.may have several salespersons reporting to her.

Page 7: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-77

General Hardware Co. General Hardware Co. Salesperson Reporting Salesperson Reporting

HierarchyHierarchy

Page 8: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-88

One-to-Many Unary One-to-Many Unary RelationshipRelationship

Requires the Requires the addition of one addition of one column to the column to the relation relation representing representing the single the single entity involved entity involved in the unary in the unary relationship.relationship.

Page 9: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-99

Unary Many-to-Many Unary Many-to-Many RelationshipsRelationships

A special case, an example of which has A special case, an example of which has come to be known as the bill of materials come to be known as the bill of materials problem.problem.

Every entity occurrence can be related to Every entity occurrence can be related to many other occurrences.many other occurrences.

Product Product

Page 10: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1010

General Hardware Company’s General Hardware Company’s Product SetProduct Set

Wrench Model A (#11)Deluxe Wrench Set (#43)

Wrench Model B (#14)Supreme Tool

Wrench Model C (#17) Set (#53)Master Wrench Set (#44)

Wrench Model D (#19)

Hammer Model A (#22)Grand Tool

Hammer Model B (#24) Deluxe Hammer Set (#48) Set (#56)

Hammer Model C (#28)

Drill Model A (#31)

Drill Model B (#35)

Figure 6.5 General Hardware Co. product bill of materials.

Tools and sets of tools are sold.Tools and sets of tools are sold.

Many-to-many nature of products.Many-to-many nature of products.

Page 11: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1111

Modified Product RelationModified Product Relation

Product Numbers Product Numbers have been reduced to have been reduced to 2 digits for simplicity.2 digits for simplicity.

Every individual unit Every individual unit item and every set of item and every set of tools has its own row tools has its own row in the relation in the relation because every item because every item and set is available and set is available for sale.for sale.

Page 12: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1212

Unary Many-to-Many Unary Many-to-Many Relationship: New RelationRelationship: New Relation

Just as a binary many-to-many Just as a binary many-to-many relationship requires the creation relationship requires the creation of an additional relation in a of an additional relation in a relational database, so does a relational database, so does a unary many-to-many relationship.unary many-to-many relationship.

The domain of values of each The domain of values of each column is that of the Product column is that of the Product Number column of the PRODUCT Number column of the PRODUCT relation.relation.

Page 13: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1313

Ternary RelationshipsTernary Relationships

Involves three Involves three different entity types.different entity types.

Page 14: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1414

General Hardware Co.: General Hardware Co.: Ternary RelationshipTernary Relationship

Page 15: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1515

Ternary RelationshipTernary Relationship

These new General Hardware Co. These new General Hardware Co. relations are all independent with no relations are all independent with no foreign keys in any of them.foreign keys in any of them.

The SALES relation shows how this The SALES relation shows how this ternary relationship is represented in a ternary relationship is represented in a relational database. relational database.

Page 16: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1616

Ternary RelationshipTernary Relationship

The primary key of the additional relation The primary key of the additional relation (SALES) will be (at least) the combination (SALES) will be (at least) the combination of the primary keys of the entities involved of the primary keys of the entities involved in the relationship.in the relationship.

Page 17: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1717

Ternary RelationshipTernary Relationship

Did salesperson 137 sell product 19440 to customer 0839?

Page 18: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1818

Database OperationsDatabase Operations

In addition to retrieving data we must be In addition to retrieving data we must be prepared to perform data maintenance prepared to perform data maintenance operations, including:operations, including: inserting new recordsinserting new records deleting existing recordsdeleting existing records updating existing recordsupdating existing records

Page 19: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-1919

Referential IntegrityReferential Integrity

Revolves around the circumstance of Revolves around the circumstance of trying to refer to data in one relation in the trying to refer to data in one relation in the database, based on values in another database, based on values in another relation.relation.

Page 20: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2020

Referential Integrity - Record Referential Integrity - Record DeletionDeletion

A problem A problem arises, e.g., arises, e.g., because a because a deleted deleted record, a record, a salesperson salesperson record, is on record, is on the “one side” the “one side” of a one-to-of a one-to-many many relationship.relationship.

Page 21: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2121

Referential Integrity - Referential Integrity - InsertionInsertion

Insertion - if a new record is inserted into the Insertion - if a new record is inserted into the “one side” (SALESPERSON relation) of the one-“one side” (SALESPERSON relation) of the one-to-many relationship, there is no problem.to-many relationship, there is no problem.

If a new customer record is inserted into the If a new customer record is inserted into the “many side” (CUSTOMER relation) of the one-“many side” (CUSTOMER relation) of the one-to-many relationship and it happens to include a to-many relationship and it happens to include a salesperson number that does not have a match salesperson number that does not have a match in the SALESPERSON relation—that would in the SALESPERSON relation—that would cause the same kind of problem as the deletion cause the same kind of problem as the deletion example.example.

Page 22: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2222

Referential Integrity - UpdateReferential Integrity - Update

Updating a foreign key value.Updating a foreign key value.

For example, a salesperson number in the For example, a salesperson number in the CUSTOMER relation with a new CUSTOMER relation with a new salesperson number that has no match in salesperson number that has no match in the SALESPERSON relation.the SALESPERSON relation.

Page 23: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2323

DBMS & Referential IntegrityDBMS & Referential Integrity

Early relational DBMSs did not provide any Early relational DBMSs did not provide any control mechanisms for referential integrity.control mechanisms for referential integrity.

Modern relational DBMSs provide sophisticated Modern relational DBMSs provide sophisticated control mechanisms for referential integrity:control mechanisms for referential integrity: Delete rulesDelete rules Insert rulesInsert rules Update rulesUpdate rules

Page 24: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2424

Three Delete RulesThree Delete Rules

RestrictRestrict

CascadeCascade

Set-to-NullSet-to-Null

Page 25: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2525

Delete Rule: RestrictDelete Rule: Restrict

If an attempt is made to delete a record on If an attempt is made to delete a record on the “one side” of the one-to-many the “one side” of the one-to-many relationship, the system will forbid the relationship, the system will forbid the delete to take place if there are any delete to take place if there are any matching foreign key values in the relation matching foreign key values in the relation on the “many side.”on the “many side.”

Page 26: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2626

Delete Rule: RestrictDelete Rule: Restrict

If an attempt is made to If an attempt is made to delete the record for delete the record for salesperson 361 in the salesperson 361 in the SALESPERSON relation, SALESPERSON relation, the system will not permit the system will not permit the deletion to take place the deletion to take place because the CUSTOMER because the CUSTOMER relation records for relation records for customers 1525 and customers 1525 and 1700 include salesperson 1700 include salesperson number 361 as a foreign number 361 as a foreign key value.key value.

Page 27: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2727

Delete Rule: CascadeDelete Rule: Cascade

If an attempt is made to delete a record on If an attempt is made to delete a record on the “one side” of the relationship, not only the “one side” of the relationship, not only will that record be deleted but all of the will that record be deleted but all of the records on the “many side” of the records on the “many side” of the relationship that have a matching foreign relationship that have a matching foreign key value will also be deleted.key value will also be deleted.

The deletion will cascade from one relation The deletion will cascade from one relation to the other.to the other.

Page 28: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2828

Delete Rule: CascadeDelete Rule: Cascade

If an attempt is made to delete the record for If an attempt is made to delete the record for salesperson 361 in the SALESPERSON relation, that salesperson 361 in the SALESPERSON relation, that salesperson record will be deleted and so too, salesperson record will be deleted and so too, automatically, will the records for customers 1525 and automatically, will the records for customers 1525 and 1700 in the CUSTOMER relation because they have 361 1700 in the CUSTOMER relation because they have 361 as a foreign key value.as a foreign key value.

Page 29: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-2929

Delete Rule: Set-to-NullDelete Rule: Set-to-Null

If an attempt is made to delete a record on If an attempt is made to delete a record on the “one side” of the one-to-many the “one side” of the one-to-many relationship, that record will be deleted relationship, that record will be deleted and the matching foreign key values in the and the matching foreign key values in the records on the “many side” of the records on the “many side” of the relationship will be changed to null.relationship will be changed to null.

Page 30: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-3030

Delete Rule: Set-to-NullDelete Rule: Set-to-Null

If an attempt is made to delete the record for salesperson If an attempt is made to delete the record for salesperson 361 in the SALESPERSON relation, that record will be 361 in the SALESPERSON relation, that record will be deleted, and the Salesperson Number attribute values in deleted, and the Salesperson Number attribute values in the records for customers 1525 and 1700 in the the records for customers 1525 and 1700 in the CUSTOMER relation will have their Salesperson Number CUSTOMER relation will have their Salesperson Number attribute values changed from 361 to null.attribute values changed from 361 to null.

Page 31: Chapter 6 The Relational Database Model: Additional Concepts Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis

6-6-3131

“Copyright 2004 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information contained herein.”