Relationship Importance

philljp390

Registered User.
Local time
Today, 21:58
Joined
Sep 7, 2003
Messages
64
I have my database all designed with all the queries and tables. Tables and queries are linked throughout.

How important is it then to define all these relationships under Tools-->Relationships

Is it really necessary?
 
I'd have defined the relationship before making any queries - it's only from the Relationships window that you can set Referential Integrity.
 
Yeah i was thinking that, really the relationships should be defined first.

Could you explain Referential Integrity please?
 
From the Microsoft Help Files:

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.

Cascading updates and deletes

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.
 
What im wondering is if the required relationships have all been defined in the queries themselves. Do you actually need the relationships defined in Tools-->Relationships as well?
 
Last edited:
It seems you can define relationships in the queries which aren't defined in the main relationships. Is that correct?
 
What im wondering is if the required relationships have all been defined in the queries themselves. Do you actually need the relationships defined in Tools-->Relationships as well?
- Joins inqueries do NOT define relationships. Relationships are ONLY defined in the relationship window. It is important that you check the Enforce Referential Integrity box or you may as well not bother to define the relationship at all.

It seems you can define relationships in the queries which aren't defined in the main relationships. Is that correct?
Yes. On rare occassions you may need to join on non-relationship fields.
 
How important is it then to define all these relationships under Tools-->Relationships

I'll take a shot at answering that.

If you define the relationships first, then try to build a JOIN query between two such related tables, Access will build it for you automagically. If you try to build reports from such auto-JOIN queries, the report wizard handles it cleaner. If you define a query that violates a JOIN, you will be told about it so you can decide if you really meant what you said.

Having relationships defined first automates a LOT of things that otherwise you would have to do by hand.

Access is considered a "rapid application development" (RAD) environment. The relationships are an important component of the RAD setup because they help you in the development stage.

BUT... they help you even more in the design stage, 'cause if you can't define relationships between tables that SHOULD be related, you know you have something wrong to start with.

Does that help?
 

Users who are viewing this thread

Back
Top Bottom