SQL Server: Howto get foreign key reference from information_schema? - sql

In SQL Server, how can I get the referenced table + column name from a foreign key?
Note: Not the table/column where the key is in, but the key it refers to.
Example:
When the key [FA_MDT_ID] in table [T_ALV_Ref_FilterDisplay].
refers to [T_AP_Ref_Customer].[MDT_ID]
such as when creating a constraint like this:
ALTER TABLE [dbo].[T_ALV_Ref_FilterDisplay] WITH CHECK ADD CONSTRAINT [FK_T_ALV_Ref_FilterDisplay_T_AP_Ref_Customer] FOREIGN KEY([FA_MDT_ID])
REFERENCES [dbo].[T_AP_Ref_Customer] ([MDT_ID])
GO
I need to get [T_AP_Ref_Customer].[MDT_ID]
when given [T_ALV_Ref_FilterAnzeige].[FA_MDT_ID] as input

Never mind, this is the correct answer:
http://msdn.microsoft.com/en-us/library/aa175805(SQL.80).aspx
SELECT
KCU1.CONSTRAINT_SCHEMA AS FK_CONSTRAINT_SCHEMA
,KCU1.CONSTRAINT_NAME AS FK_CONSTRAINT_NAME
,KCU1.TABLE_SCHEMA AS FK_TABLE_SCHEMA
,KCU1.TABLE_NAME AS FK_TABLE_NAME
,KCU1.COLUMN_NAME AS FK_COLUMN_NAME
,KCU1.ORDINAL_POSITION AS FK_ORDINAL_POSITION
,KCU2.CONSTRAINT_SCHEMA AS REFERENCED_CONSTRAINT_SCHEMA
,KCU2.CONSTRAINT_NAME AS REFERENCED_CONSTRAINT_NAME
,KCU2.TABLE_SCHEMA AS REFERENCED_TABLE_SCHEMA
,KCU2.TABLE_NAME AS REFERENCED_TABLE_NAME
,KCU2.COLUMN_NAME AS REFERENCED_COLUMN_NAME
,KCU2.ORDINAL_POSITION AS REFERENCED_ORDINAL_POSITION
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU1
ON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG
AND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
AND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU2
ON KCU2.CONSTRAINT_CATALOG = RC.UNIQUE_CONSTRAINT_CATALOG
AND KCU2.CONSTRAINT_SCHEMA = RC.UNIQUE_CONSTRAINT_SCHEMA
AND KCU2.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION
Note:
Information_schema doesn't contain indices (it does find unique-contraints).
So if you want to find foreign-keys based on unique-indices, you need to go over the microsoft proprietary tables:
SELECT
fksch.name AS FK_CONSTRAINT_SCHEMA
,fk.name AS FK_CONSTRAINT_NAME
,sch1.name AS FK_TABLE_SCHEMA
,t1.name AS FK_TABLE_NAME
,c1.name AS FK_COLUMN_NAME
-- The column_id is not the ordinal, it can be dropped and then there's a gap...
,COLUMNPROPERTY(c1.object_id, c1.name, 'ordinal') AS FK_ORDINAL_POSITION
,COALESCE(pksch.name,sch2.name) AS REFERENCED_CONSTRAINT_SCHEMA
,COALESCE(pk.name, sysi.name) AS REFERENCED_CONSTRAINT_NAME
,sch2.name AS REFERENCED_TABLE_SCHEMA
,t2.name AS REFERENCED_TABLE_NAME
,c2.name AS REFERENCED_COLUMN_NAME
,COLUMNPROPERTY(c2.object_id, c2.name, 'ordinal') AS REFERENCED_ORDINAL_POSITION
FROM sys.foreign_keys AS fk
LEFT JOIN sys.schemas AS fksch
ON fksch.schema_id = fk.schema_id
-- not inner join: unique indices
LEFT JOIN sys.key_constraints AS pk
ON pk.parent_object_id = fk.referenced_object_id
AND pk.unique_index_id = fk.key_index_id
LEFT JOIN sys.schemas AS pksch
ON pksch.schema_id = pk.schema_id
LEFT JOIN sys.indexes AS sysi
ON sysi.object_id = fk.referenced_object_id
AND sysi.index_id = fk.key_index_id
INNER JOIN sys.foreign_key_columns AS fkc
ON fkc.constraint_object_id = fk.object_id
INNER JOIN sys.tables AS t1
ON t1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas AS sch1
ON sch1.schema_id = t1.schema_id
INNER JOIN sys.columns AS c1
ON c1.column_id = fkc.parent_column_id
AND c1.object_id = fkc.parent_object_id
INNER JOIN sys.tables AS t2
ON t2.object_id = fkc.referenced_object_id
INNER JOIN sys.schemas AS sch2
ON sch2.schema_id = t2.schema_id
INNER JOIN sys.columns AS c2
ON c2.column_id = fkc.referenced_column_id
AND c2.object_id = fkc.referenced_object_id
Proof-test for edge-cases:
CREATE TABLE __groups ( grp_id int, grp_name varchar(50), grp_name2 varchar(50) )
ALTER TABLE __groups ADD CONSTRAINT UQ___groups_grp_name2 UNIQUE (grp_name2)
CREATE UNIQUE INDEX IX___groups_grp_name ON __groups(grp_name)
GO
CREATE TABLE __group_mappings( map_id int, map_grp_name varchar(50), map_grp_name2 varchar(50), map_usr_name varchar(50) )
GO
ALTER TABLE __group_mappings ADD CONSTRAINT FK___group_mappings___groups FOREIGN KEY(map_grp_name)
REFERENCES __groups (grp_name)
GO
ALTER TABLE __group_mappings ADD CONSTRAINT FK___group_mappings___groups2 FOREIGN KEY(map_grp_name2)
REFERENCES __groups (grp_name2)
GO
SELECT ##VERSION -- Microsoft SQL Server 2016 (SP1-GDR) (KB4458842)
SELECT version() -- PostgreSQL 9.6.6 on x86_64-pc-linux-gnu
GO

If you can live with using the SQL Server specific schema catalog views, this query will return what you're looking for:
SELECT
fk.name,
OBJECT_NAME(fk.parent_object_id) 'Parent table',
c1.name 'Parent column',
OBJECT_NAME(fk.referenced_object_id) 'Referenced table',
c2.name 'Referenced column'
FROM
sys.foreign_keys fk
INNER JOIN
sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN
sys.columns c1 ON fkc.parent_column_id = c1.column_id AND fkc.parent_object_id = c1.object_id
INNER JOIN
sys.columns c2 ON fkc.referenced_column_id = c2.column_id AND fkc.referenced_object_id = c2.object_id
Not sure how - if at all - you can get the same information from the INFORMATION_SCHEMA views....

I wanted a a version that would let me find all the "Key" and "ID" columns having/missing a constraint. So i wanted all columns compared to the list of all PK OR FK OR Null, here is my query. Hope it helps someone else!
SELECT
c.table_schema
,c.table_name
,c.column_name
,KeyConstraints.constraint_type
,KeyConstraints.constraint_schema
,KeyConstraints.constraint_name
,KeyConstraints.referenced_table_schema
,KeyConstraints.referenced_table_name
,KeyConstraints.referenced_column_name
,KeyConstraints.update_rule
,KeyConstraints.delete_rule
FROM information_schema.columns AS c
LEFT JOIN
(
SELECT
FK.table_schema AS TABLE_SCHEMA
,FK.table_name
,CU.column_name
,FK.constraint_type
,c.constraint_schema
,C.constraint_name
,PK.table_schema AS REFERENCED_TABLE_SCHEMA
,PK.table_name AS REFERENCED_TABLE_NAME
,CCU.column_name AS REFERENCED_COLUMN_NAME
,C.update_rule
,C.delete_rule
FROM information_schema.referential_constraints AS C
INNER JOIN information_schema.table_constraints AS FK
ON C.constraint_name = FK.constraint_name
INNER JOIN information_schema.table_constraints AS PK
ON C.unique_constraint_name = PK.constraint_name
INNER JOIN information_schema.key_column_usage AS CU
ON C.constraint_name = CU.constraint_name
INNER JOIN information_schema.constraint_column_usage AS CCU
ON PK.constraint_name = CCU.constraint_name
WHERE ( FK.constraint_type = 'FOREIGN KEY' )
UNION
SELECT
ccu.table_schema
,ccu.table_name
,ccu.column_name
,tc.constraint_type
,ccu.constraint_schema
,ccu.constraint_name
,NULL
,NULL
,NULL
,NULL
,NULL
FROM information_schema.constraint_column_usage ccu
INNER JOIN information_schema.table_constraints tc
ON ccu.table_schema = tc.table_schema
AND ccu.table_name = tc.table_name
WHERE tc.constraint_type = 'PRIMARY KEY'
) AS KeyConstraints
ON c.table_schema = KeyConstraints.table_schema
AND c.table_name = KeyConstraints.table_name
AND c.column_name = KeyConstraints.column_name
WHERE c.column_name LIKE '%ID' OR c.column_name LIKE '%Key'
ORDER BY c.table_schema
,c.table_name
,c.column_name
;
formatting courtesy of: http://www.dpriver.com/pp/sqlformat.htm

