Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a smart way to append a number to an PK identity column in a Relational database w/o total catastrophe?

It's far from the ideal situation, but I need to fix a database by appending the number "1" to the PK Identiy column which has FK relations to four other tables. I'm basically making a four digit number a five digit number. I need to maintain the relations. I could store the number in a var, do a Set query and append the 1, and do that for each table...

Is there a better way of doing this?

like image 229
proggrock Avatar asked Dec 11 '25 18:12

proggrock


2 Answers

You say you are using an identity data type for your primary key so before you update the numbers you will have to SET IDENTITY_INSERT ON (documentation here) and then turn it off again after the update.

As long as you have cascading updates set for your relations the other tables should be updated automatically.

EDIT: As it's not possible to change an identity value I guess you have to export the data, set the new identity values (+10000) and then import your data again.

Anyone have a better suggestion...

like image 161
Tony Avatar answered Dec 14 '25 09:12

Tony


Consider adding another field to the PK instead of extending the length of the PK field. Your new field will have to cascade to the related tables, like a field length increase would, but you get to retain your original PK values.

like image 42
Beth Avatar answered Dec 14 '25 08:12

Beth