MySQL Essential Training
Illustration by Don Barnett

MySQL Essential Training

with Bill Weinman

Video: Preventing automatic updates with a trigger

Triggers may also be used to prevent changes to rows that Remember I mentioned to you that this signal
Expand all | Collapse all
  1. 4m 22s
    1. Welcome
      1m 3s
    2. Using the exercise files
      1m 31s
    3. What is MySQL?
      1m 48s
  2. 45m 37s
    1. Installation overview
      3m 16s
    2. Installing XAMPP on Windows
      5m 55s
    3. Installing XAMPP on the Mac
      6m 38s
    4. Setting up MySQL users
      11m 31s
    5. Installing SID on Windows
      5m 43s
    6. Installing SID on the Mac
      6m 6s
    7. Installing time zone support in MySQL on Windows
      6m 28s
  3. 45m 43s
    1. The SELECT statement
      3m 57s
    2. Selecting rows
      4m 57s
    3. Selecting columns
      3m 8s
    4. Sorting results with ORDER BY
      2m 58s
    5. Filtering results with WHERE
      3m 52s
    6. Filtering results with LIKE and IN
      3m 41s
    7. Filtering results with regular expressions
      8m 21s
    8. Inserting rows
      4m 9s
    9. Updating rows
      2m 21s
    10. Deleting rows
      2m 25s
    11. Literal strings
      3m 12s
    12. Understanding NULL
      2m 42s
  4. 41m 47s
    1. Creating a database
      4m 30s
    2. Creating a table
      7m 18s
    3. Creating indexes
      6m 8s
    4. Controlling column behavior with constraints
      4m 46s
    5. Creating an ID column
      6m 58s
    6. Using foreign key constraints
      7m 58s
    7. Altering a table
      4m 9s
  5. 28m 56s
    1. What are data types?
      4m 1s
    2. Numeric types
      5m 21s
    3. String types
      2m 58s
    4. Date and time types
      7m 2s
    5. Bit type
      2m 26s
    6. Boolean values
      2m 15s
    7. Enumeration types
      4m 53s
  6. 32m 34s
    1. String functions
      6m 57s
    2. Numeric functions
      6m 2s
    3. Date and time functions
      4m 12s
    4. Time zones in MySQL
      3m 37s
    5. Formatting dates
      1m 51s
    6. Aggregate functions
      5m 45s
    7. Flow control with CASE
      4m 10s
  7. 7m 6s
    1. Maintaining database integrity with transactions
      4m 46s
    2. Using transactions for performance
      2m 20s
  8. 16m 49s
    1. Updating a table with a trigger
      5m 11s
    2. Preventing automatic updates with a trigger
      7m 29s
    3. Logging transactions with a trigger
      4m 9s
  9. 14m 11s
    1. Creating a simple subselect
      3m 23s
    2. Searching within a result set
      3m 53s
    3. Creating a view
      3m 32s
    4. Creating a joined view
      3m 23s
  10. 12m 26s
    1. Understanding MySQL stored routines
      2m 0s
    2. Creating a stored function
      4m 34s
    3. Creating a stored procedure
      5m 52s
  11. 14m 4s
    1. The multi-platform PDO interface
      3m 44s
    2. Executing the SQL
      4m 8s
    3. Implementing auto-increment IDs
      2m 3s
    4. Using a stored funciton
      4m 9s
  12. 1m 3s
    1. Goodbye
      1m 3s

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course MySQL Essential Training
4h 24m Beginner May 14, 2014

Viewers: in countries Watching now:

MySQL is by far the most popular database management system for small- to medium-sized web projects. In this course, Bill Weinman provides clear, concise tutorials that guide you through creating and maintaining a MySQL database of your own. Bill explores the basic syntax, using SQL statements to create, insert, update, and delete data from your tables. He also covers creating a new database from scratch, as well as data types, transactions, subselects, views, and stored routines. Plus, learn about the multi-platform PHP PDO interface that will help you connect your database to web applications.

Topics include:
  • Writing queries
  • Creating and updating databases and tables
  • Using MySQL built-in functions
  • Sorting and filtering data
  • Updating tables with triggers
  • Working with subselects and views
  • Creating and using a stored function
Bill Weinman

Preventing automatic updates with a trigger

Triggers may also be used to prevent changes to rows that have already been reconciled or should not be changed for other reasons. In this lesson, I'll be copying and pasting from the Chapter Seven exercise files. Keep in mind that I still have the customer table from the previous lesson, so if you haven't followed along you can create it from the exercise file. Going to be using the scratch database and I'm going to switch over here to the exercise file for Chapter 7 and scroll down to movie 02, preventing automatic updates.

And I'm just going to copy and paste everything from here. All the way down to there. But first let's just take a look at it. I'm going to drop the widget sale table and recreate it. And the reason I'm recreating it, is because I want to add this reconciled flag. And you notice that I'm inserting some rows into the table and as I insert those rows, I'm setting that flag just on the middle one, on. Item number two and I'm setting that flag on that one and its not going to be set on the others.

So I want to prevent updates to this row by setting that flag and so here's the trigger that does and the first thing you're going to notice here is this delimiter statement. This is not actually an SQL statement. This is something that my SQL uses in their command line client that I decided to also implement in sid because it's a common paradigm in my SQL. What this does, is it allows you to use semi colons within a statement without actually terminating the statement.

So this create trigger actually has two statements in it while it's got the If that requires a semi colon. And it has this signal statement that also requires a semi colon because it's a separate statement. And so in order for this all to be treated as one unit, I've changed the delimeter from semi colon to this double slash. And so that's a common paradigm in my SQL and then I set the, the Delimiter back to the semicolon afterwards, so that we can use it in the rest of this code.

So what this trigger does, you notice that this is a before update on widget sale, so when you go to update an already existing row in the widget sale table. Before the update is applied, this triggered code will be executed. And what this triggered code does, you'll notice we have the 4h row which is required for create trigger. And then we have this begin end block. And that allows more than one statement to be part of this trigger. So the first statement is this if, which.

Checks the reconciled flag in this row that's about to be updated. And again, we have this new ID. So, we have the row that's about to be updated, and it's all ready to be updated in to the table, but it hasn't been committed yet to the database. So we check, and we see for that row whether the reconciled flag is greater than zero. And if it is, we're going to set an error. We're going to throw an error. This signal statement allows you to throw an error in MySQL.

Now, the signal statement was new in MySQL version 5.5 and we're running 5.6 here, and 5.5 is relatively new in the last few years. So, I've got some alternate code I can show you. That works as a kluge to work around this and I'll show you that in a little bit. But for now you should learn about this signal statement. The signal statement is specific to MySQL and allows you to set this MySQL state for 5,000. Which is a generic unhandled exception, which means that its an error that will end a transaction.

And I can set the message text and you'll see that we actually get this error message, it says cannot update reconciled row in widgetSale. Then we set the delimiter back, and we start a transaction because that's the only way that we can actually prevent this table from getting updated is if we do this update within a transaction, and we throw an error that will end the transaction before the data is committed. So, let's look at what happens. I'm going to go ahead, and copy and paste all this code. So, it'll drop the table.

It'll recreate the table, it'll insert some data including a reconciled row and then we'll try and update that reconciled row. So here it is, and you'll notice there's our error, query number nine error, can not update reconciled row in widgetSale. So that's our error message there from the signal in the trigger. So what I tried to do here is, I've tried to update the row where the ID equals two, which is this one here. I tried to add nine to the quantity, and you notice that it didn't allow it.

Here's the row before the update, and here's the row after the update, and they're exactly the same. If I try to do this to one of the other rows that's not reconciled, like for example. Row number one. I don't get the error and you'll notice that row number one was five before the update, and 14 after. Nine got added to it, and it worked just fine. So our trigger is doing what its supposed to do. If the reconcile flag is set its not allowing the update, and if the reconcile flag is not set, then it is allowing the update.

Remember I mentioned to you that this signal only works in my SQL version 5.5 and later. So, what I have here in your exercise files. I've got alternate code, and I'm just going to copy this. And paste it in right here. This is alternate code that will work in previous versions of MySQL. It'll also work in this version. The thing is, is that it just, it fakes the error by trying to update a table that doesn't exist. And this table doesn't exist is the error message.

And so, it's kind of a kludge. It's kind of a workaround to the fact that you. Didn't used to have any way of actually setting an error. And so when I press Go on this. Let me go ahead and set this back to ID number two so that we get the error. And you see we get an error that says, Base table view not found. Table blah, blah, blah doesn't exist and that blah, blah, blah is scratch.Error: cannot update reconciled row in widgetSale, doesn't exist. And that is right here.

That's the name of our table, Error: cannot update reconciled row in widgetSale. And you notice again, our table did not get updated, and this works just as well. If I change this to number one, you'll see that, in fact, the quantity does get updated and we don't get that error message. So it works just as well, it's just not as clean and we're not getting the error message in a clean way. And that's why we have that, signal statement. So, triggers may be used to prevent changes to tables or rows that should not be changed.

It's a simple usage and it's a valuable business tool. Keep in mind that triggers, errors and signals are all implemented differently from system to system. This code will work. With MySQL, but you'll need to change it in order for it to work with other systems. And again, for this chapter I'm not deleting tables at the end of each lesson. I'm going to leave these tables in place so that we can continue to use them in the next movie.

There are currently no FAQs about MySQL Essential Training.

Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed MySQL Essential Training.

Return to your organization's learning portal to continue training, or close this page.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.