溫馨提示×

PostgreSQL的information_schema支持哪些SQL查詢

小樊
88
2024-08-13 13:01:41
欄目: 云計(jì)算

以下是一些可以在PostgreSQL的information_schema中執(zhí)行的SQL查詢:

  1. 查看所有數(shù)據(jù)庫:
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. 查看函數(shù)信息:
SELECT routine_name FROM information_schema.routines WHERE routine_type = 'FUNCTION';

這些是一些常見的SQL查詢,可以幫助您了解數(shù)據(jù)庫中的元數(shù)據(jù)信息。您還可以根據(jù)需要編寫自定義的查詢來獲取更詳細(xì)的信息。

0