0

我有一张像这样的小桌子

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 答案的评论。

4

2 回答 2

2

你是对的,添加PRAGMA AUTONOMOUS_TRANSACTION不是解决方案。一种方法是使用 PEOPLE 上的触发器来维护表中每组的人数GROUPS(如果您没有GROUPS表,则可以添加一个):

  • 在 INSERT on PEOPLE: update GROUPS 后,将他们所在的组数加 1
  • 在 PEOPLE 上删除后:更新 GROUPS,从他们所在的组数中减去 1
  • 在对人进行更新后:更新组,将 1 添加到新组,从旧组中减去 1

然后你的 BEFORE INSERT 触发器不需要查看 PEOPLE 表,它可以查看 GROUPS:

Select people_count into g_count from groups where group = :new.group
for update;

请注意在事务完成之前for update锁定行的子句。GROUPS

于 2014-01-21T11:37:27.407 回答
0

您可以使用复合触发器。它看起来像这样:

CREATE OR REPLACE TRIGGER people_bis
   FOR INSERT ON people
COMPOUND TRIGGER

g_count NUMBER(4);
g_num NUMBER(4);
g_l NUMBER(4);
g_r NUMBER(4);

BEFORE STATEMENT IS
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;

   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;

END BEFORE STATEMENT;

BEFORE EACH ROW IS
BEGIN

   If :NEW.group = '' or :NEW.group is null Then
   :New.group := g_num;
   End if;
END BEFORE EACH ROW;

End people_bis;

请注意,很可能此代码无法按您的意愿工作,但它应该让您大致了解如何使用复合触发器。

于 2014-01-21T11:53:08.257 回答