0

I have created a table and thought timestamp will be enough to get all records ordered and unique identify-able. My mistake, I got double timestamps.

I want to alter the table to add a record id (Autoincrement) and fill up the old records with that id as well. How can I do that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ronald Wiplinger
  • 2,033
  • 3
  • 14
  • 20
  • possible duplicate of [Insert auto increment primary key to existing mysql database](http://stackoverflow.com/questions/9070764/insert-auto-increment-primary-key-to-existing-mysql-database) – Marty Mar 27 '14 at 03:58
  • If you're running on a local server, and use PhpMyAdmin, go to localhost/PhpMyAdmin, select your table. Click on Structure tab, And you'll notice an Add button on the buttom. This will prompt you to add a new column. You can type-in record id, and check the AI for it. AI means Auto-Increment. And select Unique as its Index – Logan Wayne Mar 27 '14 at 03:59
  • @LoganWayne That advice is only useful if the OP meets a number of requirements e.g. is running locally and has phpMyAdmin. Admittedly this is very common but it's still not ideal to provide information based on those assumptions. – Marty Mar 27 '14 at 04:00

1 Answers1

0

You can add PK autoincrement field using the following statement:

ALTER TABLE `table_name` ADD COLUMN `id` INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY;

The values of this field for the existing rows will be populated automatically.

curandero
  • 141
  • 3