You are on page 1of 4

Foreign Keys

The remainder of the CREATE/ALTER statements can be listed in any order. The order chosen
here was based on which parts of this script were coded first. Feel free to adjust order if it helps
in the consumption of this scripted metadata. In the case of foreign keys, we generated ALTER
TABLE statements previously, which saved time when this moment came along. Here is the
collection TSQL used to initially collect foreign key information:

1  

2 SELECT @Sql_Command = '

3 USE [' + @Database_Name + '];

4  

5 SELECT

6 FOREIGN_KEY_DATA.name AS Foreign_Key_Name,

7 FOREIGN_KEY_SCHEMA.name AS Foreign_Key_Schema_Name,

8 FOREIGN_KEY_TABLE.name AS Foreign_Key_Table_Name,

9 ''ALTER TABLE ['' + FOREIGN_KEY_SCHEMA.name + ''].['' + FOREIGN_KEY_TABLE.name + '']

WITH '' + CASE WHEN FOREIGN_KEY_DATA.is_not_trusted = 1 THEN ''NOCHECK'' ELSE ''CHECK'' END +
10

   '' ADD CONSTRAINT ['' +


11
FOREIGN_KEY_DATA.name + '']
12
FOREIGN KEY('' + STUFF(( SELECT '', '' + FOREIGN_KEY_COLUMN.name
13
FROM sys.foreign_keys
14
INNER JOIN sys.foreign_key_columns
15
ON foreign_keys.object_id = foreign_key_columns.constraint_object_id
16
INNER JOIN sys.tables FOREIGN_KEY_TABLE
17
ON foreign_keys.parent_object_id = FOREIGN_KEY_TABLE.object_id
18
INNER JOIN sys.schemas FOREIGN_KEY_SCHEMA
19
ON FOREIGN_KEY_SCHEMA.schema_id = FOREIGN_KEY_TABLE.schema_id
20 INNER JOIN sys.columns as FOREIGN_KEY_COLUMN

21 ON foreign_key_columns.parent_object_id = FOREIGN_KEY_COLUMN.object_id

22 AND foreign_key_columns.parent_column_id = FOREIGN_KEY_COLUMN.column_id

23 WHERE FOREIGN_KEY_DATA.object_id = foreign_keys.object_id

24 AND FOREIGN_KEY_DATA.name = foreign_keys.name

25 ORDER BY FOREIGN_KEY_TABLE.name, foreign_key_columns.constraint_column_id

26 FOR XML PATH('''')), 1, 2, '''') + '')

27 REFERENCES ['' + FOREIGN_KEY_SCHEMA.name + ''].['' + REFERENCED_TABLE.name + ''] ('' +

28 STUFF(( SELECT '', '' + REFERENECD_COLUMN.name

29 FROM sys.foreign_keys

30 INNER JOIN sys.foreign_key_columns

31 ON foreign_keys.object_id = foreign_key_columns.constraint_object_id

32 INNER JOIN sys.tables REFERENCED_TABLE

33 ON foreign_keys.referenced_object_id =

REFERENCED_TABLE.object_id
34

INNER JOIN sys.schemas REFERENCED_KEY_SCHEMA


35

ON REFERENCED_KEY_SCHEMA.schema_id =
36
REFERENCED_TABLE.schema_id
37
INNER JOIN sys.columns REFERENECD_COLUMN
38
ON foreign_key_columns.referenced_object_id =
39
REFERENECD_COLUMN.object_id

40
AND foreign_key_columns.referenced_column_id =

41 REFERENECD_COLUMN.column_id

42 WHERE FOREIGN_KEY_DATA.object_id = foreign_keys.object_id

43 AND FOREIGN_KEY_DATA.name = foreign_keys.name

ORDER BY REFERENCED_TABLE.name,
foreign_key_columns.constraint_column_id

FOR XML PATH('''')), 1, 2, '''') + ''));

GO''
44

AS Foreign_Key_Creation_Script
45

FROM sys.foreign_keys FOREIGN_KEY_DATA


46

INNER JOIN sys.tables FOREIGN_KEY_TABLE


47

ON FOREIGN_KEY_DATA.parent_object_id = FOREIGN_KEY_TABLE.object_id
48

INNER JOIN sys.tables REFERENCED_TABLE


49

ON FOREIGN_KEY_DATA.referenced_object_id = REFERENCED_TABLE.object_id
50

INNER JOIN sys.schemas FOREIGN_KEY_SCHEMA


51

ON FOREIGN_KEY_SCHEMA.schema_id = FOREIGN_KEY_TABLE.schema_id
52

INNER JOIN sys.schemas REFERENCED_KEY_SCHEMA


53

ON REFERENCED_KEY_SCHEMA.schema_id = REFERENCED_TABLE.schema_id';
54

INSERT INTO @Foreign_Keys


55

(Foreign_Key_Name, Foreign_Key_Schema_Name, Foreign_Key_Table_Name,


56
Foreign_Key_Creation_Script)

EXEC sp_executesql @Sql_Command;

List-building via XML was used in order to gather the foreign key column lists, as well as the
referenced column lists all in a single statement. The syntax here is almost identical to that used
for the collection of index key and included column lists. Since this work is already complete, the
steps needed to generate the foreign key creation scripts will be much simpler:

1  

2 IF EXISTS (SELECT * FROM @Foreign_Keys WHERE Foreign_Key_Schema_Name = @Schema_Name_Current AND

Foreign_Key_Table_Name = @Table_Name_Current)
3
BEGIN
4
SELECT
5
@Schema_Build_Text = @Schema_Build_Text + '
6
 
7
' + Foreign_Key_Creation_Script
8
FROM @Foreign_Keys
9
WHERE Foreign_Key_Schema_Name = @Schema_Name_Current
10
AND Foreign_Key_Table_Name = @Table_Name_Current;
11
END
12
 

IF EXISTS checks to see if any foreign keys exist on the table, and if so, add the foreign key
creation script onto our schema creation script.

You might also like