I created a trigger like:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = '0';
END IF ;
It works as expected, when I update any row in the table_1 the one with key=0 in the other table is updated.
Since there is a field key in both tables, I want to update in the second table to row who has the same key of the updated row in table_1. I can't figure out how to use table_2.key inside the IF block.
You can use the new or old keyword (in case you change the key value, you may want to use one or the other). It let's you access all the values from the record that was updated on table_1:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = new.key;
END IF ;
You can see an example on this fiddle
try this:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = old.key;
END IF ;
Related
I have these two tables
**Table tb_data**
tb_id
timestamp
pagid
proid
status
(and many more)
**Table tb_units**
pag_id
pag_sn
user
latest_profile
latest_status
latest_feedback
latest_timestamp
Whenever a new row is created in tb_data, i would like some values updated in tb_units. In tb_units pag_id is unique and every number only exists once.
How do I trigger this, so the new values in tb_data are updated in tb_units? pagid equals pag_id and the corresponding values proid should update latest_profil, status should update latest_status, timestamp should update latest_timestamp
In the end I would like to end up with the latest pagid input to tb_data to be available in tb_units, since tb_data will contain multiple rows from the same pagid
I hvae tried several different aproaches, and have read a lot of examples, but I just don't get how these triggers work!
Latest example, that doesn't work:
CREATE TRIGGER update_units
AFTER INSERT ON tb_data
BEGIN
UPDATE tb_units
SET latest_profile = tb_data.9C,
latest_status = tb_data.91
WHERE pag_id = tb_data.86;
END
Not sure but something like this should work
CREATE TRIGGER update_units
AFTER INSERT ON tb_data
BEGIN
UPDATE tb_units
SET latest_profile = new.proid,
latest_status = new.status
WHERE pag_id = new.pagid;
END
suppose for the table student if insert query is executed as INSERT INTO student(id,name) VALUES (1,'sumit');.Just after insertion of the row I want the recently inserted row's name field value to be capitalised using trigger.I searched for it every where but couldn't get working code,please some body help?
You can write the after insert and update trigger on table.
CREATE TRIGGER lcase_insert BEFORE INSERT ON my_table FOR EACH ROW
SET NEW.name = LOWER(NEW.name);
CREATE TRIGGER lcase_update BEFORE UPDATE ON my_table FOR EACH ROW
SET NEW.name = LOWER(NEW.name);
here NEW.name is your name inserted in table.
I want to do simple trigger on delete in table X which will change value in table Table which has the same id.
UPDATE Table T
SET T.spots = T.spots +1
Where T.id = delete.id
But it doesnt work. I am not sure if "delete.columnName" works.
As mentioned by fejese in the comments, use
UPDATE Table T
SET T.spots = T.spots +1
Where T.id = OLD.id -- Use OLD.id instead of delete.id
I got MySQL update query like this :
"INSERT INTO $tbl_name
(status, name, ...)
VALUES
('$status', '$name'...)
ON duplicate KEY UPDATE
status=values(status), name=values(name)...
";
It have a lot of table columns so to keep it short I`ll get right to the issue:
Instead of updating the current table row it duplicates it with a new ID.
Note : There are unique fields in the row. Any ideas what am I doing wrong ?
Check existence before doing any Insert/Update
IF (SELECT 1 = 1 FROM Table WHERE Key=...) THEN
BEGIN
UPDATE Table Set status=values(status), name=values(name)... WHERE Key=...;
END;
ELSE
BEGIN
INSERT INTO Table (FieldValue) VALUES('');
END;
END IF;
Why not to use
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
?
Based on my previous post found here im able to insert the values to the 2nd table when the status on first table changes, but it keeps adding indiscriminately, i need to check if the submit_id has already been inserted into the 2nd table and then update the fields not insert it gain, how would i do that check before the trigger is executed?
Because the new.status and old.status refer to the row being edited not the row on table it's being inserted into, how can i compare that and insert or update if it already exists,
Thanks
You can use INSERT INTO ... ON DUPLICATE KEY UPDATE syntax for that
If order for it to work properly you have to have a UNIQUE constraint on submitId column in your second table (let's call it students).
ALTER TABLE students ADD UNIQUE (submitId);
Now an improved version of a trigger
DELIMITER $$
CREATE TRIGGER tg_au_submissions
AFTER UPDATE ON submissions
FOR EACH ROW
BEGIN
IF NEW.status = 1 THEN
INSERT INTO students (submitId, studentName, submitDate, contacts, email)
VALUES (NEW.submitId, NEW.studentName, NEW.submitDate, NEW.contacts, NEW.email)
ON DUPLICATE KEY UPDATE
studentName = VALUES(studentName),
submitDate = VALUES(submitDate),
contacts = VALUES(contacts),
email = VALUES(email);
END IF;
END$$
DELIMITER ;
Here is SQLFiddle demo