9 hours 41 minutes
All right, welcome back to Module seven. This is less than 7.8 challenge. Three. We've already gone over those top bullet points quite a few times, so I'm not gonna go over them again. However, I'm going to focus on that last bullet point and this lesson or this challenge we're going to create a sequel trigger that implements the correct precision
to solve some new business requirements Now, with that said, let's go ahead and head over to the requirements document to identify exactly what's being asked for.
All right, here we are at the requirements document again. This is for the Model seven course challenged three precision trigger,
and the requirements are as follows utilizing the axiomatic of database. Create the appropriate triggers that will round PM tran dot amount to two decimal places. The default is four decimal places. This should work on updates and inserts, so the key note there is that
should work on updates and inserts, so we'll need a trigger for the update, and we'll need a trigger for the insert.
The table required is PM Tran. The column required is the amount column, and some additional instructions are as follows due to business requirements, we need to ensure that the amounts found on transaction tables are rounded to the correct precision.
A Que Matic A uses a precision of four decimal places by default. We do not want to change the actual table architecture,
so implement triggers that will ensure that the decimal amount provided is rounded to two decimal places. For example, 4.1255 becomes 4.1300
So go ahead and pause the video and see if you can come over the solution on your own before viewing the solution.
Now, with that said, let's go ahead and take a look at that solution.
So here we are at the solution. You know, walking through the script I created to solve the problem indicated in that requirements document. I think you'll find the code used is pretty short, simple and to the point
now, with that said, let's go ahead and walk through this code
so at the top were dropping the triggers if they exist, and I plural eyes that because there are two triggers were creating, we're creating one for the update action and one for the insert action Then I dropped those so that if I run this script, I don't get the object already exists there.
And the next line of doing a delimit er swap, and that's so that we can use the semicolon in the body of the definition. Otherwise, this semicolon right here would cause an air.
And then I'm just walking through the standard create trigger syntax. So of course you need to get that syntax correct. Otherwise, the trigger create command won't work.
So create trigger
trigger name. And I like this name because it's the table name. It's the before keyword. It's the action, which is update, and it's what it's doing keyword that has something to do, what it's doing, precision. So just reading the trigger name gives you an idea of what it is doing.
And then before update on table name for each row began, and this is where we actually implement the code, this solves their problem,
which is set new amount. New dot amount equal to the rounding of new dot amount to two decimal places, and that is it that is done. That's all we need.
And then we end the definition with a dollar sign dollar signs
and then below that, we create the insert variant of that trigger. Again, we just get this syntax correct. And then the code is exactly the same. Said new amount equal to the rounding of new amount to two decimal places. And then we swapped back to the old semicolon delimit er at the end of that. And if we run this, it'll
run over and over
because of the way we've designed it.
I'm gonna go ahead and open up PM Tran, take a look at it. I will click out in one of the amount columns. Where is the column that are Trigger is focusing on. So we'll give it a test, would say to
and then I'm gonna save that,
and that should be com 0.56. So let's see.
And sure enough, it did. So the trigger is working correctly,
and that completes this lesson I hope to see in the next Thank you