After you've set up different tables for each subject in your Microsoft Access database, you need a way of telling Microsoft Access how to bring that information back together again. The first step in this process is to define relationships between your tables. After you've done that, you can create queries, forms, and reports to display information from several tables at once. For example, this form includes information from four tables:
The Customers table
The Orders table
The Products table
The Order Details table
In the previous example, the fields in four tables must be coordinated so that they show information about the same order. This coordination is accomplished with relationships between tables. A relationship works by matching data in key fields
EmployeeID appears in both tables
... and as a foreign key.
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.
One supplier ...
... can supply more than one product ...
... but each product has only one supplier.
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 table) whose primary key consists of two fields
Primary key from the Orders table
Primary key from the Products table
One order can have many products ...
... and each product can appear on many orders.
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. You might use a one-to-one relationship to divide a table with many fields, to isolate part of a table for security reasons, or to store information that applies only to a subset of the main table. For example, you might want to create a table to track employees participating in a fundraising soccer game. Each soccer player in the Soccer Players table has one matching record in the Employees table.
Each soccer player has one matching record in the Employees table.
This set of values is a subset of the EmployeeID field and the Employees table.
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-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.
You can also create a relationship between a table and itself. This is useful in situations where you need to perform a Lookup within the same table. In the Employees table, for example, you can define a relationship between the EmployeeID and ReportsTo fields, so that the ReportsTo field can display employee data from a matching EmployeeID.
Note If you drag a field that isn't a primary key and doesn't have a unique index to another field that isn't a primary key and doesn't have a unique index, an indeterminate relationship is created. In queries containing tables with an indeterminate relationship, Microsoft Access displays a default join line between the tables, but referential integrity won't be enforced, and there's no guarantee that records are unique in either table.
Referential integrity is a system of rules that Microsoft Access uses to ensure that relationships between records in related tables are valid, and that you don't accidentally delete or change related data. You can set referential integrity when all of the following conditions are met:
- The matching field from the primary table is a primary key or has a unique index.
- The related fields have the same data type. There are two exceptions. An AutoNumber field can be related to a Number field with a FieldSize property setting of Long Integer, and an AutoNumber field with a FieldSize property setting of Replication ID can be related to a Number field with a FieldSize property setting of Replication ID.
- Both tables belong to the same Microsoft Access database. If the tables are linked tables, they must be tables in Microsoft Access format, and you must open the database in which they are stored to set referential integrity. Referential integrity can't be enforced for linked tables from databases in other formats.
The following rules apply when you use referential integrity:
- You can't enter a value in the foreign key field of the related table that doesn't exist in the primary key of the primary table. However, you can enter a Null value in the foreign key, specifying that the records are unrelated. For example, you can't have an order that is assigned to a customer that doesn't exist, but you can have an order that is assigned to no one by entering a Null value in the CustomerID field.
- You can't delete a record from a primary table if matching records exist in a related table. For example, you can't delete an employee record from the Employees table if there are orders assigned to the employee in the Orders table.
- You can't change a primary key value in the primary table, if that record has related records. For example, you can't change an employee's ID in the Employees table if there are orders assigned to that employee in the Orders table.
For relationships in which referential integrity is enforced, you can specify whether you want Microsoft Access to automatically cascade update and cascade delete related records. If you set these options, delete and update operations that would normally be prevented by referential integrity rules are allowed. When you delete records or change primary key values in a primary table, Microsoft Access makes necessary changes to related tables to preserve referential integrity.
If you select the Cascade Update Related Fields check box when defining a relationship, any time you change the primary key of a record in the primary table, Microsoft Access automatically updates the primary key to the new value in all related records. For example, if you change a customer's ID in the Customers table, the CustomerID field in the Orders table is automatically updated for every one of that customer's orders so that the relationship isn't broken. Microsoft Access cascades updates without displaying any message.
Note If the primary key in the primary table is an AutoNumber field, setting the Cascade Update Related Fields check box will have no effect, because you can't change the value in an AutoNumber field.
If you select the Cascade Delete Related Records check box when defining a relationship, any time you delete records in the primary table, Microsoft Access automatically deletes related records in the related table. For example, if you delete a customer record from the Customers table, all the customer's orders are automatically deleted from the Orders table (this includes records in the Order Details table related to the Orders records). When you delete records from a form or datasheet with the Cascade Delete Related Records check box selected, Microsoft Access warns you that related records may also be deleted. However, when you delete records using a delete query, Microsoft Access automatically deletes the records in related tables without displaying a warning.
Working with other applications
Microsoft Access 2002 or later provides features for working with other programs.
- Work with Microsoft SQL Server You can create a Microsoft Access project that is easy to connect to a Microsoft SQL Server database, or use the Microsoft SQL Server Database Wizard to quickly create a SQL Server database and an Access project at the same time. Working with an Access project is similar to working with a Microsoft Access database
— the process of creating forms, reports, data access pages, macros, and modules is the same. Once you connect to a SQL Server database, you can view, create, modify, and delete tables, views, stored procedures, and database diagrams using the Microsoft SQL Server Design Tools. - Create a new Access database from data in another file format You can open a file that is in another file format
— such as text, dBASE, Paradox, or spreadsheet format — in Access; Microsoft Access automatically creates an Access database and links the file for you. - Import or link data from Microsoft Outlook or Microsoft Exchange You can use the Exchange Import Wizard or the Outlook Import Wizard to import or link data from Microsoft Outlook and Microsoft Exchange Server. For example, you might want to link to your Microsoft Outlook Contacts folder and then create form letters and mailing labels by merging the data with the Microsoft Word Mail Merge Wizard.
- Export to other sources You can export data to a number of formats and applications from Access.
Data formats you can export to
Application Version or format supported Microsoft Access database 2.0, 7.0/95, 8.0/97, 9.0/2000, 10.0/Access 2002 or later Microsoft Access project 9.0/2000, 10.0/Access 2002 or later dBASE III, IV, and 5; and 7 (Requires Borland Database Engine 4.x or later. Updated drivers may be available from Microsoft Technical Support) Paradox, Paradox for Microsoft Windows 3.x, 4.x, and 5.0; and 8.0 (Requires Borland Database Engine 4.x or later. Updated drivers may be available from Microsoft Technical Support) Microsoft Excel 3.0, 4.0, 5.0, 7.0/95, 8.0/97, 9.0/2000, and 10.0/Excel 2002 or later Microsoft Word, Rich Text Format All Lotus 1-2-3 .wj2, .wk1 and .wk3 formats Delimited text files All character sets Fixed-width text files All character sets HTML and IDC/HTX 1.0 (if a list)
2.0, 3.x, 4.x (if a table or list)Microsoft Active Server Pages All XML Documents All SQL tables, Microsoft Visual FoxPro, and programs and databases that support the ODBC protocol Visual FoxPro 3.0, 5.0, and 6.x For an up-to-date list of supported ODBC drivers, see the Microsoft Knowledge Base.
- Import or link data to an Access database Just as you can export to a number of formats and applications, you can also work with the data from other sources and applications.
Data formats you can import or link to
Data source Version or format supported Microsoft Access database 2.0, 7.0/95, 8.0/97, 9.0/2000, 10.0/Access 2002 or later Microsoft Access project 9.0/2000, 10.0/Access 2002 or later dBASE III, IV, and 5; and 7 (Linking requires updated ISAM drivers available from Microsoft Technical Support) Paradox, Paradox for Microsoft Windows 3.x, 4.x, and 5.0; and 8.0 (Linking requires updated ISAM drivers available from Microsoft Technical Support) Microsoft Excel spreadsheets 3.0, 4.0, 5.0, 7.0/95, 8.0/97, 9.0/2000, and 10.0/Excel 2002 or later Lotus 1-2-3 spreadsheets (Linking is read-only) .wks, .wk1, .wk3, and .wk4 Microsoft Exchange All versions Delimited text files All character sets Fixed-width text files All character sets HTML 1.0 (if a list)
2.0, 3.x (if a table or list)XML Documents All versions SQL tables, Microsoft Visual FoxPro, and data from other programs and databases that support the ODBC protocol Visual FoxPro 2.x, 3.0, 5.0, and 6.x (import only) For an up-to-date list of supported ODBC drivers, see the Microsoft Knowledge Base.