Change sql table primary and foreign key numbers

Posted on 2007-08-02
Medium Priority
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
  • 2
LVL 42

Accepted Solution

dqmq earned 2000 total points
ID: 19621777
Like any other columns:

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

Author Comment

ID: 19621839
I have been working with vb.net.
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 vb.net form that requires me to do this,
LVL 42

Expert Comment

ID: 19622663
>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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

830 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