|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 7:04 AM
Points: 157,
Visits: 833
|
|
Please find the table structures as below
USE Ticket GO CREATE TABLE Contact12 ( ContactID INT PRIMARY KEY, FirstName NVARCHAR(255), MiddleName NVARCHAR(255), LastName NVARCHAR(255) ) GO CREATE TABLE Contact13 ( ContactID INT, SalesOrderID INT PRIMARY KEY, TotalDue MONEY CONSTRAINT fk_Contact13_Sid FOREIGN KEY(ContactID) REFERENCES Contact12(ContactID) )
I want to update contactid in contact12 table
want to update contact 1 with 19978
wrote a query it gives following error of course there is conflict
The UPDATE statement conflicted with the REFERENCE constraint "fk_Contact13_Sid". The conflict occurred in database "Ticket", table "dbo.Contact13", column 'ContactID'.
how to go about for such kind of updates.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
|
|
A foreign key helps you keeping your data consistent. In your case the foreign key says that you can only insert ContactIds in table Contact13 if it already exists in column ContactId of table Contact12.
So to make your update you need the ContactId 19978 in table Contact12 first.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 8:08 AM
Points: 342,
Visits: 1,072
|
|
Make sure both 1 and 19978 exist in parent table, insert if not. Update the child table, it will now pass without error. Delete the old parent row.
I hope it is not your regular procedure, because updating primary key which is by default also a clustered index key, is a very bad idea because of fragmentation and slow operation. If it is your regular operation, you should create new IDENTITY field and make that a primary key, and reference that with FK constraint. That will not change and you will not have all sorts of problems you now have.
_____________________________________________________ XDetails Addin - for SQL Developers and DBA blog.sqlxdetails.com - Transaction log myths - debunked!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562,
Visits: 3,451
|
|
Vedran Kesegic (1/11/2013) I hope it is not your regular procedure, because updating primary key which is by default also a clustered index key, is a very bad idea because of fragmentation and slow operation. i would say , EITHER you are miskenly updating the root column (PK/FK column) OR you have bad business/database design here where you have to make changes in contactid (PK/FK )
-------Bhuvnesh---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Yesterday @ 2:28 PM
Points: 1,498,
Visits: 18,142
|
|
Change
CONSTRAINT fk_Contact13_Sid FOREIGN KEY(ContactID) REFERENCES Contact12(ContactID)
to
CONSTRAINT fk_Contact13_Sid FOREIGN KEY(ContactID) REFERENCES Contact12(ContactID) ON UPDATE CASCADE
____________________________________________________
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537
Never approach a goat from the front, a horse from the rear, or a fool from any direction.
|
|
|
|