简体   繁体   中英

postgresql trigger after insert or update or delete

I have table called mdl_user and I added a new columns for this table, the first one is called "LastOperation" and the second one "TimeStamp"

I need to create trigger after insert or update or delete and write the output as "I" or "U" or "D" at "LastOperation" column and the time when this action happenes at "TimeStamp" column

NOTE: All this stuff to be in the same table not to be triggered for another table

You can get what you want and still use standard DML (including delete). It does however require a little behind the scenes manipulation of database objects. What it involves doing your DML against not a table but an update-able view.

  1. Add the new columns as usual to the table.
  2. Rename the table. Say to mdl_user_tab.
  3. Create a VIEW with the same name and definition as the old table. Simple select (no join) from new table name.
  4. Create a trigger function which interprets the DML manipulates the actual table.
  5. Create an INSTEAD trigger for all DML operations against the VIEW.

You now process everything against the view (most existing code will not require updating). But you may need some additional functionality for actually processing the table. See example here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM