如何在Oracle PL / SQL中编写删除触发器 - 坚持识别“匹配行”

时间:2014-10-23 17:01:25

标签: plsql triggers

我有一个我正在编写的触发器,一旦我删除了一行,我想删除另一个表中的相应行(common_cis.security_function)。

,源表格为party.security_function

以下是common_cis.security_function中的列:

URL
SCRTY_FUNC_NAME
SCRTY_FUNC_DESC
IDN
CREATE_TMSTMP
CNCRCY_USER_IDN

以下是party.security_function中的列:

UPDATE_USER_SRC_SYS_CD
UPDATE_USER_ID
UPDATE_TS
SCRT_FUNC_NM
SCRT_FUNC_DESC
CREAT_USER_SRC_SYS_CD
CREAT_USER_ID
CREAT_TS

到目前为止我所拥有的是:

delete from common_cis.security_function   CCSF

                                             where CCSF.SCRTY_FUNC_NAME = :new.SCRT_FUNC_NM;

这是正确的想法吗?或者我使用某种行ID?

感谢

1 个答案:

答案 0 :(得分:2)

我认为你应该使用完整性约束,即具有“ON DELETE CASCADE”条件的外键约束。 这是一个示例,但首先检查您的架构中是否有我使用的名称的表:

-- create tables:
create table master_table(
URL             varchar2(1000),
SCRTY_FUNC_NAME varchar2(100),
SCRTY_FUNC_DESC varchar2(1000));

create table detail_table(
SCRT_FUNC_NM   varchar2(100),
SCRT_FUNC_DESC varchar2(1000),
UPDATE_USER_ID number,
UPDATE_TS      varchar2(100));

-- add primary key and foreign key constraints:
alter table master_table add constraint function_pk primary key (SCRTY_FUNC_NAME);

alter table detail_table add constraint function_fk foreign key (SCRT_FUNC_NM) references master_table (SCRTY_FUNC_NAME) on delete cascade;

-- fill tables with data:
insert into master_table
values ('url number 1', 'sec function #1', 'description of function #1');

insert into detail_table
values('sec function #1', 'description', 1, '123abc');

insert into detail_table
values('sec function #1', 'description', 2, '456xyz');

-- check tables: first contains 1 row and second - 2 rows
select count(*) from master_table;

select count(*) from detail_table;

-- delete rows from first table only:
delete from master_table;

-- check tables once again - both are empty:
select count(*) from master_table;

select count(*) from detail_table;

-- clear test tables:
drop table detail_table;

drop table master_table;