重要

翻訳は あなたが参加できる コミュニティの取り組みです。このページは現在 60.00% 翻訳されています。

15.6. レッスン: ルール

ルールは "クエリ木" に書き換えることができます。一つの一般的な使用法は、更新可能なビューなど、ビューを実装することです。 Wikipediaより

このレッスンの目標: データベースの新しいルールを作成する方法を学習する.

15.6.1. ログに記録するルールを作る

Say you want to log every change of phone_no in your people table in to a people_log table. So you set up a new table:

create table people_log (name text, time timestamp default NOW());

In the next step, create a rule that logs every change of a phone_no in the people table into the people_log table:

create rule people_log as on update to people
  where NEW.phone_no <> OLD.phone_no
  do insert into people_log values (OLD.name);

ルールが正しく機能することを確認するには, 電話番号を変更してみましょう :

update people set phone_no = '082 555 1234' where id = 2;

Check that the people table was updated correctly:

select * from people where id=2;

 id |    name    | house_no | street_id |   phone_no
----+------------+----------+-----------+--------------
  2 | Joe Bloggs |        3 |         2 | 082 555 1234
(1 row)

Now, thanks to the rule we created, the people_log table will look like this:

select * from people_log;

    name    |            time
------------+----------------------------
 Joe Bloggs | 2014-01-11 14:15:11.953141
(1 row)

注釈

The value of the time field will depend on the current date and time.

15.6.2. 結論

ルールを使用すると、データベースの他の部分の変更を反映するために、自動的にデータベース内でデータを追加または変更できます。

15.6.3. 次は?

The next module will introduce you to Spatial Database using PostgreSQL with PostGIS extension, which takes these database concepts and applies them to GIS data.