1

I would like to do a trigger with a join included.

I need to, dont allow a mechanic to be inserted if he is marked as inactive. I have the table machanic with an attribute called MecActiv that can be active or inactive, also i have a table called reparation that have a foreign key to the table mechanic. If this attribute on mechanic is inactive, that mechanic cant be on the table reparation so i need a trigger with a join, i cant found how to do it.

Thanks!

P.S: I need the code in Mysql

4

1 に答える 1

0

実際の db スキーマは投稿していませんが、ここにあります。

あなたの場合は必要ありませんJOIN。トリガーは次のようになります

CREATE TRIGGER tg_reparation_insert
BEFORE INSERT ON reparation
FOR EACH ROW
  SET NEW.mechanic_id = 
  (
    SELECT IF(IFNULL(mecactiv, 0) = 0, NULL, NEW.mechanic_id)
      FROM mechanic
     WHERE id = NEW.mechanic_id
  );

挿入されている ID を持つメカニックが非アクティブ ( ) である場合、それは基本的に何をするかを示します。これは、制約違反にmecactiv = 0割り当てる場合です。これが機能するためには、明らかに、補償テーブルに制約を設ける必要があります。NULLmechanic_idNOT NULLNOT NULL

これがSQLFiddle のデモです。最後INSERTにコメントを外して、それが妨げられるかどうかを確認してください。

于 2013-06-10T22:36:09.790 に答える