Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Quertion regarding update when there relationship between two tables Expand / Collapse
Author
Message
Posted Friday, January 11, 2013 6:40 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 8:24 AM
Points: 194, Visits: 1,087
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.
Post #1405973
Posted Friday, January 11, 2013 7:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 3:31 AM
Points: 194, Visits: 717
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.
Post #1406008
Posted Friday, January 11, 2013 8:29 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 2:57 AM
Points: 369, Visits: 1,208
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.


_____________________________________________________
Microsoft Certified Master: SQL Server 2008
XDetails Addin - for SQL Developers
blog.sqlxdetails.com - Transaction log myths
Post #1406305
Posted Monday, January 14, 2013 2:23 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, July 14, 2014 4:48 AM
Points: 2,834, Visits: 3,950
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----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1406605
Posted Monday, January 14, 2013 5:21 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 6:13 AM
Points: 1,694, Visits: 19,550
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.
Post #1406668
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse