Advanced MySQL Features: Stored Procedures and Triggers
Explore advanced MySQL features like stored procedures (precompiled SQL code) and triggers (automatic actions triggered by database events).
Mastering MySQL Triggers: Accessing Data
Triggers: Accessing Data within Triggers
Triggers in MySQL are special stored programs that automatically execute in response to certain events on a particular table. These events are typically INSERT
, UPDATE
, or DELETE
operations. A crucial aspect of trigger functionality is the ability to access and manipulate the data that is being affected by the triggering event. This is primarily achieved through the NEW
and OLD
keywords.
The ability to access data within a trigger allows you to implement a variety of functionalities, including:
- Data Validation: Ensure that data being inserted or updated meets specific criteria.
- Auditing: Log changes made to a table, tracking who made the changes and when.
- Data Transformation: Modify data before it is stored in the table.
- Referential Integrity: Maintain consistency between related tables.
- Calculated Fields: Automatically update fields based on other values.
Without the ability to access the data being affected by the trigger, the functionality of triggers would be severely limited. NEW
and OLD
provide the mechanism for accessing this critical information.
Understanding `NEW` and `OLD` Keywords
NEW
and OLD
are special keywords used within triggers to refer to the data being modified. Their behavior depends on the type of trigger:
`NEW` Keyword
The NEW
keyword refers to the new row being inserted or updated. It allows you to access and potentially modify the values of the columns in the new row. The availability of NEW
depends on the trigger event:
INSERT
triggers:NEW
represents the row being inserted.UPDATE
triggers:NEW
represents the row after the update.DELETE
triggers:NEW
is not available because there is no new row being created or updated.
You can access individual column values using NEW.column_name
. You can also modify these values before the data is written to the table. For example:
CREATE TRIGGER before_insert_product
BEFORE INSERT ON products
FOR EACH ROW
BEGIN
-- Convert product name to uppercase
SET NEW.product_name = UPPER(NEW.product_name);
-- Set default value if price is missing
IF NEW.price IS NULL THEN
SET NEW.price = 0.00;
END IF;
END;
`OLD` Keyword
The OLD
keyword refers to the old row that existed before an update or delete operation. It allows you to access the original values of the columns in the row being affected. The availability of OLD
also depends on the trigger event:
INSERT
triggers:OLD
is not available because there is no old row being modified.UPDATE
triggers:OLD
represents the row before the update.DELETE
triggers:OLD
represents the row being deleted.
Similar to NEW
, you access individual column values using OLD.column_name
. However, you cannot modify the values referenced by OLD
; they are read-only. For example:
CREATE TRIGGER after_update_product
AFTER UPDATE ON products
FOR EACH ROW
BEGIN
-- Log the changes to an audit table
INSERT INTO product_audit (product_id, old_price, new_price, updated_at)
VALUES (OLD.product_id, OLD.price, NEW.price, NOW());
END;
Important Considerations
- Read-Only within `AFTER` Triggers: While you can modify `NEW` values in
BEFORE
triggers,NEW
values are read-only withinAFTER
triggers. This is because the data has already been written to the table inAFTER
triggers. - NULL Values: Be mindful of
NULL
values. Using comparison operators (=, !=) withNULL
will not work as expected. UseIS NULL
orIS NOT NULL
instead. - Performance: Complex trigger logic can impact database performance. Optimize your triggers to ensure they execute efficiently.