Quertion regarding update when there relationship between two tables

  • 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.

  • 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.

  • 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
  • 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;-)

  • 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

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply