当前位置:网站首页>Oracle triggers and packages

Oracle triggers and packages

2022-07-05 07:36:00 Mr. Li, a genius

Everything is inferior 、 Only reading is high
Articles are constantly updated , You can search by wechat 【 Xiaoqi JAVA interview 】 First time reading , reply 【 Information 】 Access to benefits , reply 【 project 】 Get the source code of the project , reply 【 The resume template 】 Get resume template , reply 【 Learning Roadmap 】 Get a learning roadmap .

 Insert picture description here


One 、 The concept of trigger

1、 The basic concept of triggers

The trigger itself is a piece of program code , Similar to stored procedures and functions , But unlike stored procedures and functions , Stored procedures and functions are saved in oracle In the database , If you want to execute, you need user calls . After the trigger is created , Stored as a separate object in oracle In the database , Decide whether to execute according to the pre-defined trigger event , As long as these trigger events occur , The trigger will automatically execute . in addition , Trigger cannot receive parameters .
Trigger usually consists of trigger head and trigger body , It can be divided into the following aspects .
1、 Object of action : Include data sheets 、 View 、 Databases and schemas .
2、 Triggering event : Refers to the event that can cause trigger execution . for example ,DML Statement that performs data operations on a data table or view insert、update、delete sentence ;DDL Statement 、 Modified or deleted cretae、alter、drop Statements and database system events , Include oracle The system starts or exits 、 Abnormal error, etc .
3、 The trigger condition : from when Clause specifies a logical expression , When the trigger event occurs , If the value of this logical expression is true when , The trigger will automatically execute .
4、 Triggering event : It refers to that the trigger instruction is executed before the trigger event occurs , Or after the trigger event .
5、 Trigger level or trigger frequency : It is divided into statement level and row level triggers . Statement level triggers are the default , Refers to after the trigger event occurs , The trigger executes only once ; Row level trigger means that the trigger event acts on each record , The trigger executes once .

2、 Classification of triggers

According to the application scope of trigger , Triggers can be divided into 3 Kind of :DML trigger 、instead of Trigger and system trigger .
1、dml trigger
In execution dml Statement , Can be defined as insert、update、delete operation , It can also be defined to trigger before or after the operation , It can also be specified as row level trigger or statement level trigger .
2、instead of trigger
It is also called a substitute trigger , It is oracle A trigger designed specifically for views . stay oracle In the database , Generally, you cannot directly perform general trigger operations on views created by more than two tables , If it has to be changed , Just use an alternative trigger .
3、 System triggers
stay oracle Trigger when the system event of the database occurs , For example, the system starts or exits 、 Abnormal error, etc , This system trigger is called a database trigger ; Or it happens ddl Statement , For example, execute create 、 Modified or deleted create、alter、drop Statement etc. , This trigger is called a mode trigger .

Two 、 Trigger creation

Basic grammar

create or replace trigger< Trigger Name >< Trigger time >< Triggering event >on< Table name >|< View name >|< Database name >|< Schema name >
[for each row]
[when< Conditional expression >]
begin
<pl/sql sentence >
end

The parameters are described as follows :
1、or replace Is an optional parameter , If the trigger to be created already exists in the database , Delete the original trigger first , Then rebuild the trigger , Or overwrite the original trigger .
2、 Trigger time includes before and after Two kinds of ,before The trigger is executed before the trigger event occurs ,after The trigger executes after the trigger event occurs .
3、 Triggering event , for example insert,update,delete,create,alter,drop etc. .
4、<pl/sql sentence > Is the trigger operation to be performed .

1、DML Trigger creation

dml The trigger is executing dml Statement , Can be divided into insert、update and delete operation , It can be defined to trigger before or after the operation , It can also be specified as row level trigger or statement level trigger .
1、 Sentence level dml Trigger creation
It's the default dml Trigger creation , Don't use for each row Clause . Statement trigger corresponds to dml Statement affects all rows in the table that meet the conditions , But the trigger only executes once , And no longer use when Conditional statements .

1、 Create trigger , When the student Table to add records 、 When updating records and deleting records , Determine whether it is a working period , If it's not working hours , No execution is allowed .

create or replace trigger tri_detect
	before insert or update or delete 	-- Define the trigger time and trigger event 
	on student
begin
	if to_char<sysdate,'HH24:MI'> not between '09:00' and '17:00';	-- Give the working time of judgment conditions 
		or to_char<sysdate,'DY','nls_date_lancuage=american'> in <'sat','sun'> then
			raise_application_error<-20005,' Not normal working hours , Cannot perform dml operation '>;	-- disqualification , Give tips 
	end if;
end;

2、 In the data table student Create trigger on , When inserting 、 When deleting or updating , Record operation log .

create or replace trigger tri_log
after insert or delete or update on student 	-- Define trigger event and trigger time 
declare
	var_user varchar2<20>;
	var_action varchar2<20>;	-- Defining local variables 
begin
	select distinct user into var_user from all_users;	-- Get users 
	if inserting then	-- Judge whether it is “ Insert ”
		insert into stu_log values<var_user,' Insert ',sysdate>;
	elsif deleting the	-- Judge whether it is “ Delete ”
		insert into stu_log values<var_user,' Delete ',sysdate>;
	else	-- Judge whether it is “ to update ”
		insert into stu_log values<var_user,' to update ',sysdate>;
	end if;
end;

2、INSTEAD OF Trigger creation

Create a view before creating , View associated data table ,student、teacher.

create view test_view as
select s.id,s.name,t.id,t.name
from student s,teacher t
where s.id=t.sid;

At this time, we will report an error when inserting data into the view , We can only create instead of Trigger to achieve .

1、 establish instead of Trigger has implemented modify view test_view The content in

create or replace trigger tri_view
	instead of insert
	on test_view
	for each row
declare
	var_row dept%rowtype;
begin
	select * into var_row from student where id=:new.id;
	if sql%found then
		insert into student<id,name,class> values <:new.id,:new.name,:new.class>;
	end if;
exception
	when no_data_found then
		insert into student<id,name> values<:new.id,:new.name>;
		insert into teacher<id,name,sid> values<:new.id,:new.name,:new.sid>;
end;

3、 System triggers

1、 When logging in or out , Add data to the log table

create or replace trigger user_login
	after logon on database	-- Login triggers 
begin
	insert into user_log<user_name,login_date> values <ora_login_user,systimestamp>;
end user_login;

4、 Disable and enable triggers

stay oracle In the database , Trigger can modify his state , Make it valid or invalid , That is, enable or disable
The syntax is as follows .
alter trigger Trigger Name [disable | enable]
Parameter description :disable Is the trigger disable parameter , Even if the trigger is in an invalid state ;enable Is the trigger enable parameter , Even if the trigger is in a valid state .
For example, the following disables triggers
alter trigger tri_ddl disable;

5、 View and delete triggers

Want to view all trigger information , You can use a data dictionary user_triggers, This data dictionary has many fields to view the names of all triggers 、 type 、 Table name 、 Owner and other information .

select trigger_name,trigger_type,table_name from user_triggers;

Delete trigger

drop trigger  Trigger Name 

3、 ... and 、 Package

1、 Package creation

Package consists of package specification and package body , Therefore, the creation of the package is also divided into two parts , They are the creation of package specification and package body .
1、 Creation of package specification
The package specification provides the interface of the application , Declare variables that can be used in the package in the package specification 、 data type 、 stored procedure 、 function 、 Abnormal etc. . However, functions and procedures only include prototype information , That is, only the declaration of the head , Does not include any implementation code , In fact, modern code is defined in the envelope . Here is the syntax .

create or replace package  Package name 
isias
 Type declaration  |  Variable declarations  |  Exception declaration  |  Process declaration  |  Function declaration 
end [ Package name ]

2、 Creation of inclusion
Include the implementation code of procedures and functions declared in the specification in the package , Besides , You can also declare other variables that are not declared in the package specification 、 type 、 The process 、 function , But they can only be used in bags , Cannot be called by other applications . Here is the grammatical format

create or replace package body  Package name 
isias
[< Internal variable declaration >]
[ The process of body ]
[ The body of the function ]
end [ Package name ]

2、 Package deletion

Deleting includes two steps , First delete the package body , Then delete the package specification . The grammar is as follows

drop package body  Package name 
drop package  Package name 

Four 、 summary

The relevant contents here have not been sorted out yet , The article continues to be updated later , Recommended collection .

The commands involved in the article must be typed several times each like me , Only in the process of knocking can you find out whether you really master the command .

You can search by wechat 【 Xiaoqi JAVA interview 】 First time reading , reply 【 Information 】 Access to benefits , reply 【 project 】 Get the source code of the project , reply 【 The resume template 】 Get resume template , reply 【 Learning Roadmap 】 Get a learning roadmap .

原网站

版权声明
本文为[Mr. Li, a genius]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050731206696.html