before_insert_trigger

BEFORE INSERT Trigger

0 Shares

A BEFORE INSERT Trigger means that event is triggered prior to new data being added to the table

Syntax

The syntax to create a BEFORE INSERT Trigger in MySQL is:

Parameters or Arguments

OR REPLACE: It is an optional parameter. It is used to re-create the trigger if it already exists. It facilitates you to change the trigger definition without using a DROP TRIGGER statement.
trigger_name : The name of the trigger to create.
BEFORE INSERT: It indicates that the trigger will fire before the INSERT operation is executed.
table_name : The name of the table that the trigger is created on.

Restrictions

  • You can not create a BEFORE trigger on a view.
  • You can update the NEW values.
  • You can not update the OLD values.

Example

Let’s look at an example of how to create an BEFORE INSERT trigger using the CREATE TRIGGER statement in MySQL.

If you had a table created as follows:

We could then use the CREATE TRIGGER statement to create an BEFORE INSERT trigger as follows:

In this  trigger when a new row is added to the t_contacts  table dtCreatedOn will be set to current date and time and the vchCreatedBy will set as the logged user name.

0 Shares

Rasmi Ranjan

A tech guy who loves to learn code. I'm open source enthusiast and a delicious coffee maker

Leave a Reply

Your email address will not be published. Required fields are marked *