Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 15930

Unable to create foreign key

$
0
0

I have recreated some tables in a separate DB so that I can work on them without interuppting users.
I have created these tables using a simple
select *
into otherDB.dbo.theTable
from currentDB.dbo.tablename

This is all fine. I then went to each table and added a PK constraint on their respective ID fields.
Now, each of the tables has a column that is used when comparing to the ID of another table in joins.

So I want to make these ones FKs. I have looked at my views/functions to identify all of the relationships and am trying to add the FK constraints.

When I do I recieve the error "The ALTER TABLE statement conflicted with the FOREIGN KEY constraint xxxxx The conflict occurred in database "xx", table "dbo.xxx", column 'ID'.

I'm definately working in the correct DB and am using the tables that were created and populated using the SELECT INTO command. Other than creating the PKs on these tables I've done nothing else so I'm not sure why SQL would think I was risking ref integrity.

Any ideas? I have a fairly complicated query to run on these tables which includes joins and inline selects from UDFs - without the PK-FK relationship I fear the query could run for days!

The syntax I am using to create the FK is

ALTER TABLE table2
 ADD FOREIGN KEY (rlRecUri)
 REFERENCES table1(uri)

Table names changed to protect the innocent


Viewing all articles
Browse latest Browse all 15930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>