-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPLSQL_41.sql
More file actions
107 lines (97 loc) · 4.27 KB
/
Copy pathPLSQL_41.sql
File metadata and controls
107 lines (97 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---------------------------------------------------------------------------------------------
-------------------------------:NEW & :OLD QUALIFIERS IN TRIGGERS----------------------------
---------------------------------------------------------------------------------------------
create or replace trigger before_row_emp_cpy
before insert or update or delete on employees_copy
referencing old as O new as N
for each row
begin
dbms_output.put_line('Before Row Trigger is Fired!.');
dbms_output.put_line('The Salary of Employee '||:o.employee_id
||' -> Before:'|| :o.salary||' After:'||:n.salary);
end;
---------------------------------------------------------------------------------------------
--------------------------------USING CONDITIONAL PREDICATES --------------------------------
---------------------------------------------------------------------------------------------
create or replace trigger before_row_emp_cpy
before insert or update or delete on employees_copy
referencing old as O new as N
for each row
begin
dbms_output.put_line('Before Row Trigger is Fired!.');
dbms_output.put_line('The Salary of Employee '||:o.employee_id
||' -> Before:'|| :o.salary||' After:'||:n.salary);
if inserting then
dbms_output.put_line('An INSERT occurred on employees_copy table');
elsif deleting then
dbms_output.put_line('A DELETE occurred on employees_copy table');
elsif updating ('salary') then
dbms_output.put_line('A Updating occurred on the salary column');
elsif updating then
dbms_output.put_line('An UPDATE occurred on employees_copy table');
end if;
end;
---------------------------------------------------------------------------------------------
------------------------USING RAISE_APPLICATION_ERROR PROCEDURE WITH TRIGGERS----------------
---------------------------------------------------------------------------------------------
create or replace trigger before_row_emp_cpy
before insert or update or delete on employees_copy
referencing old as O new as N
for each row
begin
dbms_output.put_line('Before Row Trigger is Fired!.');
dbms_output.put_line('The Salary of Employee '||:o.employee_id
||' -> Before:'|| :o.salary||' After:'||:n.salary);
if inserting then
if :n.hire_date > sysdate then
raise_application_error(-20000,'You cannot enter a future hire..');
end if;
elsif deleting then
raise_application_error(-20001,'You cannot delete from the employees_copy table..');
elsif updating ('salary') then
if :n.salary > 50000 then
raise_application_error(-20002,'A salary cannot be higher than 50000..');
end if;
elsif updating then
dbms_output.put_line('An UPDATE occurred on employees_copy table');
end if;
end;
---------------------------------------------------------------------------------------------
--------------------------------USING UPDATE OF EVENT IN TRIGGERS----------------------------
---------------------------------------------------------------------------------------------
create or replace trigger prevent_updates_of_constant_columns
before update of hire_date,salary on employees_copy
for each row
begin
raise_application_error(-20005,'You cannot modify the hire_date and salary columns');
end;
---------------------------------------------------------------------------------------------
----------------------------------USING WHEN CLAUSE ON TRIGGERS------------------------------
---------------------------------------------------------------------------------------------
create or replace trigger prevent_high_salary
before insert or update of salary on employees_copy
for each row
when (new.salary > 50000)
begin
raise_application_error(-20006,'A salary cannot be higher than 50000!.');
end;
set serveroutput on;
update employees_copy set salary = salary + 100;
delete from employees_copy;
update employees_copy set salary = salary + 100
where employee_id = 30;
alter table employees_copy disable all triggers;
/
update employees_copy set salary = salary + 100
where department_id = 30;
/
delete from employees_copy;
/
desc employees_copy
/
insert into employees_copy select * from employees;
/
update employees_copy set salary = 60000;
/
update employees_copy set hire_date=sysdate;
/