我可以帮助您翻译相关的IT技术内容,您需要列出PostgreSQL数据库中所有表格并按层次顺序排列。也就是说,如果“用户”表格有一个指向“角色”表格的外键,则它必须在“角色”表格之后列出。以下是等同于您要求的内容:
在SQL Server中。
我尝试过这个,但没有成功:
EXEC sp_msdependencies @intrans = 1
在SQL Server中。
我尝试过这个,但没有成功:
SELECT
pt.table_name as tablename,
string_agg(DISTINCT ccu.table_name, ',') AS reftable
FROM information_schema.tables pt
LEFT JOIN information_schema.columns c
ON c.table_name = pt.table_name
LEFT JOIN information_schema.table_constraints tc
ON tc.table_name = pt.table_name AND tc.constraint_type = 'FOREIGN KEY'
LEFT JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name AND kcu.column_name = c.column_name
LEFT JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE pt.table_schema = 'public'
GROUP BY pt.table_name,pt.table_type
ORDER BY pt.table_type DESC, COUNT(TRUE) ASC;