触发根据另一个表

时间:2016-12-02 09:40:01

标签: c# database oracle triggers oracle-sqldeveloper

我有这些表格:

CREATE TABLE Functions
  (
    id_f NUMBER (5) NOT NULL,
    id_gFK NUMBER (5) NOT NULL
  ) ;

--id_gFK is foreign key from Salaries table

CREATE TABLE Salaries
  (
    id_g NUMBER (5) NOT NULL ,
    g1_g1 NUMBER (6) ,
    g1_g2 NUMBER (6) ,
    g1_g3 NUMBER (6) ,
    g2_g1 NUMBER (6) ,
    g2_g2 NUMBER (6) ,
    g2_g3 NUMBER (6) ,
    g3_g1 NUMBER (6) ,
    g3_g2 NUMBER (6) ,
    g3_g3 NUMBER (6)
  ) ;

--g1_g1 means - grade 1, gradation 1

CREATE TABLE Employee
  (
    id_e  NUMBER (5) NOT NULL ,
    id_fFK NUMBER (5) NOT NULL ,
    grade NUMBER (1) ,
    gradation NUMBER (1) ,
    salary NUMBER (6)
  ) ;

 --id_fFK is foreign key from Functions table

当我插入一名员工时,一切正常,他根据该栏目取薪。但是,我可以在Salaries表中编辑薪水。

例如

id_g=101, g1_g1=5000....g3_g3=1200

中的

Salaries id_f=201, id_gFK=101

中的

Functions id_e=1001, id_fFK=201, grade=1, gradation=1, salary=5000表中的

Employee(5000取自Salaries取自id_fFK, grade and gradation表。)

之后我会在Salaries表格中编辑该工资5000,例如4000,我希望此编辑能够在Employee表格中自动完成。

我确信我可以使用触发器执行此操作。我试过了,但没有。

可以任何人告诉我我必须使用哪种触发器以及如何使用?

谢谢!!!

1 个答案:

答案 0 :(得分:2)

请参阅以下示例,其中updatinginserting记录到emp_sal表,并通过employee将其插入trigger表。

CREATE OR REPLACE TRIGGER change_salary
   AFTER INSERT OR UPDATE ON emp_sal
   FOR EACH ROW
BEGIN

IF INSERTING THEN 

  INSERT INTO Employee (employee_id,first_name,salary)
  values (:new.eno,:new.ename,:new.sal);

END IF;

IF UPDATING THEN 
   UPDATE Employee
      SET salary = nvl(:NEW.sal,:old.sal)
      WHERE employee_id = :new.eno; 
 end if;

 END;

执行:

SQL>  select sal from emp_sal  where eno = 3 ;

       SAL
----------
     80006

SQL>  select salary from employee where employee_id = 3;

    SALARY
----------
     50000

SQL>  update emp_sal 
 set sal = 1234
 where eno = 3 ;   

1 row updated.

SQL> commit;

Commit complete.


SQL> select sal from emp_sal  where eno = 3 ;

       SAL
----------
      1234

SQL> select salary from employee where employee_id = 3;

    SALARY
----------
      1234

SQL>  insert into emp_sal(eno,ename,sal)
        values
       (9,'XING',40000);    

   1 row created.

SQL> commit;

    Commit complete.

SQL> select salary from employee where employee_id =9;  

    SALARY
----------
     40000

编辑:我不确定您的trigger是什么,但我确实看到数据设置错误。我读了你的requirement并按照自己的方式进行了工作和测试。见下文:

包含数据的表格:

CREATE TABLE Salaries
  (
    id_g NUMBER (5) NOT NULL PRIMARY KEY ,
    g1_g1 NUMBER (6) ,
    g1_g2 NUMBER (6) ,
    g1_g3 NUMBER (6) ,
    g2_g1 NUMBER (6) ,
    g2_g2 NUMBER (6) ,
    g2_g3 NUMBER (6) ,
    g3_g1 NUMBER (6) ,
    g3_g2 NUMBER (6) ,
    g3_g3 NUMBER (6)
  ) ;

Insert into SALARIES
 Values
   (101, 5000, 2000, 3000, 4000, 
    6000, 7000, 8000, 6000, 12000);
COMMIT;
--------------------------------

CREATE  TABLE Functions
  (
    id_f NUMBER (5) NOT NULL,
    id_gFK NUMBER (5) NOT NULL PRIMARY KEY,
    CONSTRAINT fk_sal
    FOREIGN KEY (id_gFK)
    REFERENCES Salaries(id_g)

  ) ;

Insert into FUNCTIONS
   (ID_F, ID_GFK)
 Values
   (201, 101);
COMMIT;  
--------------------------
  CREATE TABLE Employees
  (
    id_e  NUMBER (5) NOT NULL ,
    id_fFK NUMBER (5) NOT NULL ,
    grade NUMBER (1) ,
    gradation NUMBER (1) ,
    salary NUMBER (6),
    CONSTRAINT fk_id_emp
    FOREIGN KEY (id_fFK)
    REFERENCES Functions(id_gFK)
  ) ;

 Insert into EMPLOYEES
   (ID_E, ID_FFK, GRADE, GRADATION, SALARY)
 Values
   (101, 101, 1, 1, 5000);
COMMIT;

触发:

CREATE OR REPLACE TRIGGER change_salary_new
   AFTER UPDATE ON Salaries
   FOR EACH ROW
BEGIN   
   UPDATE Employees
      SET salary = nvl(:NEW.g1_g1,:old.g1_g1)
      WHERE id_e = :new.id_g; 
End ;

执行:

SQL> select id_g,g1_g1 from Salaries;

      ID_G      G1_G1
---------- ----------
       101       5000

SQL> select id_e,salary from  Employees;

      ID_E     SALARY
---------- ----------
       101       5000

SQL> update Salaries set g1_g1 = 10202 where id_g = 101;

1 row updated.

SQL> commit;

Commit complete.

SQL> select id_e,salary from  Employees;

      ID_E     SALARY
---------- ----------
       101      10202