Query to find all FK constraints and their delete

2019-02-12 04:23发布

问题:

In SQL Server 2005, can I issue an SQL query to list all FK constraints on tables within the DB, and show the delete rule? (ie nothing, cascade, set null, or set default)

The output I'm looking for is something akin to:

FK_NAME                  ON_DELETE
==================================
FK_LINEITEM_STATEMENT    CASCADE
FK_ACCOUNTREP_CLIENT     NOTHING

回答1:

You can try this:

SELECT name, delete_referential_action_desc
FROM sys.foreign_keys


回答2:

Little late to the game here, but you might also try this:

select * from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS


回答3:

You can use also expression in the WHERE block:

objectproperty(object_id('FK_your_constraint_name'), 'CnstIsDeleteCascade')

or

objectproperty(your_constraint_object_id, 'CnstIsDeleteCascade')