<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WhyPad &#187; SQL</title>
	<atom:link href="http://www.whypad.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.whypad.com</link>
	<description>Tips, tricks, and hacks for life and tech...</description>
	<lastBuildDate>Tue, 06 Jul 2010 15:21:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>C# &#8211; SQL Server Foreign Key, Unique constraints</title>
		<link>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/</link>
		<comments>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/#comments</comments>
		<pubDate>Wed, 27 May 2009 21:40:24 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=682</guid>
		<description><![CDATA[I got tripped up today with a particularly nasty Join statement that worked perfectly form 90% of the queries, but was blowing up at times.  The error message was:  Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. I looked and looked at this SQL code: SELECT Process.ProcessID, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.whypad.com/wp-content/uploads/unique_key_constraint.gif"><img class="alignleft size-thumbnail wp-image-684" title="unique_key_constraint" src="http://www.whypad.com/wp-content/uploads/unique_key_constraint-150x150.gif" alt="unique_key_constraint" width="150" height="150" /></a>I got tripped up today with a particularly nasty Join statement that worked perfectly form 90% of the queries, but was blowing up at times.  The error message was: </p>
<p><span id="more-682"></span></p>
<p>Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.</p>
<p>I looked and looked at this SQL code:</p>
<pre class="brush: sql;">
SELECT Process.ProcessID, Process.Name, Process.Description, Process.ResultLogStatusID, Process.StatusTimeStamp, ResultLogStatus.Name AS ProcResult, RequirementTestLink.RequirementID

FROM RequirementTestLink

LEFT JOIN Process ON Process.ProcessID = RequirementTestLink.ProcessID

LEFT JOIN ResultLogStatus ON Process.ResultLogStatusID = ResultLogStatus.ResultLogStatusID

WHERE (RequirementTestLink.RequirementID IN (&quot; + sReqs + &quot;))

ORDER BY RequirementTestLink.RequirementID, Process.ProcessID
</pre>
<p>It looked perfectly legit to me. I figured out that there would be multiple instances of Process.ProcessID in the RequirementTestLink table. But this shouldn&#8217;t have been a problem since it would just create multiple rows for the multiple instances.</p>
<h2>Relax or turn off constraints in your DataSet.</h2>
<p>The problem was that I was using a SQL Adapter to fill a DataTable. When I made the DataTable, it got junked up with Primary Key and Foreign Key constraints on the ProcessID field. The answer had been right there in the message (see image below).  They spelled it out in the first line:  &#8220;Relax or turn off constraints in your DataSet&#8221;. It took me 30 minutes to work it out on my own&#8230;if I&#8217;d just actually read those pop ups (but so often they are so cryptic I can&#8217;t comprehend them <img src='http://www.whypad.com/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' />  ).<br />
<a href="http://www.whypad.com/wp-content/uploads/fk-constraint.gif"></a><br />
<a href="http://www.whypad.com/wp-content/uploads/relax_constraints.gif"><img class="alignleft size-full wp-image-689" title="relax_constraints" src="http://www.whypad.com/wp-content/uploads/relax_constraints.gif" alt="relax_constraints" width="450" height="228" /></a></p>
<p><a href="http://www.whypad.com/wp-content/uploads/delete_key_button.gif"><img class="alignright size-full wp-image-688" title="delete_key_button" src="http://www.whypad.com/wp-content/uploads/delete_key_button.gif" alt="delete_key_button" width="180" height="236" /></a>So, to get rid of that Primary Key:</p>
<ol>
<li>Left click on the key icon (you can see it in the image at right beside ProcessID)</li>
<li>Right click on the same key icon and select &#8216;Delete key&#8217;. </li>
</ol>
<p><a href="http://www.whypad.com/wp-content/uploads/fk-constraint.gif"><img class="alignleft size-thumbnail wp-image-683" title="fk-constraint" src="http://www.whypad.com/wp-content/uploads/fk-constraint-150x150.gif" alt="fk-constraint" width="150" height="150" /></a>If that doesn&#8217;t do it for you, then zap the Foreign Keys as well.  You can see one coming in at the top right (left picture).  Click on the line and delete it.</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=682&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
