Reader small image

You're reading from  Learn SQL using MySQL in One Day and Learn It Well

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781836205678
Edition1st Edition
Right arrow
Author (1)
Jamie Chan
Jamie Chan
author image
Jamie Chan

Jamie Chan is a tutor and freelance programmer with years of experience and a dedicated passion for sharing the joy of programming with as many people as possible. With seven bestselling programming books on Amazon, Jamie's publications stand out for their ability to break down complex concepts into simple terms. Additionally, each book includes complete projects at the end, enabling hands-on learning and a deep understanding of the concepts presented.
Read more about Jamie Chan

Right arrow

Creating a Trigger

 

Next, we’ll use the following syntax to create our trigger (line numbers are added on the left for reference and are not part of the syntax):

 

1 DELIMITER $$

2

3 CREATE TRIGGER name_of_trigger BEFORE/AFTER UPDATE/DELETE/INSERT ON name_of_table FOR EACH ROW

4

5 BEGIN

6      -- Actions to take

7 END $$

8

9 DELIMITER ;

 

This syntax may look overwhelming at first. Do not worry, we’ll go over each keyword one by one.

 

On line 1, we have a new keyword - DELIMITER.

 

A delimiter is a character or sequence of characters that specifies the end of a SQL statement. Recall that previously, we have always used ; to specify the end of our SQL statements? We can change that if we want.

 

The code on line 1 (DELIMITER $$) tells MySQL that we want to use $$ as the delimiter for our CREATE TRIGGER statement (from lines 3 to 7).

 

Why do we need to do that?

 

The reason for...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learn SQL using MySQL in One Day and Learn It Well
Published in: Apr 2024Publisher: PacktISBN-13: 9781836205678

Author (1)

author image
Jamie Chan

Jamie Chan is a tutor and freelance programmer with years of experience and a dedicated passion for sharing the joy of programming with as many people as possible. With seven bestselling programming books on Amazon, Jamie's publications stand out for their ability to break down complex concepts into simple terms. Additionally, each book includes complete projects at the end, enabling hands-on learning and a deep understanding of the concepts presented.
Read more about Jamie Chan