触发每个预言家至少一个

问题描述 投票:0回答:1

我正在创建一个触发器,向我保证,每种性别至少有一位舞者。例如,如果我的那位桌上舞者只有2位舞者,那么他们将有1位男性和1位女性]

我有这些表

dancers (artistic_name, sex, fnac, real_name)
couples (name1, name2, fnac, times_together)

我有解决方案的原理就是这个触发器

create or replace trigger t_control
before insert or update on dancers
for each row
declare
    cnt number;
begin
    select count(*) into cnt
    from   dancers d
    where  :new.artistic_name != d.artistic_name
    and    :new.sex = d.sex;

    if cnt !=0 then
        raise_application_error('-20001', 'Not possible');
    end if;
end t_control;
/

但是我有一个问题,如果我先输入女性,然后再输入男性,则其余插入内容会跳过触发器

您将如何处理“至少一个”?

INSERT INTO dancers VALUES('artistic1', 'm', 'today', 'anna')    
INSERT INTO dancers VALUES('artistic2', 'f', 'today', 'paul') 

从这里开始,每次插入都会触发触发器

ORA-20001: Not possible
ORA-06512: at "FIDDLE_TQJRRVAQQOICTXMWSHDG.T_CONTROL", line 12
ORA-04088: error during execution of trigger 'FIDDLE_TQJRRVAQQOICTXMWSHDG.T_CONTROL'

INSERT INTO dancers VALUES('artistic3', 'f', 'today', 'mery') 
INSERT INTO dancers VALUES('artistic4', 'm', 'today', 'joan')
oracle plsql ddl
1个回答
0
投票

您的触发器正在检查是否有其他与您插入的性别相同的舞者。因此,当您插入第三个舞者时,您会收到错误消息。我认为这不是您要尝试的方法:确保每种性别至少有一位舞者。因此,您的触发器应:

  1. 检查这不是表中的第一行
  2. 如果还有其他行,则至少应有不同性别的人
  3. 它应检查'delete'DML,因此,删除舞者时,必须确保还有其他同性的舞者。这实际上是您要执行的操作吗?
© www.soinside.com 2019 - 2024. All rights reserved.