you can use the following script in order to find all the fk,pk relationship for specific table
*DECLARE #tablename VARCHAR(100)
SET #tablename='xxxxxxx'
Select 'Referenced by FK table' AS Type, FK.TABLE_SCHEMA, FK.TABLE_NAME AS
'FK_TABLE_NAME' ,cu.COLUMN_NAME AS 'FK_ReferencingColumn',PK.TABLE_NAME AS
'PK_TABLE_NAME',
ku.COLUMN_NAME AS 'PK_ReferencedColumn'
From INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS As RC
Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As PK
On PK.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As FK
On FK.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu
ON cu.CONSTRAINT_NAME = Rc.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE ku
ON ku.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
Where
PK.TABLE_NAME = #tablename
UNION
SELECT 'Referencing PK table' AS Type, FK.TABLE_SCHEMA, FK.TABLE_NAME AS
'FK_TABLE_NAME' ,cu.COLUMN_NAME AS 'FK_ReferencingColumn',PK.TABLE_NAME AS
'PK_TABLE_NAME',
ku.COLUMN_NAME AS 'PK_ReferencedColumn'
From INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS As RC
Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As PK
On PK.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As FK
On FK.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu
ON cu.CONSTRAINT_NAME = Rc.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE ku
ON ku.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
Where
fk.TABLE_NAME = #tablename*

Related

List one to one foreign key relationships

Due to refactorings of our database, I am looking to rename some of the foreign keys in the database. The format of the names of the foreign keys that we want to apply is different depending on the cardinality of the relationship (currently we only have 1-n and 1-1).
I would like to identify all the 1-1 foreign key relationships such that I apply a different naming template to those. And I don't know how to achieve this. So far I can only list all foreign key relationships with this query:
SELECT RC.CONSTRAINT_NAME FK_Name
, KP.TABLE_NAME PK_Table
, KF.TABLE_NAME FK_Table
, KP.COLUMN_NAME PK_Column
, KF.COLUMN_NAME FK_Column
, RC.UNIQUE_CONSTRAINT_NAME PK_Name
, KP.TABLE_SCHEMA PK_Schema
, RC.MATCH_OPTION MatchOption
, RC.UPDATE_RULE UpdateRule
, RC.DELETE_RULE DeleteRule
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KF ON RC.CONSTRAINT_NAME = KF.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KP ON RC.UNIQUE_CONSTRAINT_NAME = KP.CONSTRAINT_NAME
SSMS can obviously extract this data since in the diagram viewer it displays differently the two types of relationships (with a key at both ends of the connecting line).
Any help is appreciated.
I believe I found it with a little help of a query that sqlmetal uses to generate the mappings:
select RC.CONSTRAINT_NAME FK_Name
, KP.TABLE_NAME PK_Table
, KF.TABLE_NAME FK_Table
, KP.COLUMN_NAME PK_Column
, KF.COLUMN_NAME FK_Column
, RC.UNIQUE_CONSTRAINT_NAME PK_Name
, KP.TABLE_SCHEMA PK_Schema
, RC.MATCH_OPTION MatchOption
, RC.UPDATE_RULE UpdateRule
, RC.DELETE_RULE DeleteRule
from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
join INFORMATION_SCHEMA.KEY_COLUMN_USAGE KF ON RC.CONSTRAINT_NAME = KF.CONSTRAINT_NAME
join INFORMATION_SCHEMA.KEY_COLUMN_USAGE KP ON RC.UNIQUE_CONSTRAINT_NAME = KP.CONSTRAINT_NAME
where KF.TABLE_NAME + '.' + KF.COLUMN_NAME in
(
select t.name + '.' + c.name
from sys.indexes as x,
sys.index_columns as ic,
sys.columns as c,
sys.tables as t,
sys.schemas as s
where x.object_id = ic.object_id and
x.index_id = ic.index_id and
x.object_id = c.object_id and
ic.column_id = c.column_id and
c.object_id = t.object_id and
t.schema_id = s.schema_id and
ISNULL(OBJECTPROPERTY(t.object_id, 'IsMSShipped'), 0) = 0 and
x.is_unique = 1 and
(
select count(*)
from sys.index_columns ic2
where ic2.object_id = ic.object_id and
ic2.index_id = ic.index_id and
ISNULL(OBJECTPROPERTY(ic2.object_id, 'IsMSShipped'), 0) = 0
) = 1
)

Best way to ensure bost sides of a Foreign Key are the same column type

I've got a large database and we are heading into a project to change the int identity fields to BigInt or GUID.
I've changed a few but now I'd like to see if all the foreign keys have their opposite sides changed.
Yes I can go through them all but I'm hoping to find a script of some sort that will compare both sides and alert me if the column types don't match?
I figured it out by butchering someone elses code.
SELECT C.TABLE_CATALOG [PKTABLE_QUALIFIER],
C.TABLE_SCHEMA [PKTABLE_OWNER],
C.TABLE_NAME [PKTABLE_NAME],
KCU.COLUMN_NAME [PKCOLUMN_NAME],
col.Data_Type,
C2.TABLE_CATALOG [FKTABLE_QUALIFIER],
C2.TABLE_SCHEMA [FKTABLE_OWNER],
C2.TABLE_NAME [FKTABLE_NAME],
KCU2.COLUMN_NAME [FKCOLUMN_NAME],
RC.UPDATE_RULE,
RC.DELETE_RULE,
C.CONSTRAINT_NAME [FK_NAME],
C2.CONSTRAINT_NAME [PK_NAME],
CAST(7 AS SMALLINT) [DEFERRABILITY]
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
ON C.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA
AND C.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.COLUMNS col
ON col.Table_Name = C.TABLE_NAME
AND col.Column_Name = KCU.COLUMN_NAME
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
ON C.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
AND C.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C2
ON RC.UNIQUE_CONSTRAINT_SCHEMA = C2.CONSTRAINT_SCHEMA
AND RC.UNIQUE_CONSTRAINT_NAME = C2.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU2
ON C2.CONSTRAINT_SCHEMA = KCU2.CONSTRAINT_SCHEMA
AND C2.CONSTRAINT_NAME = KCU2.CONSTRAINT_NAME
AND KCU.ORDINAL_POSITION = KCU2.ORDINAL_POSITION
INNER JOIN INFORMATION_SCHEMA.COLUMNS col2
ON col2.Table_Name = C2.TABLE_NAME
AND col2.Column_Name = KCU2.COLUMN_NAME
WHERE C.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND col.Data_Type <> col2.Data_Type

How can I find out what FOREIGN KEY constraint references a table in SQL Server?

I am trying to drop a table but getting the following message:
Msg 3726, Level 16, State 1, Line 3
Could not drop object 'dbo.UserProfile' because it is referenced by a FOREIGN KEY constraint.
Msg 2714, Level 16, State 6, Line 2
There is already an object named 'UserProfile' in the database.
I looked around with SQL Server Management Studio but I am unable to find the constraint. How can I find out the foreign key constraints?
Here it is:
SELECT
OBJECT_NAME(f.parent_object_id) TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) ColName
FROM
sys.foreign_keys AS f
INNER JOIN
sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN
sys.tables t
ON t.OBJECT_ID = fc.referenced_object_id
WHERE
OBJECT_NAME (f.referenced_object_id) = 'YourTableName'
This way, you'll get the referencing table and column name.
Edited to use sys.tables instead of generic sys.objects as per comment suggestion.
Thanks, marc_s
Another way is to check the results of
sp_help 'TableName'
(or just highlight the quoted TableName and press ALT+F1)
With time passing, I just decided to refine my answer. Below is a screenshot of the results that sp_help provides. A have used the AdventureWorksDW2012 DB for this example. There is numerous good information there, and what we are looking for is at the very end - highlighted in green:
Try this
SELECT
object_name(parent_object_id) ParentTableName,
object_name(referenced_object_id) RefTableName,
name
FROM sys.foreign_keys
WHERE parent_object_id = object_id('Tablename')
I found this answer quite simple and did the trick for what I needed: https://stackoverflow.com/a/12956348/652519
A summary from the link, use this query:
EXEC sp_fkeys 'TableName'
Quick and simple. I was able to locate all the foreign key tables, respective columns and foreign key names of 15 tables pretty quickly.
As #mdisibio noted below, here's a link to the documentation that details the different parameters that can be used: https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-fkeys-transact-sql
Here is the best way to find out Foreign Key Relationship in all Database.
exec sp_helpconstraint 'Table Name'
and one more way
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME='Table Name'
--and left(CONSTRAINT_NAME,2)='FK'(If you want single key)
I am using this script to find all details related to foreign key.
I am using INFORMATION.SCHEMA.
Below is a SQL Script:
SELECT
ccu.table_name AS SourceTable
,ccu.constraint_name AS SourceConstraint
,ccu.column_name AS SourceColumn
,kcu.table_name AS TargetTable
,kcu.column_name AS TargetColumn
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
ON ccu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
ON kcu.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME
ORDER BY ccu.table_name
if you want to go via SSMS on the object explorer window, right click on the object you want to drop, do view dependencies.
SELECT
obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
FROM
sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
ON col2.column_id = referenced_column_id
AND col2.object_id = tab2.object_id;
In SQL Server Management Studio you can just right click the table in the
object explorer and select "View Dependencies". This would give a you a
good starting point. It shows tables, views, and procedures that reference
the table.
In Object Explorer, expand the table, and expand the Keys:
--The following may give you more of what you're looking for:
create Procedure spShowRelationShips
(
#Table varchar(250) = null,
#RelatedTable varchar(250) = null
)
as
begin
if #Table is null and #RelatedTable is null
select object_name(k.constraint_object_id) ForeginKeyName,
object_name(k.Parent_Object_id) TableName,
object_name(k.referenced_object_id) RelatedTable,
c.Name RelatedColumnName,
object_name(rc.object_id) + '.' + rc.name RelatedKeyField
from sys.foreign_key_columns k
left join sys.columns c on object_name(c.object_id) = object_name(k.Parent_Object_id) and c.column_id = k.parent_column_id
left join sys.columns rc on object_name(rc.object_id) = object_name(k.referenced_object_id) and rc.column_id = k.referenced_column_id
order by 2,3
if #Table is not null and #RelatedTable is null
select object_name(k.constraint_object_id) ForeginKeyName,
object_name(k.Parent_Object_id) TableName,
object_name(k.referenced_object_id) RelatedTable,
c.Name RelatedColumnName,
object_name(rc.object_id) + '.' + rc.name RelatedKeyField
from sys.foreign_key_columns k
left join sys.columns c on object_name(c.object_id) = object_name(k.Parent_Object_id) and c.column_id = k.parent_column_id
left join sys.columns rc on object_name(rc.object_id) = object_name(k.referenced_object_id) and rc.column_id = k.referenced_column_id
where object_name(k.Parent_Object_id) =#Table
order by 2,3
if #Table is null and #RelatedTable is not null
select object_name(k.constraint_object_id) ForeginKeyName,
object_name(k.Parent_Object_id) TableName,
object_name(k.referenced_object_id) RelatedTable,
c.Name RelatedColumnName,
object_name(rc.object_id) + '.' + rc.name RelatedKeyField
from sys.foreign_key_columns k
left join sys.columns c on object_name(c.object_id) = object_name(k.Parent_Object_id) and c.column_id = k.parent_column_id
left join sys.columns rc on object_name(rc.object_id) = object_name(k.referenced_object_id) and rc.column_id = k.referenced_column_id
where object_name(k.referenced_object_id) =#RelatedTable
order by 2,3
end
You could use this query to display Foreign key constaraints:
SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
---- optional:
ORDER BY
1,2,3,4
WHERE PK.TABLE_NAME='YourTable'
Taken from http://blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/
You can also return all the information about the Foreign Keys by adapating #LittleSweetSeas answer:
SELECT
OBJECT_NAME(f.parent_object_id) ConsTable,
OBJECT_NAME (f.referenced_object_id) refTable,
COL_NAME(fc.parent_object_id,fc.parent_column_id) ColName
FROM
sys.foreign_keys AS f
INNER JOIN
sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN
sys.tables t
ON t.OBJECT_ID = fc.referenced_object_id
order by
ConsTable
The easiest way to get Primary Key and Foreign Key for a table is:
/* Get primary key and foreign key for a table */
USE DatabaseName;
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_NAME LIKE 'PK%' AND
TABLE_NAME = 'TableName'
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_NAME LIKE 'FK%' AND
TABLE_NAME = 'TableName'
try the following query.
select object_name(sfc.constraint_object_id) AS constraint_name,
OBJECT_Name(parent_object_id) AS table_name ,
ac1.name as table_column_name,
OBJECT_name(referenced_object_id) as reference_table_name,
ac2.name as reference_column_name
from sys.foreign_key_columns sfc
join sys.all_columns ac1 on (ac1.object_id=sfc.parent_object_id and ac1.column_id=sfc.parent_column_id)
join sys.all_columns ac2 on (ac2.object_id=sfc.referenced_object_id and ac2.column_id=sfc.referenced_column_id)
where sfc.parent_object_id=OBJECT_ID(<main table name>);
this will give the constraint_name, column_names which will be referring and tables which will be depending on the constraint will be there.
The procedure
_sp_help 'tbl_name'_
does give a lot of information but I find the procedures
_sp_fkeys 'tbl_name'_ and
_sp_pkeys 'tbl_name'_
easier to use, and maybe with a more future-proof result.
(And they do answer the OP perfectly)

How do I get dependent tables from sql server information schema (meta data)?

I need to know which columns in which tables are primary key as well as a foreign key, so that I know which tables are dependent (associative/many to many relation) tables. I started with a query like this but its not giving me the thing I need.
--get all tables with primary keys with pk,fk in it. --not complete
SELECT
TC.TABLE_NAME, TC.CONSTRAINT_TYPE, CCU.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC left JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
ON TC.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG AND TC.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA AND
TC.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU ON CCU.CONSTRAINT_CATALOG = TC.CONSTRAINT_CATALOG AND
CCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA AND CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
I need to change it so that I can get the table name and column name of the columns which are pk+fk. Help will be appreciated.
From here: http://wiki.lessthandot.com/index.php/Find_all_Primary_and_Foreign_Keys_In_A_Database
SELECT TC.CONSTRAINT_SCHEMA + '.'+ TC.TABLE_NAME AS PRIMARYKEYTABLE
,TC.CONSTRAINT_NAME AS PRIMARYKEY,
CU.COLUMN_NAME AS [PRIMARY COLUMN Name]
,COALESCE(RC1.CONSTRAINT_NAME,'N/A') AS FOREIGNKEY,
COALESCE(CFU.Column_Name, 'N/A') AS [FOREIGN COLUMN Name]
,CASE WHEN TC2.TABLE_NAME IS NULL THEN 'N/A'
ELSE TC.CONSTRAINT_SCHEMA + '.' + TC2.TABLE_NAME END AS FOREIGNKEYTABLE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CU ON TC.TABLE_NAME = CU.TABLE_NAME
and TC.TABLE_SCHEMA = CU.TABLE_SCHEMA and Tc.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC1 ON TC.CONSTRAINT_NAME =RC1.UNIQUE_CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC2 ON TC2.CONSTRAINT_NAME =RC1.CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CFU ON RC1.CONSTRAINT_NAME = CFU.CONSTRAINT_NAME
WHERE TC.CONSTRAINT_TYPE ='PRIMARY KEY'
ORDER BY TC.TABLE_NAME,TC.CONSTRAINT_NAME,RC1.CONSTRAINT_NAME

