简体   繁体   English

在Oracle Sql数据库上触发

[英]Trigger on Oracle Sql database

I have some issues to resolve on my sql database: 我在sql数据库上有一些问题要解决:

  • ensure that a player can only have 2 yellow cards or one red 确保玩家只能有2张黄牌或1张红牌
  • a team can not have more than 5 foreign players (player who nationality is differet from French) 一个团队最多只能有5位外国球员(国籍与法国国籍不同的球员)

My model is something like that: 我的模型是这样的:

Team
-------
team_id (PK)
name

Player
-------
player_id (PK)
team_id (FK)
name
number_yellow_cards
number_red_cards
nationality

I think that trigger can resolve my problems. 我认为触发器可以解决我的问题。 Here is my trigger, but doesn't work in any situation (insert/update) 这是我的触发器,但是在任何情况下(插入/更新)都无效

create or replace TRIGGER foreign_players
Before Insert OR UPDATE on Player
FOR EACH ROW 
DECLARE nr_foreign_players INTEGER;
Begin

  IF INSERTING THEN
    SELECT Count(*) INTO nr_foreign_players
    FROM
        Team T
        INNER JOIN Player P ON P.team_id = T.team_id
    WHERE 
        P.team_id =  :new.team_id 
        AND P.nationality <> 'French';

    if(nr_foreign_players > 5) then
        Raise_Application_Error(-20000,'Error insert' ); 
    End If;
  END IF; --INSERTING

  IF UPDATING THEN
    SELECT Count(*) INTO nr_foreign_players
    FROM
        Team T
        INNER JOIN Player P ON P.team_id = T.team_id
    WHERE 
        P.team_id =  :new.team_id 
        AND P.nationality <> 'French';

    if(nr_foreign_players > 5) then
    Raise_Application_Error(-20000,'Error update' ); 
    End If;
  END IF; --UPDATING


End;

Anyone can help me? 有人可以帮助我吗?

This can't a 100% sure answer, as you did not exactly tell us what doesn't work in any situation . 这并不是100%肯定的答案,因为您没有完全告诉我们在任何情况下都不起作用

I see several issues with your trigger : 我发现您的触发器有几个问题:

  1. The logic to ensure that a team can not have more than 5 non-French players seems correct, however why do you duplicate it for INSERT and UPDATE ? 确保一支球队不能拥有超过5名非法国球员的逻辑似乎是正确的,但是为什么要为INSERTUPDATE复制它呢? This can be simplified. 这可以简化。

  2. You are not at all verifying that a player can only have 2 yellow cards or one red 您根本无法验证玩家只能拥有2张黄牌或1张红牌

Here is an updated SQL code that should get the work done. 这是应该完成工作的更新的SQL代码。 It is performing each check one by one, raising application exception as soon as one check fails. 它会逐个执行每个检查,一旦一项检查失败,就会引发应用程序异常。 No distinction is made between INSERT and UPDATE operations, as the logic is exactly the same for both operations. INSERTUPDATE操作之间没有区别,因为这两个操作的逻辑完全相同。

CREATE OR REPLACE TRIGGER foreign_players
BEFORE INSERT OR UPDATE ON Player
FOR EACH ROW 
DECLARE 
    nr_foreign_players INTEGER;
BEGIN

    IF NVL(:new.number_yellow_cards, 0) > 2 THEN
        Raise_Application_Error(-20000, 'Too many yellow cards' ); 
    END IF;

    IF NVL(:new.number_red_cards, 0) > 1 THEN
        Raise_Application_Error(-20000, 'Too many red cards' ); 
    END IF;

    SELECT Count(*) INTO nr_foreign_players
    FROM
        Team T
        INNER JOIN Player P ON P.team_id = T.team_id
    WHERE 
        P.team_id =  :new.team_id 
        AND P.nationality <> 'French';
    IF nr_foreign_players > 5 THEN
        Raise_Application_Error(-20000, 'Too many foreign players' ); 
    END IF;

END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM