Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 918539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:23:38+00:00 2026-05-15T18:23:38+00:00

I want to change the primary key value for one row in a table

  • 0

I want to change the primary key value for one row in a table that has relations with other tables:

For example

Table Person { Id, Name, +50 fields }
Table Address { Id, City, +10 fields }
Table Person2Address { Id, PersonId, AddressId }

I want to change Person.Id and Person2Address.PersonId

I try something like:

BEGIN TRANSACTION    
  UPDATE Pers SET Id = NewId WHERE Id = OldId
  UPDATE Person2Address SET PersonId = NewId WHERE PersonId = OldId
COMMIT TRANSACTION

But of course it provides conflicts 🙂

How can I temporary suppress foreign key constraints or is there a better way to change Id for person?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:23:38+00:00Added an answer on May 15, 2026 at 6:23 pm

    First off, changing the primary key value is never a good idea. Your main focus should be to try and avoid that by all means.

    If you cannot eliminate the need to update the primary key value, then your best bet would be to define the foreign key relationship between those two tables to be using ON UPDATE CASCADE, so that any changes to the main table’s primary key will be automatically cascaded down to the child table.

    To do this, drop your existing foreign key relationship and then add:

     ALTER TABLE dbo.Person2Address
       ADD CONSTRAINT FK_Person2Address_Person
       FOREIGN KEY (PersonId) REFERENCES dbo.Person(Id)
         ON UPDATE CASCADE
    

    That should then automatically update the Person2Address table’s PersonId value if the Id on the person changes.

    Now you should be able to just call

    UPDATE dbo.Person SET Id = NewId WHERE Id = OldId
    

    and that should be all there is!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having a databse with some customer information (customer_nr(primary key), name, address and so
I'm creating a database and I want to use a different naming convention other
I have an SQLite table that must remain unchanged until a specific point. Any
I have a view like this: CREATE VIEW MyView AS SELECT Column FROM Table
I am using NHibernate to map a class to a database table. The Part
I want to globally replace all instances of my site url in the Wordpress
I'm working with an MLS real estate listing provider (RETS). Every 48 hours we
I'm developing a new application which is based on a legacy database. The old
I'm having a small question concerning inserting and updating a database in C#. I'm
First of all, I'm aware of why it doesn't appear in the Add References

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.