Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: 960 | Views in the last 30 days: 5
 
Related Articles
BLOG

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

Script foreign keys

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

FORUM

Script to find Foreign Key missing Index?

Script to find Foreign Key missing Index?

SCRIPT

Effectively mapping primary key – foreign key relations

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

SCRIPT

Find Foreign Keys

This script lists all foreign keys with parent and child table information

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones