Change sql table primary and foreign key numbers

Posted on 2007-08-02
Last Modified: 2013-11-26
Visual Basic 2005
SQL Server 2005

Can someone help me with a way to change the primary key, foreign key and a sort column numbers in a sql table.

Question by:dave_sky
    LVL 42

    Accepted Solution

    Like any other columns:

    UPDATE YourTable
    SET PKColumn=NewPKValue
    , FKColumn=NewFKValue
    WHERE PKColumn=OldPKValue

    Author Comment

    I have been working with
    I don't know enough about sql server to really understand your comment.

    Here is an example of the numbers I am trying to change :
    Primary key numbers - 1,2,3,4,5,6,7,8,9,10
    Foreigh key numbers - varies from numbers 1 to 30 for each primary key number.
    Sort column numbers - varies from numbers 1 to 30 for each primary key number.

    Can the sample code you posted do something like this?

    I have a row insert, delete code change in a form that requires me to do this,
    LVL 42

    Expert Comment

    >Can the sample code you posted do something like this?

    Yes, that's an SQL template for changing an SQL update.   Can you just copy/paste it verbatim into a VB.NET program? No.

    Beyond that, I'm not too sure what your question is.  I will say that changing a primary key is not advised--or rather, if you need to change that column it's not a good choice for the primary key.  Also, changing foreign keys is tricky, precisely  because the related primary key must change in lock-step.    



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now