General Question

lefteh's avatar

Add an ID column to an already existing MySQL table?

Asked by lefteh (9434points) February 18th, 2009

This shouldn’t be so hard. I’m blaming it on my sickness and lack of sleep.

Anyway, I inherited a MySQL table, and I need to add an ID column with consecutive ID numbers. How could I go about creating this column? I need the already-existing rows to all be assigned consecutive IDs.

Observing members: 0 Composing members: 0

3 Answers

Grisson's avatar

I’m not an expert on MySQL [@stewartwb?], but in SQLServer that works just fine. Add the column and set the identity property and it autogenerates.

What happens when you try that in MySQL?
Do you get an error? or does it just not populate?

lefteh's avatar

Well damn. That was the first thing I tried, and I got an error about duplicate keys, but I just tried it again and it worked. I must have screwed something up the first time. I knew this shouldn’t be a hard task!
Thanks, Grisson.

Grisson's avatar

Heh! It’s probably the ‘It never does it when Daddy’s watching’ phenomenon. But you’re welcome.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther