A relationship is a way of formally defining how two tables relate to each other. When you define a relationship, you are telling the database engine which two fields in two related tables are joined. The two fields involved in a relationship are the primary key, introduced earlier in this chapter, and the foreign key. The foreign key is the key in the related table that stores a copy of the primary key of the main table. For example, suppose that you have tables for departments and employees. There is a one-to-many relationship between a department and a group of employees. Every department has its own ID, as does each employee. In order to denote which department an employee works in, however, you must copy the department's ID into each employee's record. So, to identify each employee as a member of a department, the Employees table must have a field say, DepartmentId to store the ID of the department to which that employee belongs. The DepartmentID field in the Employees table is referred to as the foreign key of the Employees table, because it stores a copy of the primary key of the Departments table. A relationship, then, tells the database engine which two tables are involved and which foreign key is related to which primary key. The old Access/JET engine doesn't require that you explicitly declare relationships, but it's advantageous for you to do so. The reason is that it simplifies the task of retrieving data based on records joined across two or more tables (discussed in more detail in Chapter 2). This lack of declaration is one of the major weaknesses of the JET technology and by far a good reason to upgrade any legacy applications still using JET to use ADO.NET. In addition to matching related records in separate tables, you also need to define a relationship to take advantage of referential integrity, a database engine property that keeps data in a multitable database consistent. When referential integrity exists in a database, the database engine prevents you from removing a record when other records are related to that record in the database. After you define a relationship in your database, the definition of the relationship is stored until you remove it. You can define relationships graphically, using a database diagram in VS.NET, SQL Enterprise Manager, or Visio, or programmatically with SQL DDL commands. Using Referential Integrity to Maintain ConsistencyWhen tables are linked through relationships, the data in each table must remain consistent with that in the linked tables. Referential integrity manages this task by keeping track of the relationships among tables and prohibiting certain types of operations on records. For example, suppose that you have one table called tblCustomer and another table called tblOrder. The two tables are related through a common field the ID field in tblCustomer relates to the CustomerID field in tblOrder. The premise here is that you create customers that are stored in tblCustomer and then create orders that are stored in tblOrder. But what happens if you run a process that deletes a customer who has outstanding orders stored in the order table? Or what if you create an order that doesn't have a valid CustomerID attached to it? An order without a CustomerID can't be shipped, because the shipping address is a function of the record in tblCustomer. In such a situation the data is said to be in an inconsistent state. Because your database must not become inconsistent, many database engines (including SQL Server) provide a way for you to define formal relationships among tables, as discussed earlier in this chapter. When you formally define a relationship between two tables, the database engine can monitor the relationship and prohibit any operation that violates referential integrity. Referential integrity constraints generate application errors whenever the application attempts to perform an action that would leave data in an inconsistent state. For example, in a database with referential integrity activated, if you attempted to create an order that contains a customer ID for a customer who didn't exist, you'd get an error and the order wouldn't be created. Testing Referential Integrity Constraints, Using Server ExplorerNow that you have a database with the related tblCustomer and tblOrder tables, you can use Server Explorer to examine and test the relationship between them. To do so, follow these steps.
To test the constraint imposed by the relationship, do the following.
There's no need actually to enter the data (the error message is what we were looking for). However, if you needed to create an order for some reason, you'd first need to create a customer, get the ID for that customer, and use it in the CustomerID field when creating an order. In a real application, this problem would be handled gracefully and automatically you'd typically design a user interface to avoid the problem in the first place. We discuss a variety of strategies to deal with manipulating related records consistently throughout this book. Cascading Updates and Cascading DeletesCascading updates and cascading deletes are useful features of the SQL Server database engine. They cause the following things to happen in your database.
Note Be cautious when setting up relationships that perform cascading updates and cascading deletes in your data designs. If you don't plan carefully, you could wind up deleting (or updating) more data than you intended. Some database developers avoid the use of cascades altogether, preferring explicitly to maintain referential integrity across related tables. That's fine, but once you get the hang of how they work, you'll probably find cascades easy to program. Cascading updates and cascading deletes work only if you've established a relationship between two tables. If you always create tables with AutoNumber or, in SQL Server terms, AutoIncrement primary keys you'll probably find that cascading deletes is more useful than cascading updates. The reason is that you can't change the value of an AutoNumber or AutoIncrement field (so there's no "update" to "cascade"). You can examine how cascading deletes work by using the tools provided by Server Explorer, as follows.
|