2013-03-19 43 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」。