Unix Technical Forum

Determining if foreign key constraint exists

This is a discussion on Determining if foreign key constraint exists within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, I'm working with a number of databases. I need a way to determine if a foreign key with ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:34 AM
Foehammer
 
Posts: n/a
Default Determining if foreign key constraint exists

Hello,
I'm working with a number of databases. I need a way to determine if a
foreign key with given name exists and determine what tables and
fields are linked to it. Any ideas?

I know I can issue the following statement to get a list of the fields
that are constrained by a foreign key constraint.

SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

How can I get a list of fields pointed to by the constraints?

Thanks,
Will
Foehammer@hotmail.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:34 AM
John Gilson
 
Posts: n/a
Default Re: Determining if foreign key constraint exists

"Foehammer" <foehammer@hotmail.com> wrote in message
news:27d68359.0407110735.9b75a48@posting.google.co m...
> Hello,
> I'm working with a number of databases. I need a way to determine if a
> foreign key with given name exists and determine what tables and
> fields are linked to it. Any ideas?
>
> I know I can issue the following statement to get a list of the fields
> that are constrained by a foreign key constraint.
>
> SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
>
> How can I get a list of fields pointed to by the constraints?
>
> Thanks,
> Will
> Foehammer@hotmail.com


CREATE VIEW ForeignKeyReferences
(fk_name, fk_table_name, fk_column_name,
key_name, key_table_name, key_column_name, ordinal_position)
AS
SELECT FK.constraint_name, FK.table_name, FKU.column_name,
UK.constraint_name, UK.table_name, UKU.column_name,
FKU.ordinal_position
FROM Information_Schema.Table_Constraints AS FK
INNER JOIN
Information_Schema.Key_Column_Usage AS FKU
ON FK.constraint_type = 'FOREIGN KEY' AND
FKU.constraint_name = FK.constraint_name
INNER JOIN
Information_Schema.Referential_Constraints AS RC
ON RC.constraint_name = FK.constraint_name
INNER JOIN
Information_Schema.Table_Constraints AS UK
ON UK.constraint_name = RC.unique_constraint_name
INNER JOIN
Information_Schema.Key_Column_Usage AS UKU
ON UKU.constraint_name = UK.constraint_name AND
UKU.ordinal_position =FKU.ordinal_position

SELECT *
FROM ForeignKeyReferences
ORDER BY fk_table_name, fk_name, ordinal_position

--
JAG


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:39 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com