2013-03-19 46 views

回答

5

您可以在一个数据库中提取从information_schema.columns

select distinct table_name 
from information_schema.columns 
where table_schema = 'DATABASENAME' 
     and table_name not in (select table_name 
          from information_schema.columns 
          where table_schema = 'DATABASENAME' 
            and column_key = 'PRI' 
            and data_type = 'int' 
            and extra = 'auto_increment') 

这看起来对所有表信息具有auto_increment列,然后返回剩余的表。这也可以正确检测带复合键的表格。

+1

我也会添加'data_type ='int',因为你不想列出PK字段是字符数据。 – 2013-03-19 13:56:57

1

你可以在表information_schema.columns中找到那种信息。如果自动递增,则列column_key将为PRI,并且列extra将包含auto_increment

SELECT 
    table_schema, 
    table_name, 
    column_name 
FROM 
    information_schema.columns 
WHERE 
    column_key = 'PRI' 
    AND extra <> 'auto_increment' 
    AND data_type = 'int' 

this SQL Fiddle你可以看到,在样品表中有“优先级”,并在相应的栏目“AUTO_INCREMENT”。