SQL Server 2000 - Query a Table’s Foreign Key relationships

Nearly identical to Query a Table's Foreign Key relationships, but for SQL Server 2000
For a given table 'foo', I need a query to generate a set of tables that have foreign keys that point to foo.
SELECT o2.name
FROM sysobjects o
INNER JOIN sysforeignkeys fk on o.id = fk.rkeyid
INNER JOIN sysobjects o2 on fk.fkeyid = o2.id
WHERE o.name = 'foo'
Try this T-SQL:
select col_name(fkeyid, fkey) as column_name, object_name(rkeyid) as referenced_table_name, col_name(rkeyid, rkey) as referenced_column_name from sysforeignkeys where object_name(fkeyid) = 'tableNameHere' order by constid
I've rewritten the query slightly to give you all of the other tables that rely on a particular table:
select object_name(fkeyid),
col_name(fkeyid, fkey) as column_name,
col_name(rkeyid, rkey) as referenced_column_name
from sysforeignkeys
where object_name(rkeyid) = 'tableNameHere'
order by constid
Parents and children
/* this will find out all of the foreign key references for a table*/
DECLARE #tableName varchar(128)
SET #tableName = 'tCounter'
SELECT
pt.[name] AS 'parentTable',
ct.[name] AS 'childTable',
fk.[name] AS 'fkName',
*
FROM sys.foreign_keys fk
INNER JOIN sys.tables pt
ON pt.object_ID = fk.parent_object_id
INNER JOIN sys.tables ct
ON ct.object_ID = fk.referenced_object_id
WHERE pt.name = #tableName
OR ct.name = #tableName
ORDER BY pt.name, ct.name
Start here
SELECT cons.TABLE_NAME
, cons.CONSTRAINT_NAME PK_NAME
, cols.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS cons
LEFT join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cols
ON cons.CONSTRAINT_NAME = cols.CONSTRAINT_NAME
WHERE cons.CONSTRAINT_TYPE = 'foreign KEY'
ORDER BY cons.TABLE_NAME
, cons.CONSTRAINT_NAME
, cols.COLUMN_NAME
[edit] formatting is all messed up
[edit2] no longer