Udostępnij za pośrednictwem


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

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