Database Triggers - Part 2

20 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.18
List Price:  €95.97
You save:  €28.79
£55.86
List Price:  £79.80
You save:  £23.94
CA$97.87
List Price:  CA$139.82
You save:  CA$41.95
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,267.89
List Price:  R1,811.35
You save:  R543.45
Лв131.37
List Price:  Лв187.69
You save:  Лв56.31
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪259.10
List Price:  ₪370.16
You save:  ₪111.06
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$69,057.73
List Price:  CLP$98,658.13
You save:  CLP$29,600.40
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,776.20
List Price:  ISK kr13,966.60
You save:  ISK kr4,190.40
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,259.44
List Price:  ARS$100,374.93
You save:  ARS$30,115.49
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
Already have an account? Log In

Transcript

Hi. In this lesson, we go into more details related to triggers. In the previous lesson we understood what are the applications of trigger, how to create a before trigger after trigger and the difference between statement level and row level trigger. Now, let us start with a different kind of trigger called instead of trigger a before trigger is executed before the DML command is executed. After trigger is executed after the DML command is completed, but instead of trigger is used to execute some commands instead of the actual command. That means, it does not execute the given DML command.

Instead of the actual DML command it executes the trigger. So this can be created only on views not on tables. And it is used on views where we cannot directly manipulate view. If you can recollect from our discussion earlier about views, not all views are updatable. There are views that cannot be manipulated. That means they cannot manipulate base tables because they don't satisfy the rules related to making a view updatable.

We see an example here. And this is a view called employed job, which is taking data from two tables. Generally, when a view is based on multiple tables, it becomes read only. And this view is taking data from employees and jobs. And also in the context of joint views, the views that are based on join we can And played only the key preserved table. Key preserved table is the table whose primary key is still unique in the view.

So if you take this particular example, employees and jobs are the base tables and employees tables primary key that is employee ID is still unique here. But job ID, which is the primary key of A job stable is no longer unique, because there might be multiple employees belonging to the same job. So job ID is repeated in this view. So we can say employees is the key preserved table for employed job and jobs is not a key preserved table. So in this context, if I tried to update any column of employees table like first For example, that's fine, because you can update a key preserved table. But if I try to update a job title, which is coming from non key preserve table, because jobs is not a key preserve table, or racquel throws an error.

And you can see that clearly here. I'm trying to update a job title, which is coming from jobs. And I'm using my view employee job and that throws an error and the message very clearly says, you cannot modify a column that is related to non key preserved table in this case, jobs. So what is the remedy for this? I want to update a job title using employ job view. So if you want to update a column in a non key reserved table, Then you need to go for instead of trigger.

So this is the scenario. Our view is based on two tables, but implies is the key preserved table. So I can make changes to that through the view. But I can't make changes to jobs because that is not key preserved table. The solution to this is creating an instead of trigger. So create an instead of trigger on employ job because instead of triggers can be created on views.

And then I'm trying to bypass the actual command. So I'm not going to execute the personnel update command. For example, this is the original update command, and we don't want this to be executed. Whenever you give this command, we want Oracle to execute this block and that is our installation. trigger. When this block is executed there, I want to update employees table, change the first name to new first name.

And I want to change jobs table and set the job title to new job title. So, normally you cannot update jobs table using this view. But with the help of instead of trigger on employ job, I'm able to update both employees first name and the job title. job title is not updatable otherwise, but with the help of instead of trigger we are able to do that. So this is the application of instead of trigger. So if you create the trigger and give this command, it works, but if you try to do the same without creating any incident of trigger, then you get the error.

Saying cannot modify the column which is related to non key preserved table. So instead of triggers are quite different from normal triggers, we have seen before and after triggers before, but those two were executed before and after the actual DML command respectively. But instead of trigger is executed, bypassing the actual DML command. So pretty powerful, but it's only in the context of views. Then, at times when we have a single trigger fired for multiple DML commands, let us say I have a trigger which is executed both in case of insert and update. And then I want to know whether the trigger is fired because of inserting our because of updating Well, that's the reason why Oracle is providing three key words which you can use in the context of your trigger.

So inside the trigger, if you say inserting, it returns true if the trigger is fired because of insert command. And it returns true if you're executing update command and your trigger is fired because of update command for updating keyword. And deleting keyword will return true if the trigger is fired because of delete. Let's talk about an example related to inserting, updating and deleting. I want to create a trigger that is going to log all the changes you make to employees table. So whenever you're trying to insert a row I want to write into implies log with employee ID and remember for insertion We have only new ID no old ID.

So we use new and we put the system date and time and I, I assume there is a table called employee log with the three columns, employee ID, date and time. And then the kind of operation so that's going to be I in case of insertion. If it is not insertion, if the trigger is fired because of we updating implies table then this is true and we are going to put you as the operation and we again use the employee ID and sis date. And here you can use either new or old because update is providing both correlation names. But when it comes to the last one, we are trying to put D and we must use old because we don't have any new employee ID when we are Dealing with the deletion. So you have to be careful about the correlation names, because the operations dictate what correlation names are available to us.

So this is an example for inserting, updating and deleting keywords which are using to know whether the operation is insert or update or delete. Now, there might be cases where you want to delete a trigger all together because it's no longer relevant. But they might be also cases where your intention is not to drop the trigger all together, but only to disable it for the time being. Why Why do I disable a trigger? Well, you disable the trigger because you strongly feel in the current context that trigger need not be executed. If you feel the trigger need not be executed because the change Just you're about to make are all valid changes, no need for trigger to verify the changes, then you can disable trigger to improve performance.

