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


Locate primary key - foreign key relations


Locate primary key - foreign key relations

Author
Message
Mohit Nayyar
Mohit Nayyar
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 94
Comments posted to this topic are about the item Locate primary key - foreign key relations

Thanks
Mohit Nayyar
http://mohitnayyar.blogspot.com/
"If I am destined to fail, then I do have a purpose in my life, To fail my destiny"
Todd Sherman-487999
Todd Sherman-487999
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 148
Howdy, I expanded this a bit, and its now my favorite query for a new database Smile

By adding the bit to the WHERE clause, it brings back information on all the key fields so its a little more generic and useful for a first glance.


SELECT base.TABLE_NAME, base.COLUMN_NAME, cons.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.COLUMNS base
LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE usage ON
(base.TABLE_SCHEMA = usage.TABLE_SCHEMA
and base.TABLE_NAME = usage.TABLE_NAME
and base.COLUMN_NAME = usage.COLUMN_NAME)
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS cons on
(base.TABLE_SCHEMA = usage.TABLE_SCHEMA
and usage.CONSTRAINT_NAME = cons.CONSTRAINT_NAME
and usage.TABLE_NAME = cons.TABLE_NAME)
WHERE base.COLUMN_NAME in
(SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_NAME LIKE '%_pk')
ORDER BY 1, 2


Prasad Bhogadi
Prasad Bhogadi
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2451 Visits: 450
Todd Sherman (11/7/2007)
Howdy, I expanded this a bit, and its now my favorite query for a new database Smile

By adding the bit to the WHERE clause, it brings back information on all the key fields so its a little more generic and useful for a first glance.


SELECT base.TABLE_NAME, base.COLUMN_NAME, cons.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.COLUMNS base
LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE usage ON
(base.TABLE_SCHEMA = usage.TABLE_SCHEMA
and base.TABLE_NAME = usage.TABLE_NAME
and base.COLUMN_NAME = usage.COLUMN_NAME)
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS cons on
(base.TABLE_SCHEMA = usage.TABLE_SCHEMA
and usage.CONSTRAINT_NAME = cons.CONSTRAINT_NAME
and usage.TABLE_NAME = cons.TABLE_NAME)
WHERE base.COLUMN_NAME in
(SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_NAME LIKE '%_pk')
ORDER BY 1, 2



Probably if you want all the constraints in the database you may want to just use something like
SELECT base.TABLE_NAME,
base.COLUMN_NAME,
cons.CONSTRAINT_TYPE
FROM
INFORMATION_SCHEMA.COLUMNS base
LEFT JOIN
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE usage ON (base.TABLE_SCHEMA = usage.TABLE_SCHEMA
and
base.TABLE_NAME = usage.TABLE_NAME
and
base.COLUMN_NAME = usage.COLUMN_NAME)
LEFT JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS cons on (base.TABLE_SCHEMA = usage.TABLE_SCHEMA and usage.CONSTRAINT_NAME = cons.CONSTRAINT_NAME
and
usage.TABLE_NAME = cons.TABLE_NAME)
WHERE
cons.CONSTRAINT_TYPE IS NOT NULL

or if you want to check for only Primary key or foreign keys you may change your where clause to include the filter on cons.CONSTRAINT_TYPE = @Constraint_type
Assuming you replace @Constraint_type with appropriate filter condition.

Prasad Bhogadi
www.inforaise.com
Iwas Bornready
Iwas Bornready
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22222 Visits: 885
Thanks for the script.
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