postgresql

PostgreSQL的information_schema支持哪些SQL查询

小樊
98
2024-08-13 13:01:41
栏目: 云计算

以下是一些可以在PostgreSQL的information_schema中执行的SQL查询:

  1. 查看所有数据库:
SELECT datname FROM information_schema.schemata;
  1. 查看所有表格:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
  1. 查看表格的列信息:
SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = 'your_table_name';
  1. 查看主键信息:
SELECT tc.table_name, kcu.column_name
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
WHERE constraint_type = 'PRIMARY KEY';
  1. 查看外键信息:
SELECT tc.table_name, kcu.column_name, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY';
  1. 查看索引信息:
SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'your_table_name';
  1. 查看视图信息:
SELECT table_name FROM information_schema.views;
  1. 查看函数信息:
SELECT routine_name FROM information_schema.routines WHERE routine_type = 'FUNCTION';

这些是一些常见的SQL查询,可以帮助您了解数据库中的元数据信息。您还可以根据需要编写自定义的查询来获取更详细的信息。

0
看了该问题的人还看了