Skip to content

Instantly share code, notes, and snippets.

@iancanderson
Created October 10, 2011 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iancanderson/1276198 to your computer and use it in GitHub Desktop.
Save iancanderson/1276198 to your computer and use it in GitHub Desktop.
Get foreign key columns names of SQL SERVER tables
select pk.TABLE_NAME, c.COLUMN_NAME as 'foreign_key'
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
where
CONSTRAINT_TYPE = 'PRIMARY KEY'
and c.TABLE_NAME = pk.TABLE_NAME
and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
order by pk.TABLE_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment