In modern database systems, triggers are powerful tools that allow automatic execution of logic in response to data changes.
In GBase 8s database, triggers become even more flexible with the use of:
REFERENCING NEW AS NEWREFERENCING OLD AS OLD
These clauses let developers precisely access before and after values of data changes, enabling advanced business logic implementation.
π 1. What is a Trigger in GBase 8s?
A trigger is a database object that automatically executes when specific events occur:
- INSERT
- UPDATE
- DELETE
π It is commonly used for:
- Audit logging
- Data validation
- Business rule enforcement
- Historical tracking
π§ 2. Why REFERENCING Matters
Without referencing:
- You cannot clearly distinguish old vs new values
- Logic becomes harder to maintain
- Code readability decreases
With referencing:
REFERENCING NEW AS NEW
REFERENCING OLD AS OLD
`
π You can explicitly access:
| Keyword | Meaning |
|---|---|
| NEW | Newly inserted/updated values |
| OLD | Previous values before update/delete |
π 3. Example Table Setup
sql id="gbase_trigger_table"
CREATE TABLE t_sale (
f_saleid INT,
f_productname VARCHAR(50),
f_qty INT
);
sql id="gbase_log_table"
CREATE TABLE t_log (
f_saleid INT,
f_productname VARCHAR(50),
f_oldqty INT,
f_newqty INT
);
βοΈ 4. Using NEW Data in INSERT Trigger
Create Trigger
sql id="gbase_trigger_new"
CREATE TRIGGER trg_sale_insert
AFTER INSERT ON t_sale
REFERENCING NEW AS NEW
FOR EACH ROW
(
INSERT INTO t_log (f_saleid, f_productname, f_newqty)
VALUES (NEW.f_saleid, NEW.f_productname, NEW.f_qty)
);
Insert Data
sql id="gbase_insert_trigger"
INSERT INTO t_sale VALUES (1, 'Phone', 10);
π Result:
-
t_salestores the new record -
t_logrecords inserted data automatically
π 5. Using OLD Data in UPDATE Trigger
Create Trigger
sql id="gbase_trigger_old"
CREATE TRIGGER trg_sale_update
AFTER UPDATE OF f_qty ON t_sale
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
(
INSERT INTO t_log (f_saleid, f_productname, f_oldqty, f_newqty)
VALUES (
OLD.f_saleid,
OLD.f_productname,
OLD.f_qty,
NEW.f_qty
)
);
Update Data
sql id="gbase_update_trigger"
UPDATE t_sale
SET f_qty = 20
WHERE f_saleid = 1;
π 6. What Happens Internally?
When the update runs:
- OLD values are captured before modification
- NEW values represent updated data
- Trigger executes automatically
- Log table records both states
π§© 7. Real-World Use Cases
β Audit Logging
Track every change in sensitive tables
β Inventory Systems
Monitor stock changes:
- Before quantity
- After quantity
β Financial Systems
Ensure traceability of:
- Balance updates
- Transaction modifications
β οΈ 8. Common Mistakes
β Confusing NEW and OLD
sql
OLD.f_qty -- before change
NEW.f_qty -- after change
β Missing REFERENCING clause
π Leads to unclear or invalid variable access
β Overusing triggers
- Can slow down bulk operations
- Harder to debug complex logic
β‘ 9. Best Practices
- Keep trigger logic lightweight
- Use triggers only for critical business rules
- Always log meaningful changes only
- Avoid heavy computations inside triggers
π§ 10. Key Insight
In GBase 8s database, triggers are not just automation toolsβthey are:
A mechanism for enforcing data integrity at the database layer.
REFERENCING NEW and OLD gives you precise control over data evolution, making triggers far more powerful and expressive.
π Final Thoughts
Understanding REFERENCING NEW AS NEW and REFERENCING OLD AS OLD is essential for:
- Building audit systems
- Tracking data changes
- Implementing enterprise-level business logic
When used correctly, triggers become a silent but powerful layer of logic inside the database.
Top comments (0)