SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script for all Foreign Keys of all tables of a DB


Script for all Foreign Keys of all tables of a DB

Author
Message
Henk Schreij
Henk Schreij
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1138 Visits: 840
Comments posted to this topic are about the item Script for all Foreign Keys of all tables of a DB



neongladiator
neongladiator
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 85
I recently needed to script out all foreign keys in a database, and figured I would post that script.

SELECT 'ALTER TABLE [' + RTRIM(tabowner.name) + '].[' + RTRIM(tab.name) + '] ADD CONSTRAINT ['
+ RTRIM(fkey.name) + '] FOREIGN KEY ([' + RTRIM(col.name) + ']) REFERENCES ['
+ RTRIM(rtabowner.name) + '].[' + RTRIM(rtab.name) + '] ([' + RTRIM(rcol.name) + '])'
FROM sysforeignkeys sfk (NOLOCK)
JOIN sysobjects tab (NOLOCK)
ON tab.id = sfk.fkeyid
JOIN sysobjects rtab (NOLOCK)
ON rtab.id = sfk.rkeyid
JOIN syscolumns col (NOLOCK)
ON col.id = tab.id
AND col.colid = sfk.fkey
JOIN syscolumns rcol (NOLOCK)
ON rcol.id = rtab.id
AND rcol.colid = sfk.rkey
JOIN sysobjects fkey (NOLOCK)
ON fkey.id = sfk.constid
JOIN sysusers tabowner (NOLOCK)
ON tabowner.uid = tab.uid
JOIN sysusers rtabowner (NOLOCK)
ON rtabowner.uid = rtab.uid
MudLuck
MudLuck
Mr or Mrs. 500
Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)

Group: General Forum Members
Points: 521 Visits: 519
Hey Neon,

How do you account for FK that have more then one column? This query wont work for those. food for thought.



neongladiator
neongladiator
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 85
Hi Mud. I forgot to post the new version that I made once I remembered that not all FKs have only one column. Below is a longer version of the script that will account for any number of columns in the foreign keys.

--Declare variables used to concatenate column lists
DECLARE @column INT
, @maxcolumns INT

--Drop temp tables if they already exist
IF OBJECT_ID('tempdb..#foreignkeycolumns') IS NOT NULL DROP TABLE #foreignkeycolumns
IF OBJECT_ID('tempdb..#foreignkeys') IS NOT NULL DROP TABLE #foreignkeys

--Gather data for foreign key columns
SELECT FKeyID = fkey.id
, FKeyName = fkey.name
, TabOwner = tabowner.name
, TabID = tab.id
, TabName = tab.name
, ColName = col.name
, RTabOwner = rtabowner.name
, RTabID = rtab.id
, RTabName = rtab.name
, RColName = rcol.name
, KeyNo = sfk.keyno
INTO #foreignkeycolumns
FROM sysforeignkeys sfk (NOLOCK)
JOIN sysobjects tab (NOLOCK)
ON tab.id = sfk.fkeyid
JOIN sysobjects rtab (NOLOCK)
ON rtab.id = sfk.rkeyid
JOIN syscolumns col (NOLOCK)
ON col.id = tab.id
AND col.colid = sfk.fkey
JOIN syscolumns rcol (NOLOCK)
ON rcol.id = rtab.id
AND rcol.colid = sfk.rkey
JOIN sysobjects fkey (NOLOCK)
ON fkey.id = sfk.constid
JOIN sysusers tabowner (NOLOCK)
ON tabowner.uid = tab.uid
JOIN sysusers rtabowner (NOLOCK)
ON rtabowner.uid = rtab.uid

--Find distinct foreign keys
SELECT DISTINCT
FKeyID = FKeyID
, FKeyName = FKeyName
, TabOwner = TabOwner
, TabID = TabID
, TabName = TabName
, RTabOwner = RTabOwner
, RTabID = RTabID
, RTabName = RTabName
, Columns = CONVERT(VARCHAR(500),NULL)
, RColumns = CONVERT(VARCHAR(500),NULL)
INTO #foreignkeys
FROM #foreignkeycolumns

--Find max number of columns in any foreign key
SELECT @maxcolumns = MAX(KeyNo)
FROM #foreignkeycolumns

SET @column = 1

--Find first column in foreign key
UPDATE #foreignkeys
SET Columns = '[' + RTRIM(fkc.ColName) + ']'
, RColumns = '[' + RTRIM(fkc.RColName) + ']'
FROM #foreignkeys fk
JOIN #foreignkeycolumns fkc
ON fk.FKeyID = fkc.FKeyID
WHERE fkc.KeyNo = @column

--Concatenate list of columns for foreign keys
WHILE @column < @maxcolumns
BEGIN
SET @column = @column + 1

UPDATE #foreignkeys
SET Columns = Columns + ',[' + RTRIM(fkc.ColName) + ']'
, RColumns = RColumns + ',[' + RTRIM(fkc.RColName) + ']'
FROM #foreignkeys fk
JOIN #foreignkeycolumns fkc
ON fk.FKeyID = fkc.FKeyID
WHERE fkc.KeyNo = @column
END

--Create scripts for foreign keys
SELECT DISTINCT
'ALTER TABLE [' + RTRIM(TabOwner) + '].[' + RTRIM(TabName) + '] WITH NOCHECK ADD CONSTRAINT ['
+ RTRIM(FKeyName) + '] FOREIGN KEY (' + Columns + ') REFERENCES ['
+ RTRIM(RTabOwner) + '].[' + RTRIM(RTabName) + '] (' + RColumns + ')'
+ CHAR(10) + 'GO'
FROM #foreignkeys
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search