zoukankan      html  css  js  c++  java
  • PostgreSQ 查询表外键依赖关系

    (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';        # 是所需要查询的表
    

  • 相关阅读:
    Python标准库--abc模块
    Python标准库--argparse模块
    爬虫基础
    jQuery基础
    前端基础之javascript
    web前端之HTML
    MySQL常用语句
    多线程和多进程
    socket网络编程
    异常处理
  • 原文地址:https://www.cnblogs.com/fyusac/p/14554008.html
Copyright © 2011-2022 走看看