1 / 32

Creating and Understanding Relationships

Creating and Understanding Relationships. MS ACCESS. Office Management Tools II Ms Saima Gul. Understanding Keys. When you create your tables, you should assign each table a primary key—one or more fields whose contents are unique to each record.

armen
Télécharger la présentation

Creating and Understanding Relationships

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating andUnderstandingRelationships MS ACCESS Office Management Tools II Ms Saima Gul

  2. Understanding Keys • When you create your tables, you should assign each table a primary key—one or more fields whose contents are unique to each record. • This key is a way to make sure that the table records contain only one unique value. • The capability to assign a single, unique value to each record makes the table “clean” and reliable. This is known as entity integrity in the world of database management. • By having a different primary key value in each record, you can tell two records (in this case, students) apart. This is important because you can easily have two individual students named Atif in your table. • The goal of setting primary keys is to create individual records in a table that will guarantee uniqueness.

  3. Understanding foreign keys • Primary keys guarantee uniqueness in a table, and you use the primary key field in one table to link to related records in another table. • The common link field in the other table (records that are associated with a record in the primary table) may not be (and usually isn’t) the primary key in the other table. • The common link field is a field or fields that hold the same type of data (matching the content of the field exactly) as in the primary key of the link table. This common link field, or combination of fields, is known as a foreign key field. • By matching the same values (from a primary key field in a record in the primary key table to the values in a specific field of one or more records in a foreign key table) in both tables, you can relate records between tables.

  4. Understanding Relations between Tables • Before you learn to create these relationships, it is important to understand them. • There are three types of relationships that you can set between two tables: • One-to-one • One-to-many • Many-to-many.

  5. Understanding the four types of table relationships • When you physically join two tables (by connecting fields with like information), you create a relationship that Access recognizes. • The relationship that you specify between tables is important. It tells Access how to find and display information from fields in two or more tables. The program needs to know whether to look for only one record in a table or look for several records on the basis of the relationship.

  6. Types of relationships • One-to-one (l:l)—An occurrence of [object] 'A' can relate to one and only one occurrence of [object] 'B,' and an occurrence of 'B' can relate to only one occurrence of 'A.' • One-to-many (l:N)—One occurrence of [object] 'A' can relate to one or many occurrences of [object] 'B,' but an occurrence of 'B' can relate to only one occurrence of 'A.’ • Many-to-many (M:N)—An occurrence of [object] 'A' can relate to one or more occurrences of 'B,' while an occurrence of 'B' can relate to one or more occurrences of 'A.’

  7. Relationship types and Examples is_assigned Parking_Place Employee One-to-one issues registers_for Student Course Many-to-many

  8. Understanding Referential Integrity • In addition to specifying relationships between tables in an Access database, you can also set up some rules that will help in maintaining a degree of accuracy, or Referential Integrity, between the tables. • For example, you would not want to delete a student record in your Student table if there are related program records in the Program table. If you did delete a student record without first deleting the student’s program record, you would have a system that had programs with students whose data do not exit. This type of problem could be catastrophic. • So a database system needs to have rules that specify certain conditions between tables — rules to enforce the integrity of information between the tables. These rules are known as referential integrity; they keep the relationships between tables intact in a relational database management system. • Referential integrity prohibits you from changing your data in ways that invalidate the links between tables.

  9. Understanding Referential Integrity (Contd.) • Referential integrity operates strictly on the basis of the tables’ key fields; it checks each time a key field, whether primary or foreign, is added, changed, or deleted. • If a change to a value in a key field creates an invalid relationship, it is said to violate referential integrity. • Tables can be set up so that referential integrity is enforced automatically. • When tables are linked, one table is usually called the parent and the other (the table it is linked to) is usually called the child. This is known as a parent-child relationship between tables. • Referential integrity guarantees that there will never be an orphan, a child record without a parent record.

  10. Creating Relationships • Access has a very powerful Relationships window. With it, you can add tables, use drag-and-drop methods to link tables, easily specify the type of link, and set any referential integrity between tables. • You begin creating relationships in the Database window. From this window, you can select Tools➪Relationships or click the Relationships button on the toolbar. • The main Relationships window appears, which lets you add tables and create links between them. • When first opened, the Relationships window is a blank surface. Tables are added to the window by using one of these methods: • Add the tables before entering the Relationships window from the Show Table dialog box that’s first displayed. • Click the Show Table button on the toolbar. • Click Relationships ➪Show Table from the menu bar. • While in the Relationships window, click the right mouse button (which displays the shortcut menu) and select Show Table from the menu.

  11. Creating Relationships

  12. Creating Relationships (contd.) • To start the Relationships window and add the tables to the Relationships window, follow these steps: • Click the Relationships button on the toolbar. Access opens the Show Table dialog box. • Select all the tables by clicking the first table, holding Shift, and clicking last table name. Then click Add. • Click the Close button on the Show Table dialog box. Notice that Access has placed each table in the Relationships window. Each table is in its own box; the title of the box is the name of the table. Inside the table box are the names of the fields for each table. Currently, there are no relationships, or lines, between the tables. Now you are ready to set relationships between them.

  13. Creating Relationships

  14. Creating relationships between tables • To create a relationship between two tables, select the common field in one table, drag it to the field in the table you want to relate it to, and drop it on the common field. • Follow these steps to create a relationship between the Program and Student tables: • Click the progId field of the Program table. • While holding down the mouse button, move the cursor to the Student table. Notice that Access displays a field-select icon (a small rectangular box with lines inside of it) as you hold and drag the progId field from the Program table. • Drag and drop the field-select icon to the pId field of the Student table. Access displays the Edit Relationships dialog box. • Click the Create button to create the relationship. Access closes the dialog box and places a join line between the Program and Student tables.

  15. Creating relationships between tables

  16. Creating relationships between tables After you click Create button in Edit Relationships dialog box

  17. Specifying relationship options in the Edit Relationships dialog box • The Edit Relationships dialog box has several options for the relationship between the Program and Student tables. • The Edit Relationships dialog box tells you which table is the primary table for the relationship and whether referential integrity is enforced (a check box below the names of the related fields on the left side). • The dialog box also tells you the type of relationship (one-to-one or one-to-many—at the bottom of the dialog box) and lets you specify (after selecting Enforce Referential Integrity) whether cascading updates and deletes (automatically fix key changes or deletions in related records) between related tables are allowed.

  18. Checking the primary table • The top of the dialog box has two table names—Program on the left and Student on the right. • The Program table is considered the primary table for this relationship. • The dialog box shows the related fields for each table in a separate box immediately below the table names. • Make sure that the correct table name is in both boxes and that the correct field is specified (progId and pId).

  19. Checking the Join Type between tables • The right side of the Edit Relations window has four buttons—OK, Cancel, Join Type, and Create New ... buttons. Pressing the OK button returns you to the Relationships window with any changes specified. • The Cancel button will cancel the current changes and also return you to the Relationships window. • The Create New ... button will let you specify a new relation between two tables and fields. • The one button that you should also click after you first activate the Edit Relations dialog box is Join Type. This button will activate another window—the Join Properties dialog box. • This dialog box lets you decide if you want to see records in both tables, see all records in one table or the other, or see only those that are related. • For instance, to make sure that you have the correct join type between the Program and Program tables, follow these steps:

  20. Creating relationships between tables After you click Join Type button in Edit Relationships dialog box

  21. Checking the Join Type between tables (contd.) • Click the Join Type button to activate the Join Properties Dialog box. • Click the option that says “Include ALL records from ‘Program’ and only those records from ‘Student’ where the joined fields are equal.” (the second option on the authors system). • Click the OK button to return to the Edit Relationships Dialog box. • Click the OK button of the Edit Relationships dialog box to return to the Relationships window. • You have now specified that the relationship is from the Program table to the Student table—meaning that many students can be related to a single program. • The Relationships window should now show an arrow going from the Student table to the Program table. At this point, you are ready to set Referential Integrity between the two tables.

  22. Creating relationships between tables

  23. Enforcing referential integrity • After specifying the relationship, verifying the table and related fields, and specifying the type of join between the tables, you can set referential integrity between the tables by clicking the Enforce Referential Integrity check box below the table information. • If you choose not to enforce referential integrity, you can add new records, change key fields, or delete related records without worrying about referential integrity—thus making it possible to change critical fields without being warned or prevented from doing so. • With no integrity active, you can create tables that have orphans (student not enrolled in any program) or parents without children (program in which no student is enrolled). • With normal operations (such as data entry or changing information), referential integrity rules should be enforced. By setting this option, you can specify several additional options.

  24. Enforcing referential integrity (contd.) • Re-open the Edit Relationships dialog box for the relations between the Program and Student tables by double-clicking on the join line. • Clicking the check box in front of the option Enforce Referential Integrity activates the two Cascading choices (Update and Delete) in the dialog box and enforces Referential Integrity.

  25. Choosing the Cascade Update Related Fields option • If you specify Enforce Referential Integrity in the Edit Relationships dialog box, Access activates a check box option labeled Cascade Update Related Fields. • This option tells Access that a user can change the contents of a related field (the primary key field in the primary table) • When the user changes the contents of the primary key field in the primary table, Access verifies that the change is to a unique value (because there cannot be duplicate records in the primary table) and then goes through the related records in the many table and changes the foreign key value from the old value to the new value. • If this option is not selected, you cannot change the primary key value in the primary table that is used in a relationship with another table.

  26. Choosing the Cascade Delete Related Records option • Similarly, if you specify Enforce Referential Integrity in the Edit Relationships dialog box, Access activates the Cascade Delete Related Records check box. • By selecting this option, you tell Access that if a user attempts to delete a record in a primary table that has child records, first it must delete all the related child records and then delete the primary record. • If you do not specify this option, Access will not enable you to delete a record that has related records in another table.

  27. Finalizing Relationships • Now you can create relationships among the remaining tables.

  28. After relationships are created

  29. making change in child table

  30. Deleting record from parent table

  31. Creating many to many relationship • Create a junction table carrying key fields of the two tables to be related. • To be explained in class!

  32. The end

More Related