Error 547 raised by ALTER TABLE when creating foreign key constraint

Posted on 2001-08-22
Last Modified: 2008-02-26
I have two tables as follows:

- Col1 NOT NULL (PK)
- Col2
- Col3

- Col1 NOT NULL (composite PK)
- Col2 NOT NULL (composite PK)

I am trying to create a foreign key constraint from T1.(C1,C2) to T2.(C1,C2) using the alter table statement, but get the following error:

Server: Msg 547, Level 16, State 1, Line 1
ALTER TABLE statement conflicted with COLUMN FOREIGN KEY contraint 'xxx'. The conflict occurred in database 'xxx', table 'xxx', column 'xxx'.

I have another similar case which gives a slightly different error:

Server: Msg 547, Level 16, State 1, Line 1
ALTER TABLE statement conflicted with TABLE FOREIGN KEY contraint 'xxx'. The conflict occurred in database 'xxx', table 'xxx'.

In both cases the name of the constraint given in the error message matches the one being created, and the table is the table of the relationship.

I am using SQL Server 7 with SP3.

I've found some stuff on MSDN about error 547 occurring incorrectly during an update statement, but no mention of 547 with alter table.

Any help gratefully apprecitated...
Question by:LordSauce
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

ibro earned 100 total points
ID: 6414059
Your problem probably is that Col2 column in Table1 probaly has either NULL values or values that are not in Table2. So creation of the constraint fails. Check the data in Table1 with statement

select t1.* from Table1 t1
on t1.Col1=t2.Col1 and t1.Col2=t2.Col2
where t2.Col1 is null

This will give you all rows in Table1 which can not be found in Table2 by the constraint Col1, Col2. The trick is in where clause (t2.Col1 is null)!
Hope this helps!

Author Comment

ID: 6416866
It was due to the data in the referencing tables. Thanks!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question