简体   繁体   English

是否可以在Oracle 12c中的另一个表上使用触发器来填充表

[英]Is it possible to populate table using trigger on another table in oracle 12c

Using oracle 12c, I have a table for employee and a table for managers, if the newly inserted employee salary >=5000 then he/she is considered manager. 使用oracle 12c,我有一个用于员工的表和一个用于经理的表,如果新插入的员工薪水> = 5000,则他/她被视为经理。 So I'd like to create trigger on table employee that checks if the salary of the newly inserted employee >=5000 this row should be duplicated in the manager table. 因此,我想在表employee上创建触发器,以检查新插入的雇员的薪水是否> = 5000,该行是否应在manager表中重复。 IS this possible? 这可能吗? If yes, could you simply give me the right syntax. 如果是,您能不能给我正确的语法。

Some general words first: This could be considered bad database design. 首先说一些通俗的话:这可能被认为是不好的数据库设计。 If you consider an employee beyond a certain salary a manager, this almost screams for a column in the same table, either physical or virtual. 如果您将某个薪水超过某个薪水的员工视为经理,这几乎在同一表中的物理或虚拟列中大喊大叫。 For example, it could look like this: 例如,它可能看起来像这样:

CREATE TABLE employees (
  id          NUMBER,
  first_name  VARCHAR2(10),
  last_name   VARCHAR2(10),
  salary      NUMBER(9,2),
  is_manager  as (case when salary >= 5000 then 1 else 0 end)
  CONSTRAINT employees_pk PRIMARY KEY (id)
);

If you still want to use a trigger and a second managers table, it could work like this: 如果您仍然想使用触发器和第二个manager表,则可以像这样工作:

CREATE OR REPLACE TRIGGER trig_emp_insert
AFTER INSERT
   ON employees
FOR EACH ROW

BEGIN

    if (:new.salary >= 5000) then
        insert into managers (...) values (...)
    end if;

END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM