Sunday, January 23, 2022

[FIXED] How do you change the primary key, when rows have the same value?

Issue

I added a new row to my Payable table named id (int 5) and want to change the primary key from stock (int 5) to id. However when I created the id row sql inserted a 0 value for each row. How do I alter the table, giving each row an auto incremented value starting at 10001 and make that the primary key?


Solution

Something like this (since MySQL allows in-line assignments):

SET @var = 10000;
UPDATE Payable
SET id = (@var := @var + 1);

SQL Fiddle



Answered By - PM 77-1

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.