14
Database Relationships Database Relationships Objective 5.01 Understand database tables used in business

Database Relationships

  • Upload
    wmassie

  • View
    3.428

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Database Relationships

Database RelationshipsDatabase Relationships

Objective 5.01 Understand database tables used in business

Page 2: Database Relationships

RelationshipsRelationships

A relationship means that two or more A relationship means that two or more tables are linked together by a common tables are linked together by a common field, called a primary key field.field, called a primary key field.

Table relationships increase the power of Table relationships increase the power of the database by allowing data to be stored the database by allowing data to be stored separately, but managed and retrieved separately, but managed and retrieved collectively.collectively.

Page 3: Database Relationships

Slide Slide 335.01 Understand database tables used in business5.01 Understand database tables used in business

Making Friends with RelationsMaking Friends with Relations

Relationships link data from individual tables and increase the usefulness of a database.

One of the great benefits of One of the great benefits of working with databases is the working with databases is the ability to store huge quantities of ability to store huge quantities of information. One company’s information. One company’s database may contain multiple database may contain multiple tables of related information.tables of related information.

When the information between When the information between tables is linked, it is called a tables is linked, it is called a relationship.relationship.

Page 4: Database Relationships

What does a relationship look like?What does a relationship look like?

One record in the Student Table is related to one record in the Participation Table

Relationship established between two tables

Page 5: Database Relationships

Keys to the RelationshipKeys to the Relationship

A primary keyA primary key is an essential element of a is an essential element of a database table. It is a data type that is set to database table. It is a data type that is set to make each record within a database table make each record within a database table unique and to link tables together.unique and to link tables together.

Primary key fields must be of the same data type and size for the tables to which they link.

The red line that graphically represents the relationship is called a Join Line

Page 6: Database Relationships

What makes a good primary key?What makes a good primary key?

Which of the following would make each Which of the following would make each record in a database unique?record in a database unique? Social security numberSocial security number Last nameLast name First nameFirst name Automobile VIN numberAutomobile VIN number Birth dateBirth date Product serial numberProduct serial number

Page 7: Database Relationships

Primary and Foreign KeysPrimary and Foreign KeysWhen tables relate, the primary key of one table becomes a foreign key of the other table

For example, in the tables below, • ISBN appears as a primary key in the Grocery

Products table . . .…and in the Orders Table as a foreign key

Page 8: Database Relationships

Diagram of Primary & Foreign KeysDiagram of Primary & Foreign Keys

885.02 Understand queries, 5.02 Understand queries,

forms, and reports.forms, and reports.

Page 9: Database Relationships

Function of the Junction TableFunction of the Junction Table

A junction table is used to join primary key A junction table is used to join primary key fields of multiple tablesfields of multiple tablesThe Junction table is like a hub or control The Junction table is like a hub or control centercenter

In this example, Orders is the Junction Table. It allows the Grocery Products and the Customers tables to share information.

Page 10: Database Relationships

One-to-ManyOne-to-Many

One-to-ManyOne-to-Many One record in Table A links to multiple One record in Table A links to multiple

records in Table B through the records in Table B through the primary/foreign key fields of the two tablesprimary/foreign key fields of the two tables

Page 11: Database Relationships

Many-to-ManyMany-to-Many

Many-to-ManyMany-to-Many Multiple records in multiple tables are linked Multiple records in multiple tables are linked

by their primary keys through a Junction by their primary keys through a Junction Table. Table.

A many-to-many relationship is formed by A many-to-many relationship is formed by connecting two or more tables that have a connecting two or more tables that have a one-to-many relationship. The connection is one-to-many relationship. The connection is made by a made by a Junction Table.Junction Table.

Page 12: Database Relationships

Setting up a RelationshipSetting up a Relationship

Add one table’s primary key to a field in Add one table’s primary key to a field in another table that has the same another table that has the same properties properties

The primary key in Grocery Products is ISBN

The primary key in Orders is Order No

The primary key in Customers is Store ID

Page 13: Database Relationships

Referential IntegrityReferential Integrity

Referential integrityReferential integrity protects related protects related data that is stored in multiple tables. data that is stored in multiple tables.

It would prevent a customer in one table It would prevent a customer in one table from being deleted if the customer is also from being deleted if the customer is also in a related table.in a related table.

Page 14: Database Relationships

Relationship DiscussionRelationship Discussion

A music store database contains three tables. A music store database contains three tables. An Inventory table is linked by artist to a Royalty An Inventory table is linked by artist to a Royalty table which contains the artist’s contact table which contains the artist’s contact information and royalty percentagesinformation and royalty percentages

The Inventory table is also linked to a Sales The Inventory table is also linked to a Sales table by salesperson ID numbers, which table by salesperson ID numbers, which contains employee information and commission contains employee information and commission ratesrates

Once a purchase is made, the inventory is Once a purchase is made, the inventory is adjusted in one table and the artist’s royalties adjusted in one table and the artist’s royalties and employee’s commissions are calculated and employee’s commissions are calculated from the information in the other tablesfrom the information in the other tables

What are the advantages of this system?

Are there disadvantages?