DELETE / INSERT行的SQL CTE语法

时间:2015-06-17 21:10:57

标签: sql postgresql common-table-expression postgresql-9.2

从表中删除的CTE语法是什么,然后插入到同一个表并返回 insert

在2个小时的睡眠状态下运行并且看起来并不正确(除了这不会执行之外):

WITH delete_rows AS (
   DELETE FROM <some_table> WHERE id = <id_value>
   RETURNING *
)
SELECT * FROM delete_rows
UNION
(
   INSERT INTO <some_table> ( id, text_field )
      VALUES ( <id_value>, '<text_field_value>' )
      RETURNING *
)

预期的行为是首先清除ID的所有记录,然后插入相同ID的记录(故意不是upsert)并返回插入的记录(而不是删除)。

4 个答案:

答案 0 :(得分:4)

您的问题更新明确表示您无法在一个声明中执行此操作。

包含在同一语句的CTE中,两个操作([ii, jj, aa] = find(A); [~, kk] = sort(aa, 'descend'); result = [ii(kk) jj(kk)]; INSERT)都会看到表格的相同快照,并且几乎同时执行 。即,DELETE仍会看到您认为已删除的所有行。 Per documentation:

  

所有语句都使用相同的快照执行(参见章节   13),所以他们不能“看到”彼此对目标表的影响。

您可以将它们作为两个独立的语句包装到同一个事务中 - 这似乎也不是必需的,但它可以允许整个操作以原子方式成功/失败:

INSERT

现在,BEGIN; DELETE FROM <some_table> WHERE id = <id_value>; INSERT INTO <some_table> (id, text_field) VALUES ( <id_value>, '<text_field_value>') RETURNING *; COMMIT; 可以看到INSERT的结果。

答案 1 :(得分:0)

CREATE TABLE test_table (value TEXT UNIQUE);
INSERT INTO test_table SELECT 'value 1';
INSERT INTO test_table SELECT 'value 2';

WITH delete_row AS (DELETE FROM test_table WHERE value='value 2' RETURNING 0)
  INSERT INTO test_table
    SELECT DISTINCT 'value 2' 
    FROM (SELECT 'dummy') dummy
    LEFT OUTER JOIN delete_row ON TRUE
    RETURNING *;

上面的查询处理DELETE删除0/1 /某些行的情况。

答案 2 :(得分:0)

详细介绍了skif1979的“ DelSert” CTE方法“ Logged DelSert:”

-- setups
DROP TABLE IF EXISTS _zx_t1 ;

CREATE TEMP TABLE 
  IF NOT EXISTS 
     _zx_t1 
     ( id bigint
     , fld2 bigint
     , UNIQUE (id)
     );
-- unique records
INSERT INTO _zx_t1 SELECT 1, 99;
INSERT INTO _zx_t1 SELECT 2, 98;


WITH 
  _cte_del_row AS 
   (   DELETE 
       FROM _zx_t1 
       WHERE id = 2 
     RETURNING id as _b4_id, fld2 as _b4_fld2 -- returns complete deleted row
   )
 , _cte_delsert AS
     (  INSERT 
       INTO _zx_t1 
       SELECT DISTINCT 
          _cte_del_row._b4_id
        , _cte_del_row._b4_fld2 + 1 
        from (SELECT null::integer AS _zunk) _zunk  -- skif1979's trick here
             LEFT OUTER JOIN _cte_del_row         -- clever LOJ magic  
             ON TRUE                              -- LOJ cartesian product
        RETURNING id as _aft_id , fld2 as _aft_fld2 -- return newly "delserted" rows
       )
  SELECT * -- returns before & after snapshots from CTE's
  FROM 
   _cte_del_row
   , _cte_delsert ; 

 RESULT: 
           _b4_id | _b4_fld2 | _aft_id | _aft_fld2 
          --------+----------+---------+-----------
                2 |      209 |       2 |       210

AFAICT都是以工作为单位线性发生的,类似于日记或记录的更新。

  • 适用于

    • 子记录
    • 没有FK的OR模式
    • 带有级联删除的OR FK
  • 不适用于

    • 具有FK且没有级联删除的父记录

答案 3 :(得分:0)

与“ Logged DelSert”类似的一个相关(更好的是IMO)答案是,已记录的“ SelUp”:

    -- setups
    DROP TABLE IF EXISTS _zx_t1 ;

    CREATE TEMP TABLE 
      IF NOT EXISTS 
         _zx_t1 
         ( id bigint
         , fld2 bigint
         , UNIQUE (id)
         );
    -- unique records
    INSERT INTO _zx_t1 SELECT 1, 99;
    INSERT INTO _zx_t1 SELECT 2, 98;


    WITH 
      _cte_sel_row AS 
       (   SELECT                 -- start unit of work with read
              id as _b4_id        -- fields need to be aliased 
             ,fld2 as _b4_fld2    -- to prevent ambiguous column errors
           FROM _zx_t1 
           WHERE id = 2
           FOR UPDATE 
       )
     , _cte_sel_up_ret AS           -- we're in the same UOW
       (  UPDATE _zx_t1             -- actual table
           SET fld2 = _b4_fld2 + 1  -- some actual work
          FROM  _cte_sel_row    
            WHERE id = _b4_id
               AND fld2 < _b4_fld2 + 1  -- gratuitous but illustrates the point 
          RETURNING id as _aft_id, fld2 as _aft_fld2
         ) 
    SELECT  
          _cte_sel_row._b4_id
         ,_cte_sel_row._b4_fld2         -- before
         ,_cte_sel_up_ret._aft_id  
         ,_cte_sel_up_ret._aft_fld2     -- after
       FROM _cte_sel_up_ret  
          INNER JOIN _cte_sel_row  
           ON TRUE AND _cte_sel_row._b4_id = _cte_sel_up_ret._aft_id
    ;

 RESULT: 
           _b4_id | _b4_fld2 | _aft_id | _aft_fld2 
          --------+----------+---------+-----------
                2 |      209 |       2 |       210

另请参阅: https://rob.conery.io/2018/08/13/transactional-data-operations-in-postgresql-using-common-table-expressions/