简体   繁体   English

如何将自动增量添加到具有主键集的所有数据库表

[英]How to add Auto-Increment to all database Tables with Primary key set

I use我用

ALTER TABLE `tablename` DROP PRIMARY KEY;
ALTER TABLE `tablename` MODIFY `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY;

To add Auto increment to a specific table with column name ID , However, what I want is to:要将自动增量添加到具有列名ID的特定表中,但是,我想要的是:

1 - Add Auto-Increment to all tables in a particular database (even if there are 1000 tables) with column name ID . 1 - 将自动增量添加到特定数据库中的所有表(即使有 1000 个表),列名称为ID

2 - Check if primary key is present in each table name with column name ID to be able to alter it. 2 - 检查每个表名中是否存在主键以及列名ID以便能够更改它。

The reason is I have a database with over 2,000 tables and after upgrading my xammp version, it seems to remove the auto-increment but retains primary key value.原因是我有一个包含超过 2,000 个表的数据库,并且在升级我的 xammp 版本后,它似乎删除了自动增量但保留了主键值。

We can use a cursor to loop through all tables which has an ID column in a specific database.我们可以使用 cursor 循环遍历特定数据库中具有ID列的所有表。 Then use prepared statement to execute necessary alter table statements.然后使用prepared statement执行必要alter table语句。 Everything is done in a procedure.一切都在一个程序中完成。 It's written and tested in workbench.它是在工作台中编写和测试的。 Please try it:请试一试:

create database testdatabase;
use testdatabase;
drop table if exists t1;
drop table if exists t2;
drop table if exists t3;
create table t1 (id int,num int);
create table t2 (id int primary key, num int);
create table t3 (id int primary key auto_increment, num int);

delimiter //
drop procedure if exists add_pk_ai//
create procedure add_pk_ai(db_name varchar(20),col_name varchar(20),dt_type varchar(20)) -- in parameters include the database name, column name ,and the data type of the column
begin
declare tb_name varchar(20);
declare col_key varchar(20);
declare col_extra varchar(20);
declare done bool default false;
declare c cursor for select table_name,column_key,extra 
from information_schema.columns 
where table_schema=db_name and column_name=col_name and data_type=dt_type;
declare continue handler for not found set done=true;

open c;
lp:loop
fetch c into tb_name,col_key,col_extra;
if done=true then
leave lp;
end if;

if col_key!='PRI'  then
set @stmt=concat('alter table ',db_name,'.',tb_name,' add primary key (',col_name,');');
prepare stmt1 from @stmt;
execute stmt1;
deallocate prepare stmt1;
end if;

if col_extra!='auto_increment' then
set @stmt=concat('alter table ',db_name,'.',tb_name,' modify ',col_name,' ',dt_type,' auto_increment ;');
prepare stmt2 from @stmt;
execute stmt2;
deallocate prepare stmt2;
end if;

end loop lp;

end// 
delimiter ;
-- let's test the procedure
call add_pk_ai('testdatabase','id','int'); 

desc t1;
-- result set:
# Field, Type, Null, Key, Default, Extra
id, int(11), NO, PRI, , auto_increment
num, int(11), YES, , , 

desc t2;
-- result set:
# Field, Type, Null, Key, Default, Extra
id, int(11), NO, PRI, , auto_increment
num, int(11), YES, , , 

desc t3;
-- result set:
# Field, Type, Null, Key, Default, Extra
id, int(11), NO, PRI, , auto_increment
num, int(11), YES, , , 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM