PostgreSQL triggers Control update Fields

Source: Internet
Author: User

-- Po_info is the main table, po_item is the slave table, and po_info contains the updated field lastupdatetime, which is the associated field po_id of the two tables.

On update cascade postgresql

--------------- Po_id: Use a trigger to monitor table updates and use lastupdatetime to record time ------------------
-- Trigger creation process --
Create or replace function po_info ()
Returns trigger

postgresql update
$ Body $
Begin
If (tg_op = 'update') then
If new. lastupdatetime = old. lastupdatetime then
Update po_info set lastupdatetime = now () Where po_id = new. po_id;

postgresql update from select
End if;
End if;
Return NULL;
End;

update postgresql
$ Body $ language plpgsql
-- Create a trigger --
Create trigger tr_po_info
After update on po_info
For each row execute procedure po_info ();

--------------- Po_item uses the trigger to monitor table updates and change the lastupdatetime time of po_id ------------------
-- Trigger creation process --
Create or replace function po_item ()
Returns trigger
$ Body $

postgresql update join
Begin
If (tg_op = 'update') then
If (New! = Old) then
Update po_info P1 set lastupdatetime = now () Where p1.po _ id = new. po_id;
End if;
End if;
Return NULL;
End;

python postgresql update
$ Body $ language plpgsql

-- Create a trigger --
Create trigger tr_po_item
After update on po_item
For each row execute procedure po_item ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.