Adding a primary key to a MySQL table and automatically filling it

I have a bunch of huge tables that don't have primary keys. (Do not ask me why) I will add an id field for each table. It will be integer. Later I will push it to a non-zero index of unique value and a primary key.

My question is: is there a way in MySQL (5 ish). We have about a hundred tables, and most of them have more than 1 million records. By creating a new id column, is there a way to have MySQL backtrack (i.e. add a value to existing records) in the id field? I would rather do it all in MySQL. Otherwise, I will have to write a PHP script to populate the existing entries.

Thanx, Don!

+6
source share
1 answer

If you follow

ALTER TABLE table1 ADD COLUMN id INTEGER NOT NULL auto_increment PRIMARY KEY 

It will automatically populate your table with the auto_incrementing primary key.

It may take some time on a large table.

+12
source

Source: https://habr.com/ru/post/897719/


All Articles