1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-- 查询数据库中所有表名和表的主键
select
table_name,
column_name
from information_schema.key_column_usage
where constraint_name = 'primary'
and table_name in (
select
table_name
from information_schema.tables
where table_schema = 'testdb'
order by table_name)
and table_schema = 'testdb';
-- 查询数据库中没有主键的表
SELECT
a.TABLE_SCHEMA,
a.TABLE_NAME
FROM
(
SELECT
TABLE_SCHEMA,
TABLE_NAME
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA NOT IN
( 'mysql', 'information_schema', 'performance_schema', 'sys', 'sysdb' )) AS a
LEFT JOIN (
SELECT
TABLE_SCHEMA,
TABLE_NAME
FROM
information_schema.TABLE_CONSTRAINTS
WHERE
CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TABLE_SCHEMA NOT IN
( 'mysql', 'information_schema', 'performance_schema', 'sys', 'sysdb' )) AS b
ON a.TABLE_SCHEMA = b.TABLE_SCHEMA
AND a.TABLE_NAME = b.TABLE_NAME
WHERE
b.TABLE_NAME IS NULL;
Post
Cancel
MySQL查询主键相关
This post is licensed under
CC BY 4.0
by the author.