Solved

Error 547 raised by ALTER TABLE when creating foreign key constraint

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

Table1
- Col1 NOT NULL (PK)
- Col2
- Col3

Table2
- 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...
0
Comment
Question by:LordSauce
2 Comments
 
LVL 3

Accepted Solution

by:
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
LEFT OUTER JOIN Table2 t2
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!
0
 

Author Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now