【发布时间】:2015-01-12 02:12:36
【问题描述】:
我需要在 MySQL 数据库模式中找到所有具有 AUTO_INCREMENT 列的表。
我需要能够将所有这些表设置为较大的 AUTO_INCREMENT 值,以免与将添加到(百万?)的基础数据重叠。
我知道这是个坏主意,但我不希望每个表的基础数据超过 1000 项。
【问题讨论】:
标签: mysql auto-increment
我需要在 MySQL 数据库模式中找到所有具有 AUTO_INCREMENT 列的表。
我需要能够将所有这些表设置为较大的 AUTO_INCREMENT 值,以免与将添加到(百万?)的基础数据重叠。
我知道这是个坏主意,但我不希望每个表的基础数据超过 1000 项。
【问题讨论】:
标签: mysql auto-increment
use information_schema;
select table_name
from tables
where auto_increment is not null and table_schema=...;
然后您可以按照Change auto increment starting number? 设置自动增量值
或者,一次性(假设 Unix shell):
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
【讨论】:
部分回答...this will find all the columns with auto_increment
SELECT
*
FROM
`information_schema`.`COLUMNS`
WHERE
`EXTRA` = 'auto_increment' AND
`TABLE_SCHEMA` = 'foochoo'
【讨论】:
这是我使用的,所有都可以在 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;
这允许每个数据类型有一个特定的大小,所以我可以将长度增加到“一半”。
【讨论】: