Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
A trigger is an operation that's automatically performed when a specific database event occurs. A common use for triggers is to force a table to be updated, whenever a row's inserted or updated in another table. How triggers are implemented and used vary significantly from system to system and MySQL is no exception. The syntax used here will work with MySQL and will need to be modified, if you want to use these techniques in another database management system. For this example, we're going to be using the scratch database and we're going to be copying and pasting from the Chapter 7 File in the exercise files.
And I'm just going to grab all this code from right here till right there, and I'm going to copy and paste that but before we do that, let's take a look at the code and see what it does. Here, we create a couple of tables. The first one is a customer table and it has a serial number ID and it has a name for the customer and a last order ID, which is what we're going to be updating as a result of the trigger. And then there's a sale table with an ID serial and an item ID, which we're not using because we don't have an items table yet.
And a customer ID, which points to the customer tables, so that's our relationship, a quantity and a price and so we go and insert a few rows into the customer table: Bob, Sally, and Fred. All we're updating at this point is the names and we'll be updating with the last order ID from the trigger, when a sale is made. And so we'll just show that widget customer table and then we create the trigger.
Now, the CREATE TRIGGER syntax for MySQL, is pretty normal, with the exception is, FOR EACH ROW, which is a MySQL specific thing. It starts with CREATE TRIGGER and the name of the trigger and then the event that the trigger is going to be triggered by and in this case, it's after INSERT ON widget Sale. So when a row's inserted into the widget sale table, after that row is inserted, the trigger is run and in this case, for each row as just required by MySQL, doesn't actually seem to do anything and then the statement itself that is triggered is this update statement.
So we update the widget customer table and we set the last order ID to the ID from the row that was just inserted into the widget sale and that row that was just inserted is provided to the trigger with this new keyword. So that's a pseudo table with the, just this one row. It's really just a row and it just points to that row that was just inserted into the table and so that's really convenient. I can take the idea of that row, which was automatically generated by this serial up here and I can assign that to last order ID and I can use it to find the customer ID.
So we have NEW.customer_id. That's the customer ID that we're going to update with the last_order_id. So we use this WHERE clause in our update and that makes that work. So now we insert three rows into this sale table and that will trigger this new widget sale trigger three times and then we take a look at both of these tables. So I'm going to go ahead and select all of this and copy and paste it into SID and press go and here we go, here's our customer table, right after we just inserted Bob, Sally, and Fred.
So we have those three ID's and there's our Select statement that generates this and now we create the trigger and we go ahead and run the trigger by inserting three rows into the sale table. So, we insert these three rows into the sale table and here they are: customer IDs 3, 2 and 1, quantities and prices and so in our trigger, our last order id, you see it was null before. Now after the sale table has the three rows inserted into it, our last order ID is now updated.
So Bob, which is ID number 1 in the customer table, his last order was is number 3. So the customer ID 1 is sale ID 3 and customer ID 2 is sale ID 2 and customer ID 3 is sale number 1. So those three last order ID values, they were null before. Those are being inserted by this trigger. You see we did not update the customer table at all, except through the trigger.
So a trigger is an excellent way to enforce business rules that require a table to be updated whenever another table is updated. This example shows you how to update one table, when a row is inserted into another table. For this lesson, I'm not going to delete the tables at the end of each lesson. I'm leaving these tables in place, so that we can use them in the rest of the chapter.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98482 Viewers
61 Video lessons · 85764 Viewers
71 Video lessons · 69669 Viewers
56 Video lessons · 101993 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.
Your file was successfully uploaded.