database relationships

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

Upload: lindy23

Post on 21-May-2015

785 views

Category:

Entertainment & Humor


0 download

TRANSCRIPT

Page 1: Database Relationships

Database RelationshipsDatabase Relationships

Objective 5.01 Understand database tables used in business

Page 2: Database Relationships

Slide Slide 225.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 to increase the usefulness of the database.

One of the great benefits of working One of the great benefits of working with databases is the ability to store with databases is the ability to store huge quantities of information. One huge quantities of information. One company’s database may contain company’s database may contain many tables of related information.many 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 3: Database Relationships

RelationshipsRelationships

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 4: Database Relationships

Relationship ExampleRelationship Example

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?

Page 5: Database Relationships

Keys to the RelationshipKeys to the Relationship

A primary keyA primary key unlocks the relationship potential of a table by creating a unique link between tables

The related field between the two tables must be of the same data type and size

A join line represents the relationship between tables graphically

Page 6: Database Relationships

Primary and Foreign KeysPrimary and Foreign Keys

When tables relate, the primary key of one table becomes a foreign key of the other table

For example, in the two tables on the right, • Employee ID appears in the Employees Table as a

primary key…

• …and in the Orders Table as a foreign key

Page 7: 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 a It would prevent a customer in a customers table from being deleted if the customers table from being deleted if the customer’s ID also appears in the order customer’s ID also appears in the order tabletable

Page 8: 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 9: Database Relationships

Setting up a RelationshipSetting up a Relationship

Add one table’s primary key to a like Add one table’s primary key to a like field with the same properties in field with the same properties in another tableanother tableIn order to decide which table’s primary In order to decide which table’s primary key to use you must first determine the key to use you must first determine the nature (type) of the relationship nature (type) of the relationship There are 3 types of relationships There are 3 types of relationships

Page 10: Database Relationships

One-to-OneOne-to-One

Only one matching record between two Only one matching record between two tablestables

This relationship is the least common This relationship is the least common because it is not a very efficient use of because it is not a very efficient use of tablestables

Page 11: Database Relationships

One-to-ManyOne-to-Many

Most common type of relationshipMost common type of relationshipOne record in Table A links to multiple One record in Table A links to multiple records in Table Brecords in Table B For example: A list of suppliers for the music store is For example: A list of suppliers for the music store is

contained in Table A. It is linked by the supplier ID contained in Table A. It is linked by the supplier ID field to Table B which contains all of the products field to Table B which contains all of the products used by the music store.used by the music store.

When Table B is searched for a specific product When Table B is searched for a specific product (record), such as a CD carrying case, the manager (record), such as a CD carrying case, the manager can view the supplier’s contact information, which is can view the supplier’s contact information, which is stored in Table A.stored in Table A.

Page 12: Database Relationships

Many-to-ManyMany-to-Many

Multiple records in Table A are linked to Multiple records in Table A are linked to multiple records in Table Bmultiple records in Table B

For example: In the music store database, Table A For example: In the music store database, Table A contains customer information and Table B contains CD contains customer information and Table B contains CD inventory. A record for Bob from Table A may be linked to inventory. A record for Bob from Table A may be linked to several records of CDs in Table B by linking the Customer several records of CDs in Table B by linking the Customer ID fields. If Table B contains the CD inventory, a record ID fields. If Table B contains the CD inventory, a record for a particular CD can be linked to several customers in for a particular CD can be linked to several customers in Table A.Table A.

Page 13: Database Relationships

Function of the Junction TableFunction of the Junction Table

A junction table is used with many to many A junction table is used with many to many relationships to join primary key fields of relationships to join primary key fields of multiple tablesmultiple tablesFor example, in a Music Store database, For example, in a Music Store database, the primary key fields of the Orders, the primary key fields of the Orders, Musicians, Inventory, and Prices tables Musicians, Inventory, and Prices tables are all contained in a separate table, which are all contained in a separate table, which acts like a hub for the tables of the acts like a hub for the tables of the databasedatabase

Page 14: Database Relationships