Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Relationships

Similar presentations


Presentation on theme: "Database Relationships"— Presentation transcript:

1 Database Relationships

2 Relationships One of the great benefits of working with databases is the ability to store huge quantities of information. One company’s database may contain many tables of related information. When the information between tables is linked, it is called a relationship. Since the database information is associated, the database can pull all the information together whenever you want in ways that you specify. Examples of separate types of data (paragraph 1) In the recycling club database, member contact information is stored separately from lists of recycling volunteers or holiday planning data. Example of associated data (paragraph 2) Recycling club member Nancy Davolio’s name is associated with, or “knows about,” Nancy Davolio’s contact information, which is stored in another set of data. Creating a relational database means you can quickly pull information together. For example, you can quickly print a list of who's volunteered to recycle newspapers this Saturday, along with their up-to-date addresses and phone numbers. Relationships link data from individual tables to increase the usefulness of the database. 5.01 Understand database tables used in business 5.01 Database Relationships

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

4 Relationship Example What are the advantages of this system?
Are there disadvantages? A music store database contains three tables. An Inventory table is linked by artist to a Royalty table which contains the artist’s contact information and royalty percentages The Inventory table is also linked to a Sales table by salesperson ID numbers, which contains employee information and commission rates Once a purchase is made, the inventory is adjusted in one table and the artist’s royalties and employee’s commissions are calculated from the information in the other tables 5.01 Database Relationships

5 Keys to the Relationship
A 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 5.01 Database Relationships

6 Primary 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 Foreign key – when tables are linked by the primary key, the related field in the second table is known as the foreign key A detailed example of how primary keys and foreign keys work Let’s say you have an Employees table and an Orders table. The Employee ID number is the primary key for the Employees table and a foreign key for the Orders table. The Orders table has its own primary key, the Order ID number. When Nancy Davolio takes an order, her Employee ID number is entered into the Orders table. This ID number refers to the details about Nancy in the Employees table, so there's no need to repeat data about Nancy (such as her phone extension) in the Orders table. We’ll go into more detail about primary keys, foreign keys, and table relationships later in the presentation. 5.01 Database Relationships

7 Referential Integrity
Referential integrity protects related data that is stored in multiple tables. It would prevent a customer in a customers table from being deleted if the customer’s ID also appears in the order table

8 Example Referential integrity would prevent two cars from being assigned to the same driver or two cars from being assigned the same ID number. It would also prevent a driver from being deleted from a table if the driver is assigned to a car.

9 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 5.01 Database Relationships

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

11 One-to-One Only one matching record between two tables
This relationship is the least common because it is not a very efficient use of tables 5.01 Database Relationships

12 One-to-Many Most common type of relationship
One record in Table A links to multiple records in Table B For example: A list of suppliers for the music store is contained in Table A. It is linked by the supplier ID field to Table B which contains all of the products used by the music store. When Table B is searched for a specific product (record), such as a CD carrying case, the manager can view the supplier’s contact information, which is stored in Table A. 5.01 Database Relationships

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

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


Download ppt "Database Relationships"

Similar presentations


Ads by Google