繁体   English   中英

在PostgreSQL中,是否可以查询给定数据库中的所有表?

[英]In PostgreSQL, is it possible to query all the tables in a given database?

在PostgreSQL中,是否可以查询给定数据库中的所有表以获得满足某些条件的记录?

Select [dbname "db"] *
From [all tables in database "db"]
Where [some condition on records]

[]中的内容是任意的,直到它们通常意味着什么为止。

我最好的选择是,您要返回特定数据库中满足特定条件的每个表的所有行。 这变得棘手,因为:

  1. 您要查询其条件的表必须存在于您要查询的每个表中。
  2. 此外,由于在一个模式/数据库中的许多表可能具有不同的列,因此您不能在同一结果集中将它们全部返回。
  3. 没有可以发送到数据库的“一个” sql语句来执行此操作。 您将至少查询两次。 一次获取表列表,再一次查询这些表。

考虑具有两个表都带有列F1的数据库/架构。 我们希望该模式中F1=1的表中的所有记录。 所以:

# CREATE TABLE public.t2 (f1 int, f2 varchar(20), f3 date);
    CREATE TABLE
# CREATE TABLE public.t1 (f1 int, f4 varchar(40), f5 varchar(10), f6 int);
    CREATE TABLE
# SELECT * FROM information_schema.tables WHERE table_schema = 'public';
     table_catalog | table_schema | table_name | table_type | self_referencing_column_name | reference_generation | user_defined_type_catalog | user_defined_type_schema | user_defined_type_name | is_insertable_into | is_typed | commit_action 
    ---------------+--------------+------------+------------+------------------------------+----------------------+---------------------------+--------------------------+------------------------+--------------------+----------+---------------
     username      | public       | t2         | BASE TABLE |                              |                      |                           |                          |                        | YES                | NO       | 
     username      | public       | t1         | BASE TABLE |                              |                      |                           |                          |                        | YES                | NO       | 
    (2 rows)

# SELECT 'SELECT * FROM public.' || table_name || ' WHERE f1=1;' FROM 
    information_schema.tables WHERE table_schema = 'public';
                  ?column?               
    -------------------------------------
     SELECT * FROM public.t2 WHERE f1=1;
     SELECT * FROM public.t1 WHERE f1=1;
    (2 rows)

最后一条SQL生成两条记录(对于架构中的每个表一条记录),向其查询条件为真的记录。 现在,您可以分别执行每个操作以获取结果(在两个结果集中)。

# SELECT * FROM public.t2 WHERE f1=1;SELECT * FROM public.t1 WHERE f1=1;
     f1 | f2 | f3 
    ----+----+----
    (0 rows)

     f1 | f4 | f5 | f6 
    ----+----+----+----
    (0 rows)

如果架构中的每个表都完全相同(相同的列数,列的顺序和数据类型),则可以查询information_Schema并构建UNION查询,然后分别执行:

# CREATE TABLE public.t1 (f1 int, f2 varchar(20), f3 date);
    CREATE TABLE
# CREATE TABLE public.t2 (f1 int, f2 varchar(20), f3 date);
    CREATE TABLE
# INSERT INTO public.t1 VALUES (1, 'test', '2019-04-22'),(2, 'test2', '2019-04-22');
    INSERT 0 2
# INSERT INTO public.t2 VALUES (1, 'testtest', '2019-04-21'),(2, 'testest2', '2019-04-20');
    INSERT 0 2
# SELECT 'SELECT * FROM public.' || table_name || ' WHERE f1=1' || CASE WHEN LEAD(table_name) OVER (ORDER BY table_name) IS NOT NULL THEN ' UNION ALL' ELSE ';' END FROM information_schema.tables WHERE table_schema = 'public';
                       ?column?                   
    ----------------------------------------------
     SELECT * FROM public.t1 WHERE f1=1 UNION ALL
     SELECT * FROM public.t2 WHERE f1=1;
    (2 rows)
# SELECT * FROM public.t1 WHERE f1=1 UNION ALL
-#  SELECT * FROM public.t2 WHERE f1=1;
     f1 |    f2    |     f3     
    ----+----------+------------
      1 | test     | 2019-04-22
      1 | testtest | 2019-04-21
    (2 rows)

您会看到这两个选项都不是很好,因为您必须动态编写SQL,然后单独执行它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM