You can also use other features, such as triggers, to enforce relationships this use is procedural referential integrity. In this article, I look at how you can enforce referential integrity in SQL Server, paying particular attention to the relevant new features in SQL Server 2. SQL Server 7. 0 and earlier releases have only one way to deal with attempted violations of foreign key constraints. If anyone tries to modify data on a table in a way that would violate the referential integrity as defined through foreign keys, SQL Server disallows that modification and returns an error message. SQL Server 2. 00. Ill explain. First, lets look at a quick example to clarify what referential integrity is all about. The Northwind database has a table called Orders and one called Order Details. In the Orders table, the Order. Id column is the primary key that uniquely identifies each order. The Order Details table also has an Order. Id column, but in this table, the column is a foreign key that must match an existing Order. Exporting data from SQL Server to Excel seems like a reasonably simple request. I just need to write out a few reports for users on a regular basis, nothing too fancy. Learn how to use SQL Server 2000s new methods of enforcing referential integrity in your databases. I cant seem to figure out how this is happening. Heres an example of the file that Im attempting to bulk insert into SQL server 2005 A NICE HEADER HERE. The 1 SQL Server community and education site, with articles, news, forums, scripts and FAQs. The following article demonstrates how to bulk insert data into SQL Server 2008 using SqlBulkCopy. Id in the Orders table. In this example, the Orders table is the referenced table and the Order Details table is the referencing table. Find details on how to insert the OPENROWSET function to retrieve data from Microsoft SQL Server as well as how to bulk load data into a SQL Server table. When a new TSQL feature is introduced in SQL Server, it usually takes some time before users realize certain aspects of working with the feature. In time, people. Learn SQL Server basics in this tutorial for beginners new to SQL Server and DBAs who want a refresher. Get SQL Server help on the basic topics and features. If youve set up a foreign key constraint to enforce the relationship between Orders and Order Details, SQL Server verifies that modifying either of those two tables doesnt violate the relationship. For example, if you try to delete a row from the Orders table when the Order. Id in that row exists in the Order Details table, the deletion will violate the referential integrity constraint. Trying to update an Order. Id column in the Orders table when the original value exists in Order Details but the new value doesnt is also a violation. And, SQL Server must verify every insertion into Order Details to ensure that the new Order. Id exists in the Orders table and must verify every update of the Order. Id column in Order Details. Referential Actions. The ANSI SQL 9. 2 standard contains the concept of a referential action. Sometimes, instead of preventing a data modification operation that would violate a foreign key reference, you might want the database system to perform another, compensating action that allows the modification and still honors the constraint. For example, if you delete an Orders table row that Order Details references, you could instruct SQL Server to automatically delete all related Order Details rows i. Order Details. That way, you can modify the Orders table without violating the constraint. The ANSI standard defines four possible referential actions that apply to deletes from or updates to the referenced table NO ACTION, CASCADE, SET DEFAULT, and SET NULL. The NO ACTION option, which is the ANSI standard default, prevents the modification. CASCADE allows a delete or update of all matching rows in the referencing table. SET DEFAULT lets the delete or update occur but sets all foreign key values in the referencing table to a default value. And SET NULL allows the delete or update and sets all foreign key values in the referencing table to NULL. Note that these actions apply only to modifications to the referenced table. When modifying the referencing table, you have only one possible referential integrity action If you insert or update a value in the foreign key column, the new value must match a value in the referenced table, or SQL Server will reject the modification. Related Avoid Referential Integrity Errors When Deleting Records from Databases Enforcing Referential Integrity. SQL Server releases before SQL Server 6. All referential integrity validation must take place through triggers, which you have to code in T SQL. SQL Server 6. 0 introduced constraints, including primary and foreign key constraints, but limits referential actions to NO ACTION. SQL Server 7. 0 and 6. NO ACTION option without the NO ACTION syntax. SQL Server 2. 00. CASCADE and NO ACTION referential actions since beta 1. Listing 1 shows the syntax for creating two simple tables Table. Table. 2. Table. 2 has a foreign key that references Table. CASCADE for updates and NO ACTION for delete operations. NO ACTION is the default, so specifying it explicitly in the constraint definition is optional. The listing then attempts insertions into the two tables, including an insertion into Table. SQL Server rejects the violation and returns the following error message Server Msg 5. Level 1. 6, State 1, Line 1. INSERT statement conflicted with COLUMN FOREIGN KEYconstraint FKTable. Table. 1a. The conflict occurred indatabase pubs, table Table. The statement has been terminated. The code then tries to delete a referenced row from Table. Server Msg 5. 47, Level 1. State 1, Line 1. DELETE statement conflicted with COLUMN REFERENCE constraintFKTable. Table. 1a. The conflict occurred in database pubs, table Table. The statement has been terminated. The code then successfully deletes from Table. Table. 2 doesnt reference. Finally, the code updates all primary key values in Table. Table 2. You can also define foreign keys through the SQL Server Enterprise Manager. In the left pane, open the database, then select Tables. When the right pane lists all the tables, right click the referencing table, and select Design Table. Select the Table and Index Properties button on the toolbar, and select the Relationships tab. Figure 1 shows the definition of a foreign key from Table. Table. 1. The dialog box lets you name the constraint and choose the primary key referenced table and the foreign key referencing table. Drop down lists let you choose which column from each table will make up the key. At the bottom of the dialog box, you can choose to maintain the relationship between the tables by applying a CASCADE operation for updates to the primary key, deletes to the primary key, or both. When a box is clear, the default behavior of NO ACTION disallowing the change is in effect. Triggers. As I mentioned, in releases earlier than SQL Server 6. In SQL Server 7. 0 and 6. NO ACTION. In SQL Server 2. Even with support for the CASCADE action, SQL Server 2. ANSI defined referential actions. To delete rows from a referenced table and have the referencing table replace the foreign key with a default value or NULL, you still must use triggers. Listing 2 shows a simple trigger that sets all referencing values in Table. NULL in the case of a delete from Table. You can also use triggers to make error messages friendlier. The preceding section showed the error messages that SQL Server generates automatically when you violate a foreign key constraint they arent pretty. If you write triggers to check referential integrity, you can produce any error message you want.