Create a trigger, when a user to form to insert a row, will the department number of data on the basis of the original plus 1
CodePudding user response:
How to write code with PL/SQL statements
CodePudding user response:
SQL>
SQL> Col dept_id a10 format;
SQL> Col num a10 format;
SQL> The create table dept (dept_id int, dept_name varchar (10), num int);
The Table created
SQL> The create table emp (emp_id int, dept_id int, emp_name varchar (10));
The Table created
SQL> Create the trigger tri_emp_ins
2 before the insert on emp
3 for each row
4 the begin
5 update dept set num=num + 1 where dept_id=: new. Dept_id;
6 the end;
7/
The Trigger created
SQL> Insert into dept values (100, 'Dev, 0);
1 row inserted
SQL> Select * from dept.
DEPT_ID DEPT_NAME NUM
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
100 Dev 0
SQL> Insert into emp values (1, 100, "zhangsan");
1 row inserted
SQL> Select * from dept.
DEPT_ID DEPT_NAME NUM
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
100 Dev 1
SQL> Drop table dept purge;
Table dropped
SQL> Drop table emp purge;
Table dropped
SQL>
CodePudding user response: