Is there any SQL query which we run and it will update every table's primary ID
to AUTO_INCREMENT
(checked) ?
This is almost not a real question, but I am feeling in a good mood today and I will give you the resources you need, from there is open to you to understand how to handle those resources in order to accomplish your goal.
First, list all tables in your database:
There are many ways.
SHOW TABLES Is the most simple SQL statement for doing that. You can also take a look at INFORATION_SCHEMA.TABLES if you want to have more details or do some filtering or such.
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'your_database';
Then, iterate through your results, then for every iteration you will need to find the primary key for that table:
A better way is to use SHOW KEYS since you don't always have access to information_schema. The following works:
SHOW KEYS FROM table WHERE Key_name = 'PRIMARY' Column_name will contain the name of the primary key.
Now with the column name of the given table, modify the column to be auto increment with that max value.
alter table document modify column document_id int(4) auto_increment
Finally, get the max()
value of the primary_key
column and save that number in order that you can set the auto increment option of this table with this value so, the auto increment behavior will start in the right position.