Share via


How to: Disable Foreign Key Constraints with INSERT and UPDATE Statements

Select the option to disable a foreign key constraint during INSERT and UPDATE transactions if you know that new data will violate the constraint or if the constraint applies only to the data already in the database.

Note

A new version of Table Designer appears for databases in the SQL Server 2012 format. This topic describes the old version of Table Designer, which you use with databases in earlier formats of SQL Server.

In the new version, you can change a table definition through a graphical interface or directly in a script pane. If you use the graphical interface, the table’s definition is automatically updated in the script pane. To apply the SQL code in the script pane, choose the Update button. For more information about the new version, see How to: Create Database Objects Using Table Designer.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To disable a foreign key constraint for INSERT and UPDATE statements

  1. In Server Explorer, select the table with the constraint, and from the Database menu click Open Table Definition.

    The table opens in Table Designer.

  2. From the Table Designer menu, click Relationships.

  3. In the Foreign Key Relationships dialog box, select the relationship in the Selected Relationship list.

  4. In the grid, click Delete Rule or Update Rule and choose an action from the drop-down list box to the left of the property.

    • No Action   An error message tells the user that the deletion is not allowed and the DELETE is rolled back.

    • Cascade   Deletes all rows containing data involved in the foreign key relationship.

    • Set Null   Sets the value to null if all foreign key columns for the table can accept null values. Applies to SQL Server 2005 only.

    • Set Default   Sets the value to the default value defined for the column if all foreign key columns for the table have defaults defined for them. Applies to SQL Server 2005 only.

Note

If you plan to use triggers to implement database operations, you must disable foreign key constraints in order for the trigger to run.

See Also

Reference

Foreign Key Relationships Dialog Box

Other Resources

Working with Constraints

Working with Relationships