Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to update a primary key column

Author  Topic 

rasmasyean
Starting Member

22 Posts

Posted - 2009-11-03 : 11:31:42
Is there a way to directly update a row’s primary key column(s)?
Or do you have to select the row, delete the row and then insert it with the changes?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-03 : 12:46:05
There is nothing preventing you from updating the primary key. Updates are allowed. However you will run into issues if there are child rows via the foreign keys.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

rasmasyean
Starting Member

22 Posts

Posted - 2009-11-03 : 12:54:22
quote:
Originally posted by tkizer

There is nothing preventing you from updating the primary key. Updates are allowed. However you will run into issues if there are child rows via the foreign keys.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."



Why do I get an InvalidOperationException

"The property 'description' is part of the object's key information and cannot be modified."

I'm using ADO.NET data services in WCF by the way.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-03 : 12:59:48
I can't comment on ADO.NET, but SQL Server will allow the update. Sounds like your application is preventing it though.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

rasmasyean
Starting Member

22 Posts

Posted - 2009-11-03 : 15:33:46
quote:
Originally posted by tkizer

I can't comment on ADO.NET, but SQL Server will allow the update. Sounds like your application is preventing it though.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."



Do you have a link to describe how to do an update another way?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-03 : 15:38:04
I don't have any ADO.NET experience. SQL Server will allow the update:

UPDATE Table1
SET PKColumn = 'SomeValue'
WHERE PKColumn = 'SomeOtherValue'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-03 : 15:41:23
HOWEVER

I don't consider it a good practice to do so, and prevent my developers for requiring CASCADING anything

To me a key is a key

If for some reason, the great state of NJ (Mob Central) what's to change it's name..then that state ceased to exist, and there would be an end date of it's existance.....you would not want to associate our glorious past with what may happen with today's election



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-03 : 15:45:38
I completely agree with you, Brett. I do not think that primary key data should ever be updated.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

rasmasyean
Starting Member

22 Posts

Posted - 2009-11-03 : 16:06:25
quote:
Originally posted by X002548

HOWEVER

I don't consider it a good practice to do so, and prevent my developers for requiring CASCADING anything

To me a key is a key

If for some reason, the great state of NJ (Mob Central) what's to change it's name..then that state ceased to exist, and there would be an end date of it's existance.....you would not want to associate our glorious past with what may happen with today's election



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam








??? What the heck is that supposed to mean? I never cared about NJ politics to have a clue what you are talking about!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-03 : 16:09:49
Well you gotta update your profile so we know where you are from

And you don't, the mob could be running your industries

Also, here in NJ, it's not a choice of the best politian, it's the choice of the best crooked politician

The bad ones get caught, then there's a big mess, then you gotta re-elect someone....if you're not careful it could be a vicious cycle



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

rasmasyean
Starting Member

22 Posts

Posted - 2009-11-03 : 20:26:50
quote:
Originally posted by X002548

HOWEVER

I don't consider it a good practice to do so, and prevent my developers for requiring CASCADING anything

To me a key is a key

If for some reason, the great state of NJ (Mob Central) what's to change it's name..then that state ceased to exist, and there would be an end date of it's existance.....you would not want to associate our glorious past with what may happen with today's election



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam







So...what if you don't have to cascade anything?

What's wrong with updating a primary key? Data can't be permanent, can it?
Go to Top of Page

rasmasyean
Starting Member

22 Posts

Posted - 2009-11-03 : 20:31:05
quote:
Originally posted by tkizer

I completely agree with you, Brett. I do not think that primary key data should ever be updated.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."



So are you saying that the best way to do it is to Delete and Insert in a transaction with a rollback option?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-03 : 23:24:12
quote:
Originally posted by rasmasyean
So are you saying that the best way to do it is to Delete and Insert in a transaction with a rollback option?



Huh?

New Sheriff in town here in Jersey

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

rasmasyean
Starting Member

22 Posts

Posted - 2009-11-04 : 09:13:46
quote:
Originally posted by X002548

quote:
Originally posted by rasmasyean
So are you saying that the best way to do it is to Delete and Insert in a transaction with a rollback option?



Huh?

New Sheriff in town here in Jersey

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam







How else are you going to change the keyed column without an UPDATE statement?

I would guess that you have to copy the row to a buffer, modify the buffered row, DELETE the row, then INSERT the buffered row back into the table.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-11-04 : 12:26:11
quote:
Originally posted by rasmasyean

How else are you going to change the keyed column without an UPDATE statement?

I would guess that you have to copy the row to a buffer, modify the buffered row, DELETE the row, then INSERT the buffered row back into the table.

I think there is some confusion. Brett and Tara are saying that it is considered bad practice to update/change a primary key. And, depending on what you are doing, it points to a design issue. But, if you want to update the primary key you certainly can and Tara showed some code for how to do that. Now, if you are coding .NET and using datasets and such that you are changing and need to persist back the database, then it sounds like you might have an issue with your .NET code. I've not done any real .NET programming in the last 5 years so I'm pretty rusty, but I don't recall any limitation on being able to update a primary key. But, I also suspect that since the PK has changed .NET might be inclined to insert a new row instead up updating the existing row in that scenario..?
Go to Top of Page

rasmasyean
Starting Member

22 Posts

Posted - 2009-11-06 : 23:20:37
quote:
Originally posted by Lamprey

quote:
Originally posted by rasmasyean

How else are you going to change the keyed column without an UPDATE statement?

I would guess that you have to copy the row to a buffer, modify the buffered row, DELETE the row, then INSERT the buffered row back into the table.

I think there is some confusion. Brett and Tara are saying that it is considered bad practice to update/change a primary key. And, depending on what you are doing, it points to a design issue. But, if you want to update the primary key you certainly can and Tara showed some code for how to do that. Now, if you are coding .NET and using datasets and such that you are changing and need to persist back the database, then it sounds like you might have an issue with your .NET code. I've not done any real .NET programming in the last 5 years so I'm pretty rusty, but I don't recall any limitation on being able to update a primary key. But, I also suspect that since the PK has changed .NET might be inclined to insert a new row instead up updating the existing row in that scenario..?



Why is it a design issue if you have to update a primary key field? What's wrong with doing that?

Like if you have Name, Address in your PK ...and you want to change the name because it's was spelled wrong in the beginning.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-07 : 13:58:25
In that situation you'd use a surrogate key for the primary key and would not have to update the PK to make that kind of data change. That's why it's a design issue. If you have to design into your system the ability to change the PK data, then you should instead consider a surrogate key such as an identity column.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-07 : 17:50:26
Address tables where always the fly in the ointment for my stand against surrogate keys.

I could never argue the feasibility of have a pk of Street Number, Street addr line1, street addr line2, City, County, State and zip

I wouldn't win that one

It's just that surrrogates are SO overused and abused it's just silly

Hell Even M$ uses surrogates in it's catalog..causes ALL kinds of pain with users and logins

In any case

"Do as you will, Dr. Jones"

we are only passing through time



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -