﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / ERROR SETTING FK / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 13:02:58 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: ERROR SETTING FK</title><link>http://www.sqlservercentral.com/Forums/Topic1412046-391-1.aspx</link><description>Thanks opc.three I got if fixed thanks to you.Lon</description><pubDate>Mon, 28 Jan 2013 09:48:51 GMT</pubDate><dc:creator>lonhanner</dc:creator></item><item><title>RE: ERROR SETTING FK</title><link>http://www.sqlservercentral.com/Forums/Topic1412046-391-1.aspx</link><description>You have some orphaned data that you'll need to cleanup before adding the constraint.[code="sql"]SELECT  RIDER_NOFROM    TRIP_DETAILSWHERE   RIDER_NO NOT IN (SELECT RIDER_NO                         FROM   RIDERS);[/code]Or you can do this incrementally by adding the constraint WITH NOCHECK to in effect prevent the creation of new orphaned rows but tolerate the existing orphans, then cleaning up the bad data as time allows, and finally once all orphans are removed or resolved going back and CHECKing the constraint:[code="sql"]-- 1. add constraint but do not check existing rowsALTER TABLE TRIP_DETAILS [b]WITH NOCHECK[/b]ADD CONSTRAINT fk_TRIP_DETAILS_RIDER_NOFOREIGN KEY (RIDER_NO)REFERENCES RIDERS(RIDER_NO);-- 2. cleanup orphans as time allows...-- 3. check the constraint to let the database engine record that it can rely on the existing data conforming to the constraintALTER TABLE TRIP_DETAILS CHECK CONSTRAINT fk_TRIP_DETAILS_RIDER_NO;[/code]</description><pubDate>Sat, 26 Jan 2013 12:26:25 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: ERROR SETTING FK</title><link>http://www.sqlservercentral.com/Forums/Topic1412046-391-1.aspx</link><description>There's a RiderNo in trip details that does not have a matching entry in Riders</description><pubDate>Sat, 26 Jan 2013 10:43:55 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>ERROR SETTING FK</title><link>http://www.sqlservercentral.com/Forums/Topic1412046-391-1.aspx</link><description>I receive the following error when trying to set FKThe ALTER TABLE statement conflicted with the FOREIGN KEY constraint "fk_TRIP_DETAILS_RIDER_NO". The conflict occurred in database "DCTSSQL ", table "dbo.RIDERS", column 'RIDER_NO'.ALTER TABLE TRIP_DETAILSADD CONSTRAINT fk_TRIP_DETAILS_RIDER_NOFOREIGN KEY (RIDER_NO)REFERENCES RIDERS(RIDER_NO);</description><pubDate>Sat, 26 Jan 2013 10:18:46 GMT</pubDate><dc:creator>lonhanner</dc:creator></item></channel></rss>