Oracle Database SQL statement trigger instance description, oracle instance description

Source: Internet
Author: User

Oracle Database SQL statement trigger instance description, oracle instance description

An example of the oracle Database SQL statement trigger

/**-----------------------

* Statement-level triggers

*/

-- Create emp copy emp1

Create table emp1 as select * from emp where 1 = 2;

-- Insert trigger

Create or replace trigger trg_emp1_insert_output_number

After insert on emp1

Declare

Num number (10 );

Begin

Select count (*) into num from emp1;

Dbms_output.put_line (num );

End;

/

--

Insert into emp1 select * from emp where deptno = 10;

Insert into emp (empno, ename, sal) values (111, 'ss', 5000 );

-- Update trigger

Create or replace trigger trg_emp_update

After update on emp

Declare

V_sal number;

Begin

Select avg (sal) into v_sal from emp;

Dbms_output.put_line (v_sal );

End;

/

--

Update emp set sal = sal + 100;

-- Insert, update, or delete

Create or replace trigger trg_emp1

After insert or update or delete on emp1

Declare

Num number (10 );

V_sal number (7,2 );

Begin

If inserting then

Select count (*) into num from emp1;

Dbms_output.put_line ('number of students: '| num );

Elsif updating then

Select avg (sal) into v_sal from emp1;

Dbms_output.put_line ('average salary: '| v_sal );

Else

For I in (select deptno, count (*) num from emp1 group by deptno) loop

Dbms_output.put_line (I. deptno | 'count: '| I. num );

End loop;

End if;

End;

/

-- Insert

Insert into emp1 select * from emp where deptno = 10;

-- Update

Update emp1 set sal = sal + 100;

-- Emp2

Create table emp2 as select * from emp where 1 = 2;

-- Disable operations on the emp2 table during non-work hours

Create or replace trigger trg_emp2

Before insert or update or delete on emp2

Begin

If to_char (SYSDATE, 'hh24: Mi') not

'08: 00' and '18: 00' or to_char (SYSDATE,

'Dy ', 'nls _ DATE_LANGUAGE = AMERICAN ')

IN ('sat ', 'sum ')

Then

Raise_application_error (-20015, 'Operation only during working time ');

End if;

End;

/

-- Insert

Insert into emp2 select * from emp where deptno = 10;

-- Update

Update emp2 set sal = sal + 100;

-- Emp3

Create table emp3 as select * from emp;

/**-----------------------

* Row-sentence triggers

*/

Create or replace trigger trg_emp3

After update of sal on emp3

For each row

Begin

If updating then

Dbms_output.put_line ('Update No.: '|: new. empno | 'Update payby' |: old. sal | 'updated payby' |: new. sal );

End if;

End;

/

-- Update

Update emp3 set sal = sal + 100 where empno = 7934;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.