简体   繁体   中英

Get all tables where a primary key is linked to a foreign key

Can I retrieve all the table names and the associated column names where a primary key is linked to a foreign key in another table?

Eg: I have a table EMPLOYEE and its primary key is linked to by a foreign key in many other tables, say DEPT , ACCOUNT and so on.

Is there a way that I can can get the table and column names of those tables where I have used employee's Primary key as a Foreign Key?

You could use the "View Dependencies" feature for the primary-table to get that info, in the SSMS. Also there some tools available that help you do that, one such in "SQLSpec". It's a pretty good tool for database documentation.

http://www.sql-server-performance.com/2007/sqlspec/

Hope this helps.

by chance today i got the same query in my mind and using google i found the following sql

SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'

you can update the LIKE '%CONSTRAINT' as per your need.

i use this in sqlserver2005

Thanks A lot Everyone... i Did it in Postgresql Where in i fired the following Query:

SELECT constraint_name 
FROM information_schema.referential_constraints 
WHERE unique_constraint_name='table_name_pkey'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM