Jump to content

Triggers and resources


bepai

Recommended Posts

 

CREATE TRIGGER banmanagment AFTER UPDATE ON members
FOR EACH ROW BEGIN
UPDATE t1
SET open1 = NEW.TRUE, open2 = NEW.TRUE
WHERE temp_ban = 1 OR member_banned = 1
AND SET open1 = NEW.FALSE, open2 = NEW.TRUE
WHERE temp_ban =0
OR
member_banned =0
END
$$

 

That's what I'm playing with at the moment; I've never used triggers before so hopefully I'll get some insight. First question, other then what wrong with the above I'd like to ask if the trigger activates for all rows when any column is updated? Seems a bit of a waste of resources. Example, if a members table had 20,000 members and inside the same table had their post_count which obviously gets updated constantly, it would seem silly to have the trigger activate when it's purpose is for a specific field. Am I right in thinking this?

 

Link to comment
Share on other sites

I guess there's a timer on the edit button. Here's my progress so far to the above trigger..

 

CREATE TRIGGER bans AFTER UPDATE ON members FOR EACH ROW
UPDATE t1, (SELECT member_old_name,temp_ban,member_banned FROM members WHERE temp_ban =1 or member_banned =1) AS members
SET t1.open1 = NEW.True, t1.open2 = NEW.True
WHERE t1.usernamex = members.member_old_name

Link to comment
Share on other sites

First question, other then what wrong with the above I'd like to ask if the trigger activates for all rows when any column is updated? Seems a bit of a waste of resources. Example, if a members table had 20,000 members and inside the same table had their post_count which obviously gets updated constantly, it would seem silly to have the trigger activate when it's purpose is for a specific field.

 

MySQL only supports row level triggers, like most database platforms. You can check if a column was updated however using the NEW. and OLD. values:

 

IF OLD.col_name <> NEW.col_name THEN
    ...
END IF

 

That should allow you to only perform further statements in certain situations.

 

 

Am I right in thinking this?

 

Was that your second question?

Link to comment
Share on other sites

"trigger activates for all rows when any column is updated?"

 

I meant field, not column. I.e. if the post_count field is updated will that fire off the trigger regardless? I suppose a better question would be can you put triggers ON tablename.columnname so that it is only fired when an update in that column for any row that is updated? Basically, like my trigger shows, I want the trigger to run after an update to 'members_banned' or 'temp_ban' and depending on the values apply new values else where.

Link to comment
Share on other sites

I meant field, not column. I.e. if the post_count field is updated will that fire off the trigger regardless?

 

I suppose a better question would be can you put triggers ON tablename.columnname so that it is only fired when an update in that column for any row that is updated?

 

Contradicting yourself completely there. No, MySQL only supports row-level update triggers. As I said you can check which column was updated using IF statements, but you can't define the trigger to only execute when a certain column is updated.

Link to comment
Share on other sites

Ok need help with the trigger now...

 


CREATE
    TRIGGER `bu_bans` BEFORE UPDATE ON `members` 
    FOR EACH ROW BEGIN IF NEW.member_banned > OLD.member_banned OR  NEW.temp_ban > OLD.temp_ban THEN
update t1 set column1="True", column2="True";
ELSEIF NEW.member_banned < OLD.member_banned OR  NEW.temp_ban < OLD.temp_ban THEN
update t1 set column1="False", column2="True";
END IF;
END;
$$

 

This works for the most part but what I need to do now is specify the row that gets altered by the "member_id" that is in both 'members' and 't1'.

 

I Don't know how to select and utilize the data WHERE t1.member_id = members.member_id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.