I needed to add a new, autoincrementing, primary key column 
to a table and have been struggling to figure out how to 
assign an initial, unique value to each row.

I finally accomplished my task, but feel sure there's an 
easier way.

Here is my solution:

1. Add the column:
alter table mytable add mycolumn int auto_increment;

2. Set up a user variable:
@mycounter = 0;

3. Assign the initial values by incrementing the counter:
Update mytable set mycolumn = max((@mycounter := @mycounter 
+ 1), @mycounter);

4. Finally, set the column to be the primary key:
alter table mytable set primary key mycolumn;

This seems like a roundabout way of doing things. Can any 
of you improve on it?

Thanks,

John

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to