It's all about performance. Because if I'm trying to change something like thousands of rows, I don't want that trigger to be fired. And a lot of time is spent on executing trigger for each row. So I would like to disable the trigger. Before I execute a command like this, which is changing the first name, and I'm just changing the first name to init cap. That means every first letter of the word is in uppercase, remaining in lowercase.

This is not an operation that could inject any invalid data. So before I do this, I want to disable the triggers related to imply stables updation Well, in such a case You just need to go and use disable option, either with altered trigger. In that case, you give explicitly a particular trigger name. Or you can disable all the triggers related to a table. When you say ALTER TABLE, employees disappear, disable all triggers, all triggers are disabled, anytime later, you can re enable all the triggers. And if you want a single trigger to be disabled, just give the trigger name, disable and perform whatever operations you want to perform.

And again, you can re enable the trigger. Of course you got to be the owner of the trigger to do that, not somebody else. So this is how you can disable and enable triggers depending on your requirement. Of course if you want to drop the trigger because you feel the business rule that you make In the trigger is no longer applicable. For example, we didn't want anyone to make changes on Sunday. But for some reasons that is no longer relevant, we can drop the trigger altogether.

Then, in the recent past, we got a very powerful kind of trigger called compound trigger. This is one trigger that can perform four different actions at four different events. Like before row before statement after row after statement. Actually, this would need four different triggers. In olden days, if you had to create a trigger for before role level, and then before statement, you would have to have two different triggers. The problem with the two different triggers, they are two different PL SQL blocks.

So if I have to access some data that Common to these two, it was difficult. So compound trigger is saying I put all of them in one PL SQL block. So the before roll level before statement, and after roll level, and after statement, we are going to put all these four in one block. And then there you can have some data also if you like. And the advantage is all these blocks get access to this data. So it's one PL SQL block with four different sub blocks, and all of them are having access to some variables.

So this is ideal for implementing some complex business logic. This is called compound trigger. It's one trigger with four different sub blocks and one for before row one for after row. And one for before statement, and the last one for after statement. So where do we use this? Do we need this in any case, let us see a use case here.

Well, this is the syntax for that, you just need to give compound trigger. And that makes a tech compound trigger. And here are the declarations. And these are the blocks first to block and second block. And third, and fourth, these blocks can have the code and all these blocks can also access the data that you declare here. All the variables that are there that are declared here can be accessed from anywhere.

Now here is my use case. I don't want to increase the salary of employees by more than 10,000 in a single update command. So in a single change, I don't want to actually cede the total hike of 10,000 How do I do that? Because this is difficult if you have to go without compound trigger, but compound trigger made it simple. So here we are, we create a variable called Total and this total variable is set to zero in before statement trigger. So before the statement, the UPDATE statement is executed total is zero.

And then before each row I have nothing to do, if there is nothing to be done in a particular sub block, you can just ignore it but you have to give now without that it is invalid. You can't leave any block totally empty. So you can just give now statement, then bought in after row. You see after each row, we want to take the difference between the new salary an old salary, and that is added to total and put into the total variable. So this is where we are adding the hike to total. And now comes the last one the after statement.

This is where I want to know whether the total is exceeding 10,000. If so, I throw an error and stop the process, some saying total hike in salary cannot cross 10,000. So this is the use case where you can apply compound trigger. Because if you want to do the same thing without a compound trigger, it would be I don't say impossible, but would be much more difficult than this. Now, let us see how we can apply this and here is my compound trigger. I'm trying to make some change and here if I try to make a change like this, so is this single Update command in which we are increasing the total salary of all the employees by more than 10,000.

Of course, because we are going with the first 10 employees, and we are increasing the salary of each employee by 2000. So obviously, the total hike is going to exceed 10,000. But without a trigger, this will go ahead and it is updating 10 rows. But now I need to go and roll back because I don't mean to make such a change done. And now let's create the trigger if I create the trigger successfully, and if I now go and execute this, it's trying to increase the salary of each employee by 2000. And it is updating 10 employees salary that's going to be 20,000.

But that's bigger than what we can allow with Because we can allow only 10,000 So, this is going to be aborted because the trigger fails go ahead try There we are. So, total hiking salary cannot cross 10,000 that is the message and this change is aborted. That means, the compound trigger is what is failing, and as a result the whole operation is aborted. So, compound triggers can play a very important role, relatively advanced, but you may need it in some specific cases. So, that's about the compound trigger instead of trigger. So, this is where we talked about how to raise the salary by 500.

That's okay because 500 is not going to cross the 10,000 threshold we said, but if you try to increase salary by 2000 for 10 months It's going to cross that. So, this is how you can test it this is allowed, this is valid, but this is not allowed because this is crossing the limit. So in this lesson we started with instead of trigger instead of trigger is used only with the views, specifically the views which are not allowing you to update base tables. You can overcome that limitation by using insert of triggers. Then you can know whether the trigger is fired because of inserting or updating or deleting using those keywords. Anytime you can enable or disable a trigger, you have to decide whether to disable or not to improve performance in some cases.

If you want, you can, anytime you can re enable it. Drop a trigger if it is not making sense anymore. compound trigger is one trigger with the four events before row before statement after row after statement. And we have seen how we can use it in order to prevent a change to salaries in such a way the total hike is crossing 10,000. So this is about triggers. And we have seen it in two parts, because triggers are pretty important to maintain integrity of data.

That's all for now. See you in the next lesson.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.