Oracle SQL 触发器使用添加列、注释
Oracle SQL trigger using add columns,comment
我应该在 Oracle SQL 中创建一个触发器
- 将
department_amount
列添加到位置 table
- 添加评论'Contains the amount of departments in the location'
- 创建一个触发器,每当来自部门的行 inserted/deleted 时,它将更新位置中的部门数量。
表格:
CREATE TABLE departments
(
department_id NUMBER(4) PRIMARY KEY,
department_name VARCHAR2(30) NOT NULL,
manager_id NUMBER(6),
location_id NUMBER(4) NOT NULL
);
CREATE TABLE locations
(
location_id NUMBER(4) PRIMARY KEY,
street_address VARCHAR2(40),
postal_code VARCHAR2(12),
city VARCHAR2(30),
state_province VARCHAR2(25),
country_id CHAR(2) NOT NULL
);
回答您的问题
将 department_amount 列添加到位置 table
alter table locations add department_amount number ;
添加评论'Contains the amount of departments in the location'
comment on column locations.deparment_amount is 'Contains the amount of departments in the location';
创建一个触发器,每当一行 inserted/deleted 来自部门时,它将更新 location 中的部门数量。
create or replace trigger trg_loc
after insert or delete on departments
declare
begin
merge into locations t
using ( select count(department_id) as dpt_amount, location_id as loc_id
from departments b
group by location_id ) s
on (t.location_id = s.loc_id)
when matched then
update set t.department_amount = s.dpt_amount ;
end;
/
下面有一个 db<>fiddle 数据示例和当您插入或删除部门时更新 locations
table 中的 department_amount
的触发器演示每个位置。
我认为这项任务太过不要那样做,没有任何意义。连训练都不行。
你需要做的是
- 检查
locations.department_amount
列是否存在。您可以通过查看系统视图 dba_tables
(或 all_tables
或 user_tables
(如果适用)来执行此操作。
- 如果该列不存在,请通过
ALTER TABLE
创建它。但是,这是 DDL,而不是 DML,因此您必须通过 EXECUTE IMMEDIATE
. 动态地 运行 它
- 如果您刚刚创建了专栏,请同时创建评论。
- 现在,在同一个触发器中,从
department_amount
中加或减 1。因此,您可以保证第一个操作(导致创建列的操作)已经更新了它。
第 1 点到第 3 点只需要发生在语句级别,而第 4 点应该发生在行级别。因此,为了避免其他触发器出现问题,此触发器应为复合触发器。
但是,如果可以在数据库中创建触发器,为什么不能也添加列及其注释呢?为什么必须编写触发器,使其必须检查该列是否存在?如前所述,这毫无意义,所以最好不要那样做。
我应该在 Oracle SQL 中创建一个触发器
- 将
department_amount
列添加到位置 table - 添加评论'Contains the amount of departments in the location'
- 创建一个触发器,每当来自部门的行 inserted/deleted 时,它将更新位置中的部门数量。
表格:
CREATE TABLE departments
(
department_id NUMBER(4) PRIMARY KEY,
department_name VARCHAR2(30) NOT NULL,
manager_id NUMBER(6),
location_id NUMBER(4) NOT NULL
);
CREATE TABLE locations
(
location_id NUMBER(4) PRIMARY KEY,
street_address VARCHAR2(40),
postal_code VARCHAR2(12),
city VARCHAR2(30),
state_province VARCHAR2(25),
country_id CHAR(2) NOT NULL
);
回答您的问题
将 department_amount 列添加到位置 table
alter table locations add department_amount number ;
添加评论'Contains the amount of departments in the location'
comment on column locations.deparment_amount is 'Contains the amount of departments in the location';
创建一个触发器,每当一行 inserted/deleted 来自部门时,它将更新 location 中的部门数量。
create or replace trigger trg_loc
after insert or delete on departments
declare
begin
merge into locations t
using ( select count(department_id) as dpt_amount, location_id as loc_id
from departments b
group by location_id ) s
on (t.location_id = s.loc_id)
when matched then
update set t.department_amount = s.dpt_amount ;
end;
/
下面有一个 db<>fiddle 数据示例和当您插入或删除部门时更新 locations
table 中的 department_amount
的触发器演示每个位置。
我认为这项任务太过不要那样做,没有任何意义。连训练都不行。
你需要做的是
- 检查
locations.department_amount
列是否存在。您可以通过查看系统视图dba_tables
(或all_tables
或user_tables
(如果适用)来执行此操作。 - 如果该列不存在,请通过
ALTER TABLE
创建它。但是,这是 DDL,而不是 DML,因此您必须通过EXECUTE IMMEDIATE
. 动态地 运行 它
- 如果您刚刚创建了专栏,请同时创建评论。
- 现在,在同一个触发器中,从
department_amount
中加或减 1。因此,您可以保证第一个操作(导致创建列的操作)已经更新了它。
第 1 点到第 3 点只需要发生在语句级别,而第 4 点应该发生在行级别。因此,为了避免其他触发器出现问题,此触发器应为复合触发器。
但是,如果可以在数据库中创建触发器,为什么不能也添加列及其注释呢?为什么必须编写触发器,使其必须检查该列是否存在?如前所述,这毫无意义,所以最好不要那样做。