6

给定这样的结构:

CREATE TABLE reference_table (
  reference_table_key numeric NOT NULL,
  reference_value numeric,
  CONSTRAINT reference_table_pk PRIMARY KEY (reference_table_key)
);

CREATE TABLE other_table (
  other_table_key numeric NOT NULL,
  reference_table_key numeric,
  CONSTRAINT other_table_pk PRIMARY KEY (other_table_key),
  ONSTRAINT other_table_reference_fk FOREIGN KEY (reference_table_key)
      REFERENCES reference_table (reference_table_key) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL
);

CREATE TABLE another_table (
  another_table_key numeric NOT NULL,
  do_stuff_key numeric,
  CONSTRAINT another_table_pk PRIMARY KEY (another_table_key),
  ONSTRAINT another_table_reference_fk FOREIGN KEY (do_stuff_key)
      REFERENCES reference_table (reference_table_key) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL
);

--there are 50-60 tables which have similar foreign key references to reference_table

我想编写一个查询,告诉我 other_table 和 another_table 中的主键,以及可能更多 reference_value 为 NULL 的表。

在伪代码中:

SELECT table_name, table_primary_key, table_fk_column_name
FROM ?????? some PG table ???????, reference_table
WHERE reference_table.reference_value IS NULL;

结果看起来像:

table_name | table_primary_key | table_fk_column_name | reference_table_pk
---------------------------------------------------------------------------
other_table   | 2                |  reference_table_key | 7
other_table   | 4                |  reference_table_key | 56
other_table   | 45               |  reference_table_key | 454
other_table   | 65765            |  reference_table_key | 987987
other_table   | 11               |  reference_table_key | 3213
another_table | 3                |  do_stuff_key        | 4645
another_table | 5                |  do_stuff_key        | 43546
another_table | 7                |  do_stuff_key        | 464356
unknown_table | 1                |  unkown_column_key   | 435435
unknown_table | 1                |  some_other_column_key | 34543
unknown_table | 3                |  unkown_column_key   | 124
unknown_table | 3                |  some_other_column_key | 123

这类似于但不是Postgres: SQL to list table foreign keys的副本。该问题显示了表结构。我想找到具体的实例。

本质上,如果我DELETE FROM reference_table WHERE reference_value IS NULL;要这样做,postgres 必须在内部做一些事情来确定它需要reference_table_key在第 2 行中设置other_table为 NULL。我想看看那些行会是什么。

有没有可以做到这一点的查询?是否有一个修饰符可以传递给 DELETE 调用,告诉我哪些表/行/列会受到该 DELETE 的影响?

4

2 回答 2

7

引用列中的 NULL 值

此查询生成 DML 语句以查找所有表中的所有行,其中列具有引用另一个表NULL在该列中保存值的外键约束:

WITH x AS (
 SELECT c.conrelid::regclass    AS tbl
      , c.confrelid::regclass   AS ftbl
      , quote_ident(k.attname)  AS fk
      , quote_ident(pf.attname) AS pk
 FROM   pg_constraint c
 JOIN   pg_attribute  k ON (k.attrelid, k.attnum) = (c.conrelid, c.conkey[1])
 JOIN   pg_attribute  f ON (f.attrelid, f.attnum) = (c.confrelid, c.confkey[1])
 LEFT   JOIN pg_constraint p  ON p.conrelid = c.conrelid AND p.contype = 'p'
 LEFT   JOIN pg_attribute  pf ON (pf.attrelid, pf.attnum)
                               = (p.conrelid, p.conkey[1])
 WHERE  c.contype   = 'f'
 AND    c.confrelid = 'fk_tbl'::regclass  -- references to this tbl
 AND    f.attname   = 'fk_tbl_id'         -- and only to this column
)
SELECT string_agg(format(
'SELECT %L AS tbl
     , %L AS pk
     , %s::text AS pk_val
     , %L AS fk
     , %L AS ftbl
FROM   %1$s WHERE %4$s IS NULL'
                  , tbl
                  , COALESCE(pk 'NONE')
                  , COALESCE(pk 'NULL')
                  , fk
                  , ftbl), '
UNION ALL
') || ';'
FROM   x;

产生这样的查询:

SELECT 'some_tbl' AS tbl
     , 'some_tbl_id' AS pk
     , some_tbl_id::text AS pk_val
     , 'fk_tbl_id' AS fk
     , 'fk_tbl' AS ftbl
FROM   some_tbl WHERE fk_tbl_id IS NULL
UNION ALL
SELECT 'other_tbl' AS tbl
     , 'other_tbl_id' AS pk
     , other_tbl_id::text AS pk_val
     , 'some_name_id' AS fk
     , 'fk_tbl' AS ftbl
FROM   other_tbl WHERE some_name_id IS NULL;

产生如下输出:

    tbl    |     pk       | pk_val |    fk        |  ftbl
-----------+--------------+--------+--------------+--------
 some_tbl  | some_tbl_id  | 49     | fk_tbl_id    | fk_tbl
 some_tbl  | some_tbl_id  | 58     | fk_tbl_id    | fk_tbl
 other_tbl | other_tbl_id | 66     | some_name_id | fk_tbl
 other_tbl | other_tbl_id | 67     | some_name_id | fk_tbl
  • 不可靠地覆盖多列外键或主键。为此,您必须使查询更加复杂。

  • 我将所有主键值转换text以涵盖所有类型。

  • 修改或删除这些行以查找指向其他或任何列/表的外键:

    AND    c.confrelid = 'fk_tbl'::regclass
    AND    f.attname = 'fk_tbl_id' -- and only this column
    
  • 用 PostgreSQL 9.1.4 测试。我用pg_catalog桌子。实际上,我在这里使用的任何东西都不会改变,但这并不能保证在主要版本中。information_schema如果您需要它在更新中可靠地工作,请使用表重写它。那会更慢,但可以肯定。

  • 我没有在生成的 DML 脚本中清理表名,因为quote_ident()使用模式限定名称会失败。您有责任避免使用有害的表名,例如"users; DELETE * FROM users;". 通过更多的努力,您可以分别检索模式名和表名并使用quote_ident().


引用列中的 NULL 值

我的第一个解决方案与您所要求的略有不同,因为您所描述的(据我所知)是不存在的。该值为NULL“未知”,无法引用。如果您实际上想在具有指向它的 FK 约束的列中查找具有值的行NULL当然不是具有该值的特定行),那么查询可以大大简化:NULL

WITH x AS (
 SELECT c.confrelid::regclass   AS ftbl
       ,quote_ident(f.attname)  AS fk
       ,quote_ident(pf.attname) AS pk
       ,string_agg(c.conrelid::regclass::text, ', ') AS referencing_tbls
 FROM   pg_constraint c
 JOIN   pg_attribute  f ON (f.attrelid, f.attnum) = (c.confrelid, c.confkey[1])
 LEFT   JOIN pg_constraint p  ON p.conrelid = c.confrelid AND p.contype = 'p'
 LEFT   JOIN pg_attribute  pf ON (pf.attrelid, pf.attnum)
                               = (p.conrelid, p.conkey[1])
 WHERE  c.contype = 'f'
 -- AND    c.confrelid = 'fk_tbl'::regclass  -- only referring this tbl
 GROUP  BY 1, 2, 3
)
SELECT string_agg(format(
'SELECT %L AS ftbl
     , %L AS pk
     , %s::text AS pk_val
     , %L AS fk
     , %L AS referencing_tbls
FROM   %1$s WHERE %4$s IS NULL'
                  , ftbl
                  , COALESCE(pk, 'NONE')
                  , COALESCE(pk, 'NULL')
                  , fk
                  , referencing_tbls), '
UNION ALL
') || ';'
FROM   x;

在整个数据库中查找所有此类行(注释掉对一个表的限制)。用 Postgres 9.1.4 测试并为我工作。

我将引用同一外部列的多个表分组到一个查询中,并添加一个引用表列表以提供概述。

于 2012-08-03T03:29:34.583 回答
0

您需要此查询的联合:

select *
from ((select 'other_table' as table_name,
               other_table_key as primary_key,
               'reference_table_key' as table_fk,
               ot.reference_table_key
       from other_table ot left outer join
            reference_table rt
            on ot.reference_table_key = rt.reference_table_key
       where rt.reference_value is null
      ) union all
      (select 'another_table' as table_name,
               another_table_key as primary_key,
               'do_stuff_key' as table_fk,
               at.do_stuff_key
       from another_table at left outer join
            reference_table rt
            on at.do_stuff_key = rt.reference_table_key
       where rt.reference_value is null
      )
     ) t
于 2012-08-02T18:53:42.100 回答