oracle-trigger summary

1. Triggers are used in complex security check data confirmation and audit functions to complete data backup and synchronization

2, the trigger is a special stored procedure.

3. Trigger types: statement-level triggers, row-level triggers

4. A database trigger is a stored pl/sql program associated with a table.

5. The role of triggers: Whenever a specific data operation statement (insert, update, delete) is issued on the specified table, Oracle automatically executes the sequence of statements defined in the trigger.

6. The first trigger: completion function: every time a new employee information is successfully inserted, it will automatically print "successfully inserted new employee".

Create trigger saynewemp

After insert (meaning to execute plsql after the insert operation)

On emp (indicates which table to operate on)

Shifting

Begin

Dbms_output.put_line("Successfully inserted a piece of data!")

End;

7. Trigger application scenarios: complex security check data confirmation data audit data backup and synchronization

8. Syntax for creating triggers

Create or replace trigger trigger name

{before|after} specifies before or after the operation {insert|delete|update[of column name]} You can specify the column name when performing the update operation.

On table name [for each row[when(condition)]] is used to indicate what the type of trigger is. This statement represents row-level triggers, and without this statement, it represents statement-level triggers.

Plsql block

9. Statement-level triggers are executed once before or after the specified action statement, no matter how many rows the statement affects. Statement-level triggers target tables.

10. Row-level triggers are triggered for every record affected by the row trigger statement.

Use :old and :new pseudo-record variables in row-level triggers to identify the state of a value.

The difference between the two: for example, inserting multiple pieces of data into a table at one time

If you define a statement-level trigger, because it is based on a table, it means that the table is operated once, and it is triggered once.

If you define a row-level trigger, because it is on a record basis, that is, inserting a few records will start several times.

The when condition after For each row means that it will only be triggered when the corresponding condition is met.

Trigger use case 1: Complex security check case: Prohibit data insertion during non-working hours

Non-working hours: before and after get off work on weekends, such as before nine o'clock and after six o'clock, this situation belongs to statement-level triggers

Select to_char(sysdate,'day ') from dual; means use in('Saturday','Sunday') on weekends

Select to_number(to_char(sysdate,’hh24 ’)) from dual; not between 9 and 18

如下: Create or replace trigger securityemp

Before insert On

emp

Declare//No need to write without using custom variables

 Begin If(to_char(sysdate,’day ’)) in(‘星期六’,’星期天’) or to_number(to_char(sysdate,’hh24 ’)) not between 9 and 16 then

---Prohibit inserting data

Raise_application_error(-20001, 'It is forbidden to insert data during non-working hours');

End if;

End;

Trigger Use Case 2: Data Confirmation Case: Salary increases cannot be higher or lower

This trigger belongs to row-level triggers. Every record needs to be checked

:old and :new represent the same record

:old is the value representing the line before the operation

:new is the value representing the line after the operation

Create or replace trigger checksalary

Before update

On emp

For each row

Begin If :new.sal6000

then

Insert into the newly created table values(:new.empno || :new.empname || :new.sal);

End;

Trigger use case 4: data backup and synchronization

The master database and the slave database use triggers to achieve synchronous backup (no delay backup)

Simulate the master-slave database in the same database. The emp table represents the master database. The emp_backup table represents the slave database. It is used to store the backup of the A table. Create table emp_backup as select * from emp;

Create or replace trigger sync_salary

After update

On emp For each row

Begin -- automatically update the slave table when the master table is updated

Update emp_backup set sal=:new.sal where empno=:new.empno

End;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326305583&siteId=291194637