Using MySQL 5.1.x
Trying to add a trigger to a table:
DELIMITER $$
CREATE TRIGGER group AFTER INSERT ON dataTable
FOR EACH ROW BEGIN
UPDATE dataTable SET groupName = mid(longName,1,4) WHERE groupNAME IS NULL;
END$$
When I insert a record there is no update made. Is there a syntax error? Or can I not run the Update query on the after insert event?
UPDATE: There are 2 triggers on this table (a AFTER INSERT and BEFORE UPDATE).
In a MySQL trigger, you cannot invoke DML on the table that fires the trigger.
Related
Can I have a trigger to check any insert or update on whole database?
I tried this but it didn't work:
create trigger updateHistory after insert on database
Yaou need to elaborate the database into separate tables. And give the action. Eg:
CREATE TRIGGER updatehistory
BEFORE UPDATE ON `table,table2`
FOR EACH ROW
BEGIN
INSERT INTO
SET action = 'insert',
record = OLD.record,
changedat = NOW();
END
And as answered here, you cannot create both insert and update at the same trigger, but you can move the common code into a procedure and have them both call the procedure.
I need to update the status column of all the old rows of a table to false before/after inserting a new row into that table.
I have visited these but didn't solve my query.
MySQL - Trigger for updating same table after insert
DELIMITER $$
DROP TRIGGER update_old_status $$
CREATE TRIGGER update_old_status
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
DECLARE MYCOUNTER INT;
SELECT COUNT(*) INTO MYCOUNTER FROM table_name;
IF (MYCOUNTER > 0) THEN
UPDATE table_name SET status = false WHERE status = true;
END IF;
END $$
DELIMITER ;
Here what i have as error message:
Caused by: java.sql.SQLException: Can't update table 'table_name' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
You can't use a trigger to update the same table for which the trigger is running, because there's too high a risk of creating an infinite loop.
For example, an insert trigger updates its table, which runs another trigger on update, that inserts to the same table, which runs the insert trigger...
You can't and don't need to use a trigger for this. Instead, use a transaction and run the UPDATE before your INSERT in your application.
Pseudocode:
START TRANSACTION;
UPDATE table_name SET status = false;
INSERT INTO table_name ...values...
COMMIT;
I had run two threads, one for insert another one for update in MySQL. The two operations are performed concurrently, my problem was the insert operation will performed first (before insert the update operation will be restricted) before update operation.
create a new trigger, you use the CREATE TRIGGER statement. The following illustrates the syntax of the CREATE TRIGGER statement:
CREATE TRIGGER trigger_name trigger_time trigger_event
ON table_name
FOR EACH ROW
BEGIN
...
END;
Create a trigger after insert
CREATE TRIGGER occupy_trig AFTER INSERT ON occupiedroom
FOR EACH ROW
BEGIN
//update query
UPDATE BookingRequest
SET status = 1
WHERE idRequest = NEW.idRequest;
END
learn more about triggers at techonthenet.com
My trigger fails when i try to insert a new row in my table because Mysql doesn't support updating rows in the same table the trigger is assigned to. Does anyone have suggestions on a good workaround/alternative?
My trigger:
-- Trigger DDL Statements
DELIMITER $$
CREATE TRIGGER check_sequence
BEFORE INSERT ON data FOR EACH ROW
BEGIN
IF EXISTS(SELECT TRUE FROM data WHERE sequence = NEW.sequence) THEN
UPDATE data SET sequence=sequence+1 WHERE sequence >= NEW.sequence;
END IF;
END $$
DELIMITER ;
Error Sql that is displayed when i try to insert new row:
ERROR 1442 (HY000): Can't update table 'data' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thanks for answer,
In MySQL, You can't update (or insert in) table A in a trigger for table A.
http://dev.mysql.com/doc/refman/5.0/en/stored-program-restrictions.html
You can try a sproc and use a transaction, or consider PostgreSQL, which can do this.
MySql trigger is very interesting, but very tricky.
I have some problem, I want to run a trigger once after insert on .
I want to run my trigger once after rows inserted, is there anything like for each table``???
how to make this trigger run only once, but not for each row created.
CREATE
DEFINER=`root`#`localhost`
TRIGGER `mydb`.`leave_taken_trigger`
AFTER INSERT ON `mydb`.`leave`
FOR EACH ROW
BEGIN
set #lt:= (SELECT count(*) FROM `leave` where (staff_leave_application_staff_id_staff = new.staff_leave_application_staff_id_staff and leave_type_id_leave_type = new.leave_type_id_leave_type) and active = 1 );
INSERT INTO `leave_taken`(leave_type_id_leave_type, staff_id_staff, taken_days, updated)
VALUES (new.leave_type_id_leave_type, new.staff_leave_application_staff_id_staff, IFNULL(#lt,0), CURDATE())
ON DUPLICATE KEY UPDATE taken_days=IFNULL(#lt,0), updated = CURDATE();
END$$
I think you can't achieve your requirement with trigger.
Your trigger will run in every row created.
I suggest you to using stored procedure from your code after the INSERT has successfully completed.