2014-11-14 54 views
0

我需要查找具有auto_increment列的mysql数据库模式中的所有表。如何查找所有auto_increment表

我需要能够将所有这些表设置为较大的auto_increment值,以免重叠将添加到(百万?)的基础数据。

我知道这是一个坏主意,但我不希望每个表的基础数据超过1000个项目。

回答

2
use information_schema; 
select table_name 
from tables 
where auto_increment is not null and table_schema=...; 

然后,您可以设置自动递增值按Change auto increment starting number?

或者,在单杆(假定Unix外壳):

mysql information_schema -e 
'select concat ("ALTER TABLE ",table_name," AUTO_INCREMENT=1000000") `-- sql` 
from tables 
where auto_increment is not null and table_schema="your-schema"; 
'|mysql your-schema 
0

这就是我使用的,所有可执行文件在mysql cli中。

use information_schema; 
SELECT concat ("ALTER TABLE `",table_name,"` AUTO_INCREMENT=",IF(DATA_TYPE='smallint',15000,IF(DATA_TYPE='tinyint',64,IF(DATA_TYPE='mediumint',4000000,IF(DATA_TYPE='int',1000000000,99999999999999)))),";") 
FROM `COLUMNS` WHERE extra LIKE '%auto_increment%' and table_schema='myschema' 
INTO OUTFILE '/tmp/auto.sql'; 
use izon; 
source /tmp/auto.sql; 

这允许每个data_type的特定大小,所以我可以增加长度到“中途”。