<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database</title>
	<atom:link href="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/feed/" rel="self" type="application/rss+xml" />
	<link>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/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Thu, 09 Feb 2012 19:36:10 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Naveen Kumar</title>
		<link>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/#comment-201984</link>
		<dc:creator><![CDATA[Naveen Kumar]]></dc:creator>
		<pubDate>Tue, 22 Nov 2011 07:14:47 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-201984</guid>
		<description><![CDATA[Optimized Code :

SELECT b.TABLE_NAME,d.COLUMN_NAME,c.Table_name,e.column_name,a.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS a INNER JOIN
INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE b
ON a.CONSTRAINT_NAME=b.CONSTRAINT_NAME 
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE c ON
 a.UNIQUE_CONSTRAINT_NAME=c.CONSTRAINT_NAME
 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE d ON
 d.CONSTRAINT_NAME=a.CONSTRAINT_NAME
 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE e ON
 a.UNIQUE_CONSTRAINT_NAME=e.CONSTRAINT_NAME]]></description>
		<content:encoded><![CDATA[<p>Optimized Code :</p>
<p>SELECT b.TABLE_NAME,d.COLUMN_NAME,c.Table_name,e.column_name,a.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS a INNER JOIN<br />
INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE b<br />
ON a.CONSTRAINT_NAME=b.CONSTRAINT_NAME<br />
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE c ON<br />
 a.UNIQUE_CONSTRAINT_NAME=c.CONSTRAINT_NAME<br />
 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE d ON<br />
 d.CONSTRAINT_NAME=a.CONSTRAINT_NAME<br />
 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE e ON<br />
 a.UNIQUE_CONSTRAINT_NAME=e.CONSTRAINT_NAME</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ajw</title>
		<link>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/#comment-188242</link>
		<dc:creator><![CDATA[ajw]]></dc:creator>
		<pubDate>Fri, 04 Nov 2011 10:32:57 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-188242</guid>
		<description><![CDATA[slight error, this should be the correct one:
var foreignKeys =
                             from REF_CONST in datacontext.REFERENTIAL_CONSTRAINTs
                             join FK in datacontext.TABLE_CONSTRAINTs
                             on new { REF_CONST.CONSTRAINT_CATALOG, REF_CONST.CONSTRAINT_SCHEMA, REF_CONST.CONSTRAINT_NAME }
                             equals new { FK.CONSTRAINT_CATALOG, FK.CONSTRAINT_SCHEMA, FK.CONSTRAINT_NAME }
                             where FK.CONSTRAINT_TYPE == &quot;FOREIGN KEY&quot;
                             join FK2 in datacontext.TABLE_CONSTRAINTs
                             on new { cat = REF_CONST.UNIQUE_CONSTRAINT_CATALOG, sch = REF_CONST.UNIQUE_CONSTRAINT_SCHEMA, nam = REF_CONST.UNIQUE_CONSTRAINT_NAME }
                             equals new { cat = FK2.CONSTRAINT_CATALOG, sch = FK2.CONSTRAINT_SCHEMA, nam = FK2.CONSTRAINT_NAME }
                             where FK2.CONSTRAINT_TYPE == &quot;PRIMARY KEY&quot;
                             join FK_COLS in datacontext.KEY_COLUMN_USAGEs
                             on REF_CONST.CONSTRAINT_NAME equals FK_COLS.CONSTRAINT_NAME
                             join PK_COLS in datacontext.KEY_COLUMN_USAGEs
                             on FK2.CONSTRAINT_NAME equals PK_COLS.CONSTRAINT_NAME

                             select new
                             {
                                 CONSTRAINT_NAME = REF_CONST.CONSTRAINT_NAME,
                                 TABLE_CATALOG = FK.TABLE_CATALOG,
                                 TABLE_SCHEMA = FK.TABLE_SCHEMA,
                                 TABLE_NAME = FK.TABLE_NAME,
                                 COLUMN_NAME = FK_COLS.COLUMN_NAME,
                                 REFERENCED_TABLE_CATALOG = FK2.TABLE_CATALOG,
                                 REFERENCED_TABLE_SCHEMA = FK2.TABLE_SCHEMA,
                                 REFERENCED_TABLE_NAME = FK2.TABLE_NAME,
                                 REFERENCED_COLUMN_NAME = PK_COLS.COLUMN_NAME
                             };]]></description>
		<content:encoded><![CDATA[<p>slight error, this should be the correct one:<br />
var foreignKeys =<br />
                             from REF_CONST in datacontext.REFERENTIAL_CONSTRAINTs<br />
                             join FK in datacontext.TABLE_CONSTRAINTs<br />
                             on new { REF_CONST.CONSTRAINT_CATALOG, REF_CONST.CONSTRAINT_SCHEMA, REF_CONST.CONSTRAINT_NAME }<br />
                             equals new { FK.CONSTRAINT_CATALOG, FK.CONSTRAINT_SCHEMA, FK.CONSTRAINT_NAME }<br />
                             where FK.CONSTRAINT_TYPE == &#8220;FOREIGN KEY&#8221;<br />
                             join FK2 in datacontext.TABLE_CONSTRAINTs<br />
                             on new { cat = REF_CONST.UNIQUE_CONSTRAINT_CATALOG, sch = REF_CONST.UNIQUE_CONSTRAINT_SCHEMA, nam = REF_CONST.UNIQUE_CONSTRAINT_NAME }<br />
                             equals new { cat = FK2.CONSTRAINT_CATALOG, sch = FK2.CONSTRAINT_SCHEMA, nam = FK2.CONSTRAINT_NAME }<br />
                             where FK2.CONSTRAINT_TYPE == &#8220;PRIMARY KEY&#8221;<br />
                             join FK_COLS in datacontext.KEY_COLUMN_USAGEs<br />
                             on REF_CONST.CONSTRAINT_NAME equals FK_COLS.CONSTRAINT_NAME<br />
                             join PK_COLS in datacontext.KEY_COLUMN_USAGEs<br />
                             on FK2.CONSTRAINT_NAME equals PK_COLS.CONSTRAINT_NAME</p>
<p>                             select new<br />
                             {<br />
                                 CONSTRAINT_NAME = REF_CONST.CONSTRAINT_NAME,<br />
                                 TABLE_CATALOG = FK.TABLE_CATALOG,<br />
                                 TABLE_SCHEMA = FK.TABLE_SCHEMA,<br />
                                 TABLE_NAME = FK.TABLE_NAME,<br />
                                 COLUMN_NAME = FK_COLS.COLUMN_NAME,<br />
                                 REFERENCED_TABLE_CATALOG = FK2.TABLE_CATALOG,<br />
                                 REFERENCED_TABLE_SCHEMA = FK2.TABLE_SCHEMA,<br />
                                 REFERENCED_TABLE_NAME = FK2.TABLE_NAME,<br />
                                 REFERENCED_COLUMN_NAME = PK_COLS.COLUMN_NAME<br />
                             };</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ajw</title>
		<link>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/#comment-188230</link>
		<dc:creator><![CDATA[ajw]]></dc:creator>
		<pubDate>Fri, 04 Nov 2011 10:02:30 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-188230</guid>
		<description><![CDATA[The linq version is:
var foreignKeys =
                             from rc in datacontext.REFERENTIAL_CONSTRAINTs
                             join tc in datacontext.TABLE_CONSTRAINTs
                             on new { rc.CONSTRAINT_CATALOG, rc.CONSTRAINT_SCHEMA, rc.CONSTRAINT_NAME }
                             equals new { tc.CONSTRAINT_CATALOG, tc.CONSTRAINT_SCHEMA, tc.CONSTRAINT_NAME }
                             where tc.CONSTRAINT_TYPE == &quot;FOREIGN KEY&quot;
                             join tc2 in datacontext.TABLE_CONSTRAINTs
                                 //on rc.UNIQUE_CONSTRAINT_CATALOG equals tc2.CONSTRAINT_CATALOG
                             on new { cat = rc.UNIQUE_CONSTRAINT_CATALOG, sch = rc.UNIQUE_CONSTRAINT_SCHEMA, nam = rc.UNIQUE_CONSTRAINT_NAME }
                             equals new { cat = tc2.CONSTRAINT_CATALOG, sch = tc2.CONSTRAINT_SCHEMA, nam = tc2.CONSTRAINT_NAME }
                             where tc2.CONSTRAINT_TYPE == &quot;PRIMARY KEY&quot;
                             join kcu in datacontext.KEY_COLUMN_USAGEs
                             on rc.CONSTRAINT_NAME equals kcu.CONSTRAINT_NAME
                             join kcu2 in datacontext.KEY_COLUMN_USAGEs
                             on rc.CONSTRAINT_NAME equals kcu2.CONSTRAINT_NAME

                             select new
                             {
                                 CONSTRAINT_NAME = rc.CONSTRAINT_NAME,
                                 TABLE_CATALOG = tc.TABLE_CATALOG,
                                 TABLE_SCHEMA = tc.TABLE_SCHEMA,
                                 TABLE_NAME = tc.TABLE_NAME,
                                 COLUMN_NAME = kcu.COLUMN_NAME,
                                 REFERENCED_TABLE_CATALOG = tc2.TABLE_CATALOG,
                                 REFERENCED_TABLE_SCHEMA = tc2.TABLE_SCHEMA,
                                 REFERENCED_TABLE_NAME = tc2.TABLE_NAME,
                                 REFERENCED_COLUMN_NAME = kcu2.COLUMN_NAME
                             };]]></description>
		<content:encoded><![CDATA[<p>The linq version is:<br />
var foreignKeys =<br />
                             from rc in datacontext.REFERENTIAL_CONSTRAINTs<br />
                             join tc in datacontext.TABLE_CONSTRAINTs<br />
                             on new { rc.CONSTRAINT_CATALOG, rc.CONSTRAINT_SCHEMA, rc.CONSTRAINT_NAME }<br />
                             equals new { tc.CONSTRAINT_CATALOG, tc.CONSTRAINT_SCHEMA, tc.CONSTRAINT_NAME }<br />
                             where tc.CONSTRAINT_TYPE == &#8220;FOREIGN KEY&#8221;<br />
                             join tc2 in datacontext.TABLE_CONSTRAINTs<br />
                                 //on rc.UNIQUE_CONSTRAINT_CATALOG equals tc2.CONSTRAINT_CATALOG<br />
                             on new { cat = rc.UNIQUE_CONSTRAINT_CATALOG, sch = rc.UNIQUE_CONSTRAINT_SCHEMA, nam = rc.UNIQUE_CONSTRAINT_NAME }<br />
                             equals new { cat = tc2.CONSTRAINT_CATALOG, sch = tc2.CONSTRAINT_SCHEMA, nam = tc2.CONSTRAINT_NAME }<br />
                             where tc2.CONSTRAINT_TYPE == &#8220;PRIMARY KEY&#8221;<br />
                             join kcu in datacontext.KEY_COLUMN_USAGEs<br />
                             on rc.CONSTRAINT_NAME equals kcu.CONSTRAINT_NAME<br />
                             join kcu2 in datacontext.KEY_COLUMN_USAGEs<br />
                             on rc.CONSTRAINT_NAME equals kcu2.CONSTRAINT_NAME</p>
<p>                             select new<br />
                             {<br />
                                 CONSTRAINT_NAME = rc.CONSTRAINT_NAME,<br />
                                 TABLE_CATALOG = tc.TABLE_CATALOG,<br />
                                 TABLE_SCHEMA = tc.TABLE_SCHEMA,<br />
                                 TABLE_NAME = tc.TABLE_NAME,<br />
                                 COLUMN_NAME = kcu.COLUMN_NAME,<br />
                                 REFERENCED_TABLE_CATALOG = tc2.TABLE_CATALOG,<br />
                                 REFERENCED_TABLE_SCHEMA = tc2.TABLE_SCHEMA,<br />
                                 REFERENCED_TABLE_NAME = tc2.TABLE_NAME,<br />
                                 REFERENCED_COLUMN_NAME = kcu2.COLUMN_NAME<br />
                             };</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rahul</title>
		<link>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/#comment-175692</link>
		<dc:creator><![CDATA[rahul]]></dc:creator>
		<pubDate>Thu, 06 Oct 2011 12:31:41 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-175692</guid>
		<description><![CDATA[this script is correct ?



use xyz

/*

       Truncate All tables within a database 
       
 */      

Set NoCount ON

Declare @tableName varchar(200)
Declare @tableName1 varchar(200)

set @tableName=&#039;&#039;

DECLARE @intFlag INT
Declare @count INT


--Find all child tables and those which have no relations

select ROW_NUMBER() over (order by t.table_name) sno, T.table_name 
into #childtables
from INFORMATION_SCHEMA.TABLES T
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
on T.table_name=TC.table_name where (TC.constraint_Type =&#039;Foreign Key&#039;
or TC.constraint_Type is NULL) and 
T.table_name not in (&#039;dtproperties&#039;,&#039;sysconstraints&#039;,&#039;syssegments&#039;)
and Table_type=&#039;BASE TABLE&#039; and T.table_name &gt; @TableName


--Find all Parent tables 

select ROW_NUMBER() over (order by t.table_name) sno,T.table_name 
into #parenttables
from INFORMATION_SCHEMA.TABLES T
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
on T.table_name=TC.table_name where TC.constraint_Type =&#039;Primary Key&#039;
and T.table_name &#039;dtproperties&#039;and Table_type=&#039;BASE TABLE&#039; 
and T.table_name &gt; @TableName


--select * from #childtables 
--select * from #parenttables 		


SET @intFlag = 1
SET @count = ( Select COUNT(*)as countValue  from #childtables )

-- Truncate All Child tables
WHILE (@intFlag &lt;= @count)
BEGIN
Select @tableName1 = table_name from #childtables where sno = @intFlag
SET @intFlag = @intFlag + 1
Print @tableName1
Exec(&#039;Truncate table &#039;+@tableName1)
print @tableName1 + &#039; truncated successfully &#039;
END



SET @intFlag = 1
SET @count = ( Select COUNT(*)as countValue  from #parenttables)

-- Truncate All Parent tables
WHILE (@intFlag &lt;= @count)
BEGIN
Select @tableName1 = table_name from #parenttables where sno = @intFlag
SET @intFlag = @intFlag + 1
Print @tableName1
Exec(&#039;Truncate table &#039;+@tableName1)
print @tableName1 + &#039; truncated successfully &#039;

END

Set NoCount Off]]></description>
		<content:encoded><![CDATA[<p>this script is correct ?</p>
<p>use xyz</p>
<p>/*</p>
<p>       Truncate All tables within a database </p>
<p> */      </p>
<p>Set NoCount ON</p>
<p>Declare @tableName varchar(200)<br />
Declare @tableName1 varchar(200)</p>
<p>set @tableName=&#8221;</p>
<p>DECLARE @intFlag INT<br />
Declare @count INT</p>
<p>&#8211;Find all child tables and those which have no relations</p>
<p>select ROW_NUMBER() over (order by t.table_name) sno, T.table_name<br />
into #childtables<br />
from INFORMATION_SCHEMA.TABLES T<br />
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC<br />
on T.table_name=TC.table_name where (TC.constraint_Type =&#8217;Foreign Key&#8217;<br />
or TC.constraint_Type is NULL) and<br />
T.table_name not in (&#8216;dtproperties&#8217;,'sysconstraints&#8217;,'syssegments&#8217;)<br />
and Table_type=&#8217;BASE TABLE&#8217; and T.table_name &gt; @TableName</p>
<p>&#8211;Find all Parent tables </p>
<p>select ROW_NUMBER() over (order by t.table_name) sno,T.table_name<br />
into #parenttables<br />
from INFORMATION_SCHEMA.TABLES T<br />
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC<br />
on T.table_name=TC.table_name where TC.constraint_Type =&#8217;Primary Key&#8217;<br />
and T.table_name &#8216;dtproperties&#8217;and Table_type=&#8217;BASE TABLE&#8217;<br />
and T.table_name &gt; @TableName</p>
<p>&#8211;select * from #childtables<br />
&#8211;select * from #parenttables 		</p>
<p>SET @intFlag = 1<br />
SET @count = ( Select COUNT(*)as countValue  from #childtables )</p>
<p>&#8211; Truncate All Child tables<br />
WHILE (@intFlag &lt;= @count)<br />
BEGIN<br />
Select @tableName1 = table_name from #childtables where sno = @intFlag<br />
SET @intFlag = @intFlag + 1<br />
Print @tableName1<br />
Exec(&#039;Truncate table &#039;+@tableName1)<br />
print @tableName1 + &#039; truncated successfully &#039;<br />
END</p>
<p>SET @intFlag = 1<br />
SET @count = ( Select COUNT(*)as countValue  from #parenttables)</p>
<p>&#8211; Truncate All Parent tables<br />
WHILE (@intFlag &lt;= @count)<br />
BEGIN<br />
Select @tableName1 = table_name from #parenttables where sno = @intFlag<br />
SET @intFlag = @intFlag + 1<br />
Print @tableName1<br />
Exec(&#039;Truncate table &#039;+@tableName1)<br />
print @tableName1 + &#039; truncated successfully &#039;</p>
<p>END</p>
<p>Set NoCount Off</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scozzard</title>
		<link>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/#comment-173139</link>
		<dc:creator><![CDATA[Scozzard]]></dc:creator>
		<pubDate>Thu, 29 Sep 2011 08:53:02 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-173139</guid>
		<description><![CDATA[You&#039;re actually not very good at SQL are you Pinal? LOL.]]></description>
		<content:encoded><![CDATA[<p>You&#8217;re actually not very good at SQL are you Pinal? LOL.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jayaram</title>
		<link>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/#comment-164636</link>
		<dc:creator><![CDATA[Jayaram]]></dc:creator>
		<pubDate>Fri, 02 Sep 2011 05:32:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-164636</guid>
		<description><![CDATA[I have single database with three clients, I want to show the tables those related to that client instead of all tables?]]></description>
		<content:encoded><![CDATA[<p>I have single database with three clients, I want to show the tables those related to that client instead of all tables?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ravi</title>
		<link>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/#comment-158163</link>
		<dc:creator><![CDATA[Ravi]]></dc:creator>
		<pubDate>Wed, 17 Aug 2011 05:54:51 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-158163</guid>
		<description><![CDATA[Hi,

i got the answer, i think this might help you.

--- To display table does not having foreign key---


SELECT SCHEMA_NAME(t.schema_id) AS schema_name
    ,t.name AS table_name
FROM sys.tables t 
WHERE object_id NOT IN 
   (
    SELECT parent_object_id 
    FROM sys.foreign_keys 
    WHERE type_desc = &#039;Foreign_KEY_CONSTRAINT&#039; -- or type = &#039;PK&#039;
--where type = &#039;FK&#039;
    );]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>i got the answer, i think this might help you.</p>
<p>&#8212; To display table does not having foreign key&#8212;</p>
<p>SELECT SCHEMA_NAME(t.schema_id) AS schema_name<br />
    ,t.name AS table_name<br />
FROM sys.tables t<br />
WHERE object_id NOT IN<br />
   (<br />
    SELECT parent_object_id<br />
    FROM sys.foreign_keys<br />
    WHERE type_desc = &#8216;Foreign_KEY_CONSTRAINT&#8217; &#8212; or type = &#8216;PK&#8217;<br />
&#8211;where type = &#8216;FK&#8217;<br />
    );</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ravi</title>
		<link>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/#comment-157814</link>
		<dc:creator><![CDATA[Ravi]]></dc:creator>
		<pubDate>Tue, 16 Aug 2011 13:32:23 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-157814</guid>
		<description><![CDATA[hi,

I want to display the table which does not have foreign key.
Actually i have a db with 143 tables. Now i want to know the table which does not have any foreign key relationship as i have perform delete query on those tables.
Awaiting your response desperately.
Thank you]]></description>
		<content:encoded><![CDATA[<p>hi,</p>
<p>I want to display the table which does not have foreign key.<br />
Actually i have a db with 143 tables. Now i want to know the table which does not have any foreign key relationship as i have perform delete query on those tables.<br />
Awaiting your response desperately.<br />
Thank you</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marco Pagamici</title>
		<link>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/#comment-152256</link>
		<dc:creator><![CDATA[Marco Pagamici]]></dc:creator>
		<pubDate>Mon, 01 Aug 2011 09:28:13 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-152256</guid>
		<description><![CDATA[This is exactly what I was looking for. Thanks a lot, Pinal !]]></description>
		<content:encoded><![CDATA[<p>This is exactly what I was looking for. Thanks a lot, Pinal !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ferruccio Guicciardi</title>
		<link>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/#comment-140031</link>
		<dc:creator><![CDATA[Ferruccio Guicciardi]]></dc:creator>
		<pubDate>Thu, 09 Jun 2011 14:57:34 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-140031</guid>
		<description><![CDATA[Very handy ! Thanks! 

- Ferruccio Guicciardi]]></description>
		<content:encoded><![CDATA[<p>Very handy ! Thanks! </p>
<p>- Ferruccio Guicciardi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rahulrcn</title>
		<link>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/#comment-138808</link>
		<dc:creator><![CDATA[rahulrcn]]></dc:creator>
		<pubDate>Sat, 04 Jun 2011 08:02:50 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-138808</guid>
		<description><![CDATA[Thanks...  It helped me lot..]]></description>
		<content:encoded><![CDATA[<p>Thanks&#8230;  It helped me lot..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>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/#comment-134612</link>
		<dc:creator><![CDATA[Alex]]></dc:creator>
		<pubDate>Mon, 16 May 2011 14:58:17 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-134612</guid>
		<description><![CDATA[Excellent query, exactly what I needed - thanks!]]></description>
		<content:encoded><![CDATA[<p>Excellent query, exactly what I needed &#8211; thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mohamed</title>
		<link>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/#comment-118114</link>
		<dc:creator><![CDATA[mohamed]]></dc:creator>
		<pubDate>Sat, 12 Feb 2011 04:36:40 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-118114</guid>
		<description><![CDATA[Hi Pinal,

Thanks for the query. Similar kind of queries i had written but with sys.foreign_keys views instead of REFERENTIAL_CONSTRAINTS.

Is there any other query to be written to show the hierarchial relationships between the tables exists in a DB. The DB diagram would show the logical representation but most of the times we dont have the prvileges to view.

So far i have worked in Oracle, it has the level pseudo column and a hierarchial query joining with dictionary table would give you all the details. In this we can filter with the table names. So that it would be easier to analyse the tables relation as a module wise.

Can you help me in this?

Regards
Mohamed]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>Thanks for the query. Similar kind of queries i had written but with sys.foreign_keys views instead of REFERENTIAL_CONSTRAINTS.</p>
<p>Is there any other query to be written to show the hierarchial relationships between the tables exists in a DB. The DB diagram would show the logical representation but most of the times we dont have the prvileges to view.</p>
<p>So far i have worked in Oracle, it has the level pseudo column and a hierarchial query joining with dictionary table would give you all the details. In this we can filter with the table names. So that it would be easier to analyse the tables relation as a module wise.</p>
<p>Can you help me in this?</p>
<p>Regards<br />
Mohamed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mohamed</title>
		<link>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/#comment-118113</link>
		<dc:creator><![CDATA[mohamed]]></dc:creator>
		<pubDate>Sat, 12 Feb 2011 04:34:20 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-118113</guid>
		<description><![CDATA[Hi Pinal,

Is there any other query to be written to show the hierarchial relationships between the tables exists in a DB. The DB diagram would show the logical representation but most of the times we dont have the prvileges to view.

So far i have worked in Oracle, it has the level pseudo column and a hierarchial query joining with dictionary table would give you all the details. In this we can filter with the table names. So that it would be easier to analyse the tables relation as a module wise.

Can you help me in this?

Regards
Mohamed]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>Is there any other query to be written to show the hierarchial relationships between the tables exists in a DB. The DB diagram would show the logical representation but most of the times we dont have the prvileges to view.</p>
<p>So far i have worked in Oracle, it has the level pseudo column and a hierarchial query joining with dictionary table would give you all the details. In this we can filter with the table names. So that it would be easier to analyse the tables relation as a module wise.</p>
<p>Can you help me in this?</p>
<p>Regards<br />
Mohamed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brijesh Bellur</title>
		<link>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/#comment-106835</link>
		<dc:creator><![CDATA[Brijesh Bellur]]></dc:creator>
		<pubDate>Mon, 20 Dec 2010 06:42:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-106835</guid>
		<description><![CDATA[hi 
madhivanan,
i know that ,but my question is i have created one table with primary column but when i create another table with foreign key of  references to first table primary column. when i insert data it does not except it and give above error....

plz let me know were i m going wrong
thanks in advances]]></description>
		<content:encoded><![CDATA[<p>hi<br />
madhivanan,<br />
i know that ,but my question is i have created one table with primary column but when i create another table with foreign key of  references to first table primary column. when i insert data it does not except it and give above error&#8230;.</p>
<p>plz let me know were i m going wrong<br />
thanks in advances</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>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/#comment-106316</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Fri, 17 Dec 2010 15:26:13 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-106316</guid>
		<description><![CDATA[Start with

EXEC sp_help &#039;table_name&#039;]]></description>
		<content:encoded><![CDATA[<p>Start with</p>
<p>EXEC sp_help &#8216;table_name&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>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/#comment-106312</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Fri, 17 Dec 2010 15:24:57 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-106312</guid>
		<description><![CDATA[It is becuase you didn&#039;t have e_id 3 in the emp table]]></description>
		<content:encoded><![CDATA[<p>It is becuase you didn&#8217;t have e_id 3 in the emp table</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brijesh Bellur</title>
		<link>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/#comment-106053</link>
		<dc:creator><![CDATA[Brijesh Bellur]]></dc:creator>
		<pubDate>Thu, 16 Dec 2010 05:09:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-106053</guid>
		<description><![CDATA[error for the above problem:


Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint &quot;FK__t22__e_id__4FD1D5C8&quot;. The conflict occurred in database &quot;master&quot;, table &quot;dbo.t11&quot;, column &#039;e_id&#039;.
The statement has been terminated.]]></description>
		<content:encoded><![CDATA[<p>error for the above problem:</p>
<p>Msg 547, Level 16, State 0, Line 1<br />
The INSERT statement conflicted with the FOREIGN KEY constraint &#8220;FK__t22__e_id__4FD1D5C8&#8243;. The conflict occurred in database &#8220;master&#8221;, table &#8220;dbo.t11&#8243;, column &#8216;e_id&#8217;.<br />
The statement has been terminated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brijesh Bellur</title>
		<link>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/#comment-106051</link>
		<dc:creator><![CDATA[Brijesh Bellur]]></dc:creator>
		<pubDate>Thu, 16 Dec 2010 05:01:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-106051</guid>
		<description><![CDATA[hi guys....

create table emp
(e_id int primary key,
e_name varchar(255))

insert table emp values(1,&#039;aa&#039;);
insert table emp values(2,&#039;bb&#039;)

then i created new table:

create table dept
(d_id int primary key,
d_name varchar(255),
e_id int foreign key references emp(e_id))

with this st. it works well:
insert table dept values(11,&#039;computer&#039;,1);

this st. creats problem:
insert table dept values(33,&#039;marketing&#039;,3);

so plz help on this problem]]></description>
		<content:encoded><![CDATA[<p>hi guys&#8230;.</p>
<p>create table emp<br />
(e_id int primary key,<br />
e_name varchar(255))</p>
<p>insert table emp values(1,&#8217;aa&#8217;);<br />
insert table emp values(2,&#8217;bb&#8217;)</p>
<p>then i created new table:</p>
<p>create table dept<br />
(d_id int primary key,<br />
d_name varchar(255),<br />
e_id int foreign key references emp(e_id))</p>
<p>with this st. it works well:<br />
insert table dept values(11,&#8217;computer&#8217;,1);</p>
<p>this st. creats problem:<br />
insert table dept values(33,&#8217;marketing&#8217;,3);</p>
<p>so plz help on this problem</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: arun</title>
		<link>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/#comment-105885</link>
		<dc:creator><![CDATA[arun]]></dc:creator>
		<pubDate>Wed, 15 Dec 2010 09:48:57 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-105885</guid>
		<description><![CDATA[pretty good]]></description>
		<content:encoded><![CDATA[<p>pretty good</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian</title>
		<link>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/#comment-100040</link>
		<dc:creator><![CDATA[Christian]]></dc:creator>
		<pubDate>Mon, 15 Nov 2010 15:50:34 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-100040</guid>
		<description><![CDATA[thanks for all Examples!]]></description>
		<content:encoded><![CDATA[<p>thanks for all Examples!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hermann</title>
		<link>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/#comment-89940</link>
		<dc:creator><![CDATA[hermann]]></dc:creator>
		<pubDate>Tue, 28 Sep 2010 16:30:52 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-89940</guid>
		<description><![CDATA[Edit: Added constraint type

select tbl.constraint_name
	, tbl.constraint_type
	, tbl.table_schema
	, tbl.table_name
	, col.column_name
	, col3.table_schema as ref_table_name
	, col3.table_name as ref_table_name
	, col3.column_name as ref_column_name 
from information_schema.table_constraints tbl with(nolock)
	left join information_schema.constraint_column_usage col with(nolock) on tbl.constraint_name = col.constraint_name
	left join information_schema.referential_constraints col2 with(nolock) on tbl.constraint_name = col2.constraint_name
	left join information_schema.constraint_column_usage col3  with(nolock) on col2.unique_constraint_name = col3.constraint_name
order by tbl.table_name]]></description>
		<content:encoded><![CDATA[<p>Edit: Added constraint type</p>
<p>select tbl.constraint_name<br />
	, tbl.constraint_type<br />
	, tbl.table_schema<br />
	, tbl.table_name<br />
	, col.column_name<br />
	, col3.table_schema as ref_table_name<br />
	, col3.table_name as ref_table_name<br />
	, col3.column_name as ref_column_name<br />
from information_schema.table_constraints tbl with(nolock)<br />
	left join information_schema.constraint_column_usage col with(nolock) on tbl.constraint_name = col.constraint_name<br />
	left join information_schema.referential_constraints col2 with(nolock) on tbl.constraint_name = col2.constraint_name<br />
	left join information_schema.constraint_column_usage col3  with(nolock) on col2.unique_constraint_name = col3.constraint_name<br />
order by tbl.table_name</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: clement</title>
		<link>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/#comment-82978</link>
		<dc:creator><![CDATA[clement]]></dc:creator>
		<pubDate>Wed, 04 Aug 2010 10:02:50 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-82978</guid>
		<description><![CDATA[for the scripts below, where do i put my table name and colum names



select object_name(constid) FKey_Name, object_name(fkeyid) Child_Table, c1.name FKey_Col,
object_name(rkeyid) Parent_Table, c2.name Ref_KeyCol
from sysforeignkeys s
inner join syscolumns c1
on ( s.fkeyid = c1.id
and s.fkey = c1.colid )
inner join syscolumns c2
on ( s.rkeyid = c2.id
and s.rkey = c2.colid )]]></description>
		<content:encoded><![CDATA[<p>for the scripts below, where do i put my table name and colum names</p>
<p>select object_name(constid) FKey_Name, object_name(fkeyid) Child_Table, c1.name FKey_Col,<br />
object_name(rkeyid) Parent_Table, c2.name Ref_KeyCol<br />
from sysforeignkeys s<br />
inner join syscolumns c1<br />
on ( s.fkeyid = c1.id<br />
and s.fkey = c1.colid )<br />
inner join syscolumns c2<br />
on ( s.rkeyid = c2.id<br />
and s.rkey = c2.colid )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shawn</title>
		<link>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/#comment-82879</link>
		<dc:creator><![CDATA[Shawn]]></dc:creator>
		<pubDate>Tue, 03 Aug 2010 16:12:54 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-82879</guid>
		<description><![CDATA[Terrific script, helped me a lot modeling an existing DB.  Thanks for sharing!]]></description>
		<content:encoded><![CDATA[<p>Terrific script, helped me a lot modeling an existing DB.  Thanks for sharing!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alejandra</title>
		<link>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/#comment-82211</link>
		<dc:creator><![CDATA[Alejandra]]></dc:creator>
		<pubDate>Thu, 29 Jul 2010 12:08:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/2007/01/09/query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/#comment-82211</guid>
		<description><![CDATA[Thank you Dave, you&#039;re now one of my heroes !!]]></description>
		<content:encoded><![CDATA[<p>Thank you Dave, you&#8217;re now one of my heroes !!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

