<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx</link><description>In the course of my work, I occasionally need to cluster a primary key that's nonclustered, or go the other way, or make some other modification to a primary key... But it's a hassle! All of the foreign keys need to be dropped, the PK needs to be dropped,</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#1160</link><pubDate>Thu, 19 Apr 2007 18:49:48 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:1160</guid><dc:creator>Fredrik svard</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I used this great SP but changed it to do the same for all of the database so that I can truncate all tables and get a empty copy of my production system. I then noticed that the create of primary keys didn't work for non dbo schemas so I changed the join with sysindexes to look lilke this &lt;/p&gt;
&lt;p&gt;	LEFT JOIN sysindexes s ON s.name = TC.CONSTRAINT_NAME&lt;/p&gt;
&lt;p&gt;		AND s.id = object_id(tc.table_schema + '.' + TC.TABLE_NAME)&lt;/p&gt;
&lt;p&gt;then it work also for others schemas.&lt;/p&gt;
&lt;p&gt;Thanks for a great stored procedure.&lt;/p&gt;
&lt;p&gt;Fredrik&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#1275</link><pubDate>Tue, 22 May 2007 18:08:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:1275</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Thanks for the info, Fredrik!&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#2885</link><pubDate>Fri, 05 Oct 2007 07:55:35 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:2885</guid><dc:creator>Ben Ootjers</dc:creator><description>&lt;p&gt;Thanks Adam, &amp;nbsp;for the great script. Save a lot of work.&lt;/p&gt;
&lt;p&gt;By renaming the procedure to sp_* and creating the procedure in the master database it is usable whenever in a database.&lt;/p&gt;
&lt;p&gt;Regards, Ben&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#2886</link><pubDate>Fri, 05 Oct 2007 07:58:40 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:2886</guid><dc:creator>Ben Ootjers</dc:creator><description>&lt;p&gt;Oops, just tested and it didn't work. Will check later, why, probably because table also exist in master db.&lt;/p&gt;
&lt;p&gt;Regards, Ben&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#10993</link><pubDate>Wed, 07 Jan 2009 12:45:36 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10993</guid><dc:creator>Jain</dc:creator><description>&lt;p&gt;I am having problem with following statement:&lt;/p&gt;
&lt;p&gt;AS (-- Create PK/UNIQUE constraints)&lt;/p&gt;
&lt;p&gt;What is worng with it? &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#10997</link><pubDate>Wed, 07 Jan 2009 16:29:31 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10997</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;In the code those are square brackets, not parens.&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#11000</link><pubDate>Wed, 07 Jan 2009 17:16:29 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11000</guid><dc:creator>Jain</dc:creator><description>&lt;p&gt;Thank you very much. &amp;nbsp;You saved me lots of time. &lt;/p&gt;
</description></item><item><title>Rejuvenated: Script Creates and Drops for Candidate Keys and Referencing Foreign Keys</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#24044</link><pubDate>Mon, 05 Apr 2010 03:19:21 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:24044</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Once upon a time it was 2004, and I wrote what I have to say was a pretty cool little script . (Yes,&lt;/p&gt;</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#26668</link><pubDate>Thu, 01 Jul 2010 14:39:32 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26668</guid><dc:creator>William</dc:creator><description>&lt;p&gt;Adam, thanks a lot, great script..&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#28766</link><pubDate>Tue, 14 Sep 2010 16:47:51 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28766</guid><dc:creator>Boxta26</dc:creator><description>&lt;p&gt;It would be good to be able to do this as a separate drop and create script and for all tables in a given db. How easy would it be to do this?&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#29077</link><pubDate>Thu, 30 Sep 2010 19:33:35 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:29077</guid><dc:creator>Harris</dc:creator><description>&lt;p&gt;Adam, this script is GOLDEN!!!!&lt;/p&gt;
&lt;p&gt;Saved me tons of BORING work.&lt;/p&gt;
&lt;p&gt;Thanks for sharing this.&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#32546</link><pubDate>Wed, 12 Jan 2011 07:30:34 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:32546</guid><dc:creator>Nirav Raval</dc:creator><description>&lt;p&gt;Adam, This is really awesome... This is what I'm looking for. Thank you very much.&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#32559</link><pubDate>Wed, 12 Jan 2011 15:32:44 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:32559</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi all,&lt;/p&gt;
&lt;p&gt;Please make sure to check out the newer version:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://sqlblog.com/blogs/adam_machanic/archive/2010/04/04/rejuvinated-script-creates-and-drops-for-candidate-keys-and-referencing-foreign-keys.aspx"&gt;http://sqlblog.com/blogs/adam_machanic/archive/2010/04/04/rejuvinated-script-creates-and-drops-for-candidate-keys-and-referencing-foreign-keys.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#38865</link><pubDate>Wed, 05 Oct 2011 16:42:34 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:38865</guid><dc:creator>shazamo</dc:creator><description>&lt;p&gt;The first SELECT did not return all constraints (PKs and FKs); in my case it returned only PKs&lt;/p&gt;
&lt;p&gt;WHERE (TC.CONSTRAINT_TYPE IN ('PRIMARY KEY', 'UNIQUE') AND TC.TABLE_NAME = @TableName)&lt;/p&gt;
&lt;p&gt;		OR (TC.CONSTRAINT_TYPE = 'FOREIGN KEY' AND FC.TABLE_NAME = @TableName)&lt;/p&gt;
&lt;p&gt;when I changed FC.TABLE_NAME = @TableName to TC.TABLE_NAME = @TableName, then it worked and gave me all PKs and FKs&lt;/p&gt;
&lt;p&gt;so the catch here is using FC. or TC., and because the query does LEFT JOIN with TABLE_CONSTRAINTS, TABLE_NAME here may or may not exist&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#38886</link><pubDate>Thu, 06 Oct 2011 13:45:02 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:38886</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;shazamo,&lt;/p&gt;
&lt;p&gt;Please see the prior comment: there is a much newer version available that handles things in better ways.&lt;/p&gt;
&lt;p&gt;--Adam&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#39919</link><pubDate>Mon, 21 Nov 2011 22:58:53 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:39919</guid><dc:creator>Naomi</dc:creator><description>&lt;p&gt;Adam,&lt;/p&gt;
&lt;p&gt;Do you know how to script all type DEFAULT constraints which are created with the sp_binddefault? Our database uses such constraints (what a pity!) and I can not figure out how to script them for existing table so I can reproduce it.&lt;/p&gt;
&lt;p&gt;Thanks in advance.&lt;/p&gt;
</description></item><item><title>re: Script out PKs/UNIQUE constraints and referencing FKs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx#39925</link><pubDate>Tue, 22 Nov 2011 03:06:08 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:39925</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Naomi,&lt;/p&gt;
&lt;p&gt;Apologies, but I don't -- can't say that I've seen anyone use that feature. Those constraints can't be found in sys.default_constraints?&lt;/p&gt;
</description></item></channel></rss>