我有一张像这样的小桌子
table People
( name VARCHAR(20) PRIMARY KEY
,group NUMBER(4)
);
而且我需要创建触发器(或触发器),这将允许以下规则起作用:
- 1 如果组中有超过 10 个名称,如果有人试图为该组插入下一个人,我需要引发错误。
- 2 如果 INSERT 带有组字段的 NULL 值,我需要将其分配给计数小于 10 的组。 - 3 如果所有组
中有 10 个名称,我需要生成下一个组号。
- 4 我需要避免变异表错误。
这是我到目前为止所做的:
CREATE OR REPLACE TRIGGER people_bis
BEFORE INSERT ON people
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
g_count NUMBER(4);
g_num NUMBER(4);
g_l NUMBER(4);
g_r NUMBER(4);
BEGIN
Select count(*) into g_count from people where group = :new.group;
If g_count > 9 Then
raise_application_error (-20003,'Group reached it limit, please choose other');
End if;
If :NEW.group = '' or :NEW.group is null Then
select count (*) into g_l from (select count(imie),group from people group by group having count(name) = 10);
select count (distinct group) into g_r from people;
if g_l = g_r then
select max(group)+1 into g_num from people;
else
select group into g_num from(select group, count(name) from people having count(name) < 10 group by group order by count(group) desc) where rownum < 2;
End if;
:New.group := g_num;
End if;
End people_bis;
上面的代码有效,但是当我从镜像表中选择插入时,例如
INSERT INTO people(name) select concat(name,'_next') from people_mirror;
结果是它超过了组的给定限制 (10)。我也知道使用PRAGMA AUTONOMOUS_TRANSACTION不是避免变异表错误的最佳方法,而且我知道如果我将行触发器拆分为语句触发器,我可以实现此功能,但我只是不知道如何获得它。
那么有人吗?;)
提前致谢。
- - - - - - - - - -编辑 - - - - - - - - - - - -
这些是有效的触发器,但我仍然对它们有疑问,因为它们都是 BEFORE 和行类型。
CREATE OR REPLACE TRIGGER people_bir1
BEFORE INSERT on people
FOR EACH ROW
DECLARE
V_count NUMBER(2);
BEGIN
If :NEW.group = '' or :NEW.group is null then
return;
end if;
insert into groups values(:New.group,1);
exception when dup_val_on_index then
Select count into v_count from groups where group = :New.group;
UPDATE groups set count = v_count+1 where group = :New.group;
END people_bir1;
CREATE OR REPLACE TRIGGER people_bir2
BEFORE INSERT on people
FOR EACH ROW
DECLARE
g_count NUMBER(2);
g_num NUMBER(2);
begin
if :NEW.group = '' or :NEW.group is null Then
select min(count) into g_count from groups;
if g_count = 10 Then
select max(group) into g_num from groups;
g_num := g_num+1;
Else
select min(group) into g_num from group where count = g_count;
End if;
:New.group := g_num;
Else
select count into g_count from groups where group=:New.group;
if g_count > 9 then
raise_application_error (-20003,'More then 10 people in group please select another');
end if;
end if;
end people_bir2;
因为太长了,我无法将其粘贴为@TonyAndrews 答案的评论。