Database Reference
In-Depth Information
To verify that the rows are inserted, use the following command:
warehouse_db=# SELECT warehouse_id, warehouse_name, state FROM
warehouse_tbl;
warehouse_id | warehouse_name | state
--------------+----------------+-------
1 | Mark Corp | CT
2 | Bill & Co | CT
(2 rows)
You can ind detailed references in the PostgreSQL manual, which is
available at
http://www.postgresql.org/docs/9.4/static/
server-programming.html
, about the PostgreSQL implementation
of triggers in native procedural languages with syntax and code.
We have already discussed that the trigger function gets input through a specially
passed TriggerData structure, which contains a set of local variables that we will use
in our trigger functions. This set of variables includes
OLD
,
NEW
, and other variables
that have
TG_
at the start of their names, such as
TG_WHEN
,
TG_TABLE_NAME
.
Let's discuss these set of variables:
•
NEW
: This variable is of the
RECORD
type and contains the new row to be
stored for the
INSERT
/
UPDATE
command in row-level triggers
•
OLD
: This variable is also of the
RECORD
type and stores the old row for the
DELETE
/
UPDATE
operation in row-level triggers
•
TG_OP
: This will contain one of the strings that informs you for which
operation the trigger is invoked; the value can be
INSERT
,
UPDATE
,
DELETE
, or
TRUNCATE
•
TG_TABLE_NAME
: This holds the name of the table for which the trigger is ired
•
TG_WHEN
: This will contain the string with the value of
BEFORE
,
AFTER
, or
INSTEAD OF
, as per the trigger's deinition
After creating tables, deine a trigger function called
warehouse_audit_func
that
will log changes in the
warehouse_audit
table after an
INSERT
operation on the
warehouse_tbl
table:
warehouse_db=# CREATE OR REPLACE FUNCTION warehouse_audit_func()
RETURNS trigger AS $first_trigger$
BEGIN
INSERT INTO warehouse_audit
(wlog_id, insertion_time)
VALUES