sql >> データベース >  >> RDS >> PostgreSQL

PostgreSQLのルールとnextval()/ serialの問題(非常にPostgreSQL固有)

    ドキュメントから http://www.postgresql.org/docs/8.4/ static / rules.html

    そのため、最初に何も実行せずにクエリを書き換えます。

    複数のレコードを一度に挿入しない場合でも機能させることができます:

    create or replace rule ct_i_children1 as
      on insert to Children1
      do instead (
        insert into Parents(id, attribute1, type)
          values(nextval('parents_id_seq'), new.attribute1, 'Child1');
        insert into Partial_Children1(id, attribute2, type)
          values(currval('parents_id_seq'), new.attribute2, 'Child1');
      );
    

    次に、次のことができます。

    insert into Children1 (attribute1, attribute2) values ('a1', 'a2');
    insert into Children1 (attribute1, attribute2) values ('b1', 'b2');
    

    しかし、そうではありません

    insert into Children1 (attribute1, attribute2)
      values ('a1', 'a2'),
             ('b1', 'b2');
    

    したがって、トリッキーなcurrval()呼び出しでルールシステムを使用するべきではありません。

    さらに、これらのページのコメントをご覧ください:

    もう1つのヒント:postgresqlメーリングリストでのサポートは、データベースエンジン自体と同じくらい優れています!

    ちなみに、postgresqlがすぐに継承をサポートしていることをご存知ですか?

    概要:トリガーを使用するか、複数行の挿入を避ける必要があります!



    1. 関数のみが組み込まれたurlencode

    2. 2つの時間値を追加することとの混同

    3. mongoDBとmySQL-いくつかの面で一方が他方より優れている理由

    4. SQLServer2008からのXMLファイルの生成