(1)查询当前表被哪些子表所依赖
SELECT x.table_name, x.column_name FROM information_schema.key_column_usage x #key_column_usage是系统列信息表
INNER JOIN
(SELECT t.relname, a.conname
FROM pg_constraint a #pg_constraint是系统所有约束表
INNER JOIN pg_class ft ON ft.oid = a.confrelid
INNER JOIN pg_class t ON t.oid = a.conrelid WHERE a.contype = 'f'
AND a.confrelid =
(select e.oid from pg_class e where e.relname = 'auth_user') #'auth_user'是所需要的查询表
) tp
ON (x.table_name = tp.relname AND x.constraint_name = tp.conname);
查询结果
(2)查询当前表依赖哪些表
SELECT tc.constraint_name,tc.table_name, kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name,
tc.is_deferrable,tc.initially_deferred
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' AND tc.table_name='auth_user_user_permissions'; # 是所需要查询的表