Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Report to return all foreign key information

By max,

The purpose of this task is to generate a report showing all your foreign keys. Generally this would be considered  nothing special only that this script also returns the tables, column foreign key constraint name and the relationship properties.

Here's how to do it:
First of all copy the script and paste it in Query Analyzer

Step 1: Create a temporary table to store table and column data (recommended and part of this script).

Step 2: Execute the final sql statement
Step 3: Drop the local temporary table (optional)

Note: If you have any columns in a table that are referenced more than once as a foreign key you will get duplicity for those columns, which is an excellent way of verifying your relationships too.

Total article views: 965 | Views in the last 30 days: 3
Related Articles

Script Out Foreign Keys With Multiple Keys

It's easy enough to use sys.foreign_keys and sys.foreign_key_columns to identify foreign keys.  But ...


Script foreign keys

Using this script you can create a list of sql commands for foreign keys manipulation.


Script to find Foreign Key missing Index?

Script to find Foreign Key missing Index?


Script to Generate Foreign Keys

Generate Script for existing Foreign Keys for any emergency.


Effectively mapping primary key – foreign key relations

This script maps the primary key - foreign key relations regardless if the column names are the same...