Hi all.I am required to create a trigger on an existing table (say x) wherein we have attribute called as level (with accepted values as low,medium and high). Whenever a record is inserted into an existing table (x) with 'level' as 'high',the entire record should be copied to the new table (say y). Is this possible to accomplish using triggers ?
Yes, the trigger would be similar to this (I suppose you're using MySQL): DELIMITER $$ CREATE TRIGGER copy_high_record BEFORE INSERT ON X FOR EACH ROW BEGIN IF (NEW.level = 'high') THEN // your queries END IF; END$$ DELIMITER ; Code (markup):
here NEW is the alias for the existing table right? So my query would be something like this insert into Y (column1,column2) values (NEW.description,NEW.level); Code (markup): Am i right?
DELIMITER $$ CREATE TRIGGER copy_low_record AFTER INSERT ON ossec.data FOR EACH ROW BEGIN DECLARE X varchar(10); SET X=(select level from ossec.alert_levels where rule_id in(select rule_id from ossec.alert where id in (select max(id) from ossec.alert))); IF (X='Low') THEN Insert into ossec.data_critical (server_id,user,full_log,timestamp) values (NEW.server_id,NEW.user,NEW.full_log,NEW.timestamp); END IF; END$$ DELIMITER ; Please help me with this.I am required to insert the value in the 'data_critical' table if the output of the previous query (stored in variable X) is Low.