Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Outline  What is a Primary Key?  AutoNumber primary keys  Single-field primary keys  Composite-field primary key  About Foreign Keys  Database.

Similar presentations


Presentation on theme: "1 Outline  What is a Primary Key?  AutoNumber primary keys  Single-field primary keys  Composite-field primary key  About Foreign Keys  Database."— Presentation transcript:

1 1 Outline  What is a Primary Key?  AutoNumber primary keys  Single-field primary keys  Composite-field primary key  About Foreign Keys  Database Relationships A one-to-many relationship A one-to-many relationship A one-to-one relationship A one-to-one relationship A many-to-many relationship A many-to-many relationship  About defining relationships Ms D. Anderson

2 2 What is a Primary Key?   The power of a relational database system such as Microsoft Access comes from its ability to quickly find and bring together information stored in separate tables using queries, forms, and reports. In order to do this, each table should include a field or set of fields that uniquely identifies each record stored in the table. This information is called the primary key of the table. Once you designate a primary key for a table, Access will prevent any duplicate or Null (empty) values from being entered in the primary key fields. Ms D. Anderson

3 3 AutoNumber primary keys   An AutoNumber field can be set to automatically enter a sequential number as each record is added to the table. Designating such a field as the primary key for a table is the simplest way to create a primary key. If you don't set a primary key before saving a newly created table, Microsoft Access will ask if you want it to create a primary key for you. If you answer Yes, Microsoft Access will create an AutoNumber primary key. Ms D. Anderson

4 4 Single-field primary keys   If you have a field that contains unique values such as ID numbers or part numbers, you can designate that field as the primary key. You can specify a primary key for a field that already contains data as long as that field does not contain duplicate values or Null values. Ms D. Anderson

5 5 Composite-field primary key  In situations where you can't guarantee the uniqueness of any single field, you may be able to designate two or more fields as the primary key. Composite Primary Key Ms D. Anderson

6 6 About Foreign Keys  A foreign key is a field in a relational table that matches the primary key column of another table. The foreign key can be used to cross-reference tables. The referenced table is called the parent table while the table with the foreign key is called the child table.  A foreign key is a field in a relational table that matches the primary key column of another table. The foreign key can be used to cross-reference tables. The referenced table is called the parent table while the table with the foreign key is called the child table. Primary Key – PK Primary Key – PK Foreign Key – FK Foreign Key – FK Ms D. Anderson

7 7 Database Relationships Ms D. Anderson

8 8 A one-to-many relationship   A one-to-many relationship is the most common type of relationship. In a one-to- many relationship, a record in Table A can have many matching records in Table B, but a record in Table B has only one matching record in Table A.   For example one supplier can supply more than one product to a company, but each product has only one supplier. Ms D. Anderson

9 9 The Supplier - Product Relationship PK FK Parent Table Child Table 1 ∞ Ms D. Anderson

10 10 A one-to-one relationship   In a one-to-one relationship, each record in Table A can have only one matching record in Table B, and each record in Table B can have only one matching record in Table A. This type of relationship is not common, because most information related in this way would be in one table. Ms D. Anderson

11 11 Student Names - Details relationship PK 11 Ms D. Anderson

12 12 A many-to-many relationship   In a many-to-many relationship, a record in Table A can have many matching records in Table B, and a record in Table B can have many matching records in Table A. This type of relationship is only possible by defining a third table (called a junction or join table) whose primary key consists of two fields — the foreign keys from both Tables A and B. A many-to-many relationship is really two one-to- many relationships with a third table. Ms D. Anderson

13 13 For example, the Orders table and the Products table have a many-to-many relationship that's defined by creating two one- to-many relationships to the Order Details table. One order can have many products, and each product can appear on many orders. Ms D. Anderson

14 14 Orders – Order Details – Products Relationship PK 1 1 ∞ ∞ Composite Primary Key FK Ms D. Anderson

15 15 About defining relationships   The kind of relationship that Microsoft Access creates depends on how the related fields are defined: A one-to-many relationship is created if only one of the related fields is a primary key or has a unique index. A one-to-many relationship is created if only one of the related fields is a primary key or has a unique index. A one-to-one relationship is created if both of the related fields are primary keys or have unique indexes. A one-to-one relationship is created if both of the related fields are primary keys or have unique indexes. A many-to-many relationship is really two one-to- many relationships with a third table whose primary key consists of two fields — the foreign keys from the two other tables. A many-to-many relationship is really two one-to- many relationships with a third table whose primary key consists of two fields — the foreign keys from the two other tables. Ms D. Anderson

16 16 Outline  What is a Primary Key?  AutoNumber primary keys  Single-field primary keys  Composite-field primary key  About Foreign Keys  Database Relationships A one-to-many relationship A one-to-many relationship A one-to-one relationship A one-to-one relationship A many-to-many relationship A many-to-many relationship  About defining relationships Ms D. Anderson


Download ppt "1 Outline  What is a Primary Key?  AutoNumber primary keys  Single-field primary keys  Composite-field primary key  About Foreign Keys  Database."

Similar presentations


Ads by Google