Search icon CANCEL
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn SQL using MySQL in One Day and Learn It Well

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

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781836205678
Pages 121 pages
Edition 1st Edition
Languages
Author (1):
Jamie Chan Jamie Chan
Profile icon Jamie Chan

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 chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}