Query to find all FK constraints and their delete rules (SQL Server)

五迷三道 提交于 2019-12-03 19:21:16

问题


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')


来源:https://stackoverflow.com/questions/3877518/query-to-find-all-fk-constraints-and-their-delete-rules-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!