如何查找没有自动增量的所有主键

Rpj*_*Rpj 2 mysql

如何查找数据库中所有表中没有自动增量标识符的所有主键.我们有大量的表,并希望识别主键上没有自动增量标识符的所有表.

Ola*_*che 6

您可以从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')
Run Code Online (Sandbox Code Playgroud)

这将查找具有auto_increment列的一个数据库中的所有表,然后返回其余表.这也正确地检测具有复合键的表.