在postgresql、oracle、mysql中查一张表的所有分区名、主键和主键字段及非空字段
在PostgreSQL中,可以使用以下SQL查询来获取表的所有分区名、主键和主键字段以及非空字段:
SELECT
n.nspname as "Schema",
c.relname as "Table",
array_to_string(array_agg(a.attname), ', ') as "Primary Key",
array_to_string(array_agg(a.attname), ', ') as "Non-Null Fields"
FROM
pg_class c
JOIN
pg_namespace n ON n.oid = c.relnamespace
JOIN
pg_index i ON i.indrelid = c.oid
JOIN
pg_attribute a ON a.attrelid = c.oid
JOIN
pg_constraint p ON p.conrelid = c.oid AND a.attnum = ANY(p.conkey)
WHERE
c.relkind = 'r'
AND p.contype = 'p'
GROUP BY
c.relname,
n.nspname;
在Oracle中,可以使用以下SQL查询来获取表的所有分区名和分区键:
SELECT
table_name,
partitioned,
partition_count,
subpartition_count,
DECODE(partitioned, 'YES', 'Partitioned', 'No') as "Partitioned"
FROM
user_tables;
在MySQL中,分区表的信息存储在information_schema.PARTITIONS
表中,可以使用以下SQL查询来获取表的所有分区名、主键和主键字段:
SELECT
TABLE_SCHEMA,
TABLE_NAME,
PARTITION_NAME,
GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION SEPARATOR ', ') as "Primary Key"
FROM
information_schema.KEY_COLUMN_USAGE
JOIN
information_schema.TABLE_CONSTRAINTS USING (TABLE_SCHEMA, TABLE_NAME)
WHERE
CONSTRAINT_TYPE = 'PRIMARY KEY'
GROUP BY
TABLE_SCHEMA,
TABLE_NAME,
PARTITION_NAME;
请注意,这些查询可能需要根据您的具体需求进行调整。例如,在MySQL中,您可能还需要查询information_schema.COLUMNS
表来找出非空字段。对于PostgreSQL,可能还需要查询pg_attribute
以获取非空字段信息。对于Oracle,您可能需要查询ALL_TAB_COLUMNS
或USER_TAB_COLUMNS
视图来获取非空字段信息。
评论已关闭