FK error | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

FK error

I get the following error when trying to add a FK reference: ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint ‘FK_tblTrading_Indices_Constituents_tblTrading_Indices’. The conflict occurred in database ‘database2’, table ‘tblTrading_Indices’, column ‘Index_Code’. I checked. That FK does not exists. Just to be safe I tried dropping it and no luck. I added a UNIQUE constraint on Index_Code in the table being referenced. Both Index_Code cols in each table are same datatype. Suggestions are appreciated.
No replies? Can someone help? Thanks.
quote:Originally posted by photura I get the following error when trying to add a FK reference: ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint ‘FK_tblTrading_Indices_Constituents_tblTrading_Indices’. The conflict occurred in database ‘database2’, table ‘tblTrading_Indices’, column ‘Index_Code’. I checked. That FK does not exists. Just to be safe I tried dropping it and no luck. I added a UNIQUE constraint on Index_Code in the table being referenced. Both Index_Code cols in each table are same datatype. Suggestions are appreciated.

Are you looking at just two tables? There may be a third table involved. This will tell you the name of the table on which your constraint FK_tblTrading_Indices_Constituents_tblTrading_Indices is defined: SELECT OBJECT_NAME(s.id)
FROM dbo.sysconstraints s
WHERE s.constid =
OBJECT_ID(‘FK_tblTrading_Indices_Constituents_tblTrading_Indices’)
Also run sp_fkeys ‘parentTable’ to know all child tables Madhivanan Failing to plan is Planning to fail
<pre id="code"><font face="courier" size="2" id="code"><br /><b>sp_</b>fkeys ‘parentTable'<br /></font id="code"></pre id="code"><br />[<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />]<br /><br />–<br />Frank Kalis<br />Microsoft SQL Server MVP<br /<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br />Heute schon gebloggt?<a target="_blank" href=http://www.insidesql.de/blogs>http://www.insidesql.de/blogs</a><br />Ich unterstuetze PASS Deutschland e.V. <a target="_blank" href=http://www.sqlpass.de>http://www.sqlpass.de</a>)
I just edited the reply Frank. As usual you are faster [<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />]<br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
I really like the RSS feed. [<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />]<br /><br />–<br />Frank Kalis<br />Microsoft SQL Server MVP<br /<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br />Heute schon gebloggt?<a target="_blank" href=http://www.insidesql.de/blogs>http://www.insidesql.de/blogs</a><br />Ich unterstuetze PASS Deutschland e.V. <a target="_blank" href=http://www.sqlpass.de>http://www.sqlpass.de</a>)
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by FrankKalis</i><br /><br />I really like the RSS feed. [<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />]<br /><br />–<br />Frank Kalis<br />Microsoft SQL Server MVP<br /<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br />Heute schon gebloggt?<a target="_blank" href=http://www.insidesql.de/blogs>http://www.insidesql.de/blogs</a><br />Ich unterstuetze PASS Deutschland e.V. <a target="_blank" href=http://www.sqlpass.de>http://www.sqlpass.de</a>)<br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br />I havent tried it yet though I am willing to use it [<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />]<br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
I don’t have any idea.<br /><br />I ran your statement below and it returned a blank row.<br /><br />I also ran the sp twice with each table name as the parent table and it also returned a blank row.<br /><br />Then I tried running the FK statement again:<br /><br />ALTER TABLE tblTrading_Indices_Constituents<br />ADD CONSTRAINT FK_tblTrading_Indices_Constituents_tblTrading_Indices FOREIGN KEY (Index_Code) <br />REFERENCES tblTrading_Indices (Index_Code)<br /><br />and it still returns the same error:<br /><br />ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint ‘FK_tblTrading_Indices_Constituents_tblTrading_Indices’. The conflict occurred in database ‘dfasys2’, table ‘tblTrading_Indices’, column ‘Index_Code’.<br /><br /><br />Help! <img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ /><br /><br /><br /><br /><br /><br /><blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by Adriaan</i><br /><br />Are you looking at just two tables? There may be a third table involved.<br /><br />This will tell you the name of the table on which your constraint FK_tblTrading_Indices_Constituents_tblTrading_Indices is defined:<br /><br />SELECT OBJECT_NAME(s.id)<br />FROM dbo.sysconstraints s<br />WHERE s.constid =<br />OBJECT_ID(‘FK_tblTrading_Indices_Constituents_tblTrading_Indices’)<br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
did you check if all the data in the child is in the parent table? May the Almighty God bless us all!
www.empoweredinformation.com
]]>