How to insert before update query in mysql? - mysql

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

Related

Mysql trigger on update or insert on any table of database

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.

How to UPDATE ALL OLD ROWS in A TABLE with AFTER/BEFORE INSERT ON trigger IN THE SAME TABLE in mysql?

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;

Can we have single trigger for multiple tables in MySQL

Can I have a single trigger for multiple tables in MySQL? I have to perform same task after inserting either of the table_1 or table_2 e.g :
CREATE TRIGGER trigger-1_4_task1
AFTER INSERT ON `table_1`
FOR EACH ROW
BEGIN
.....task1
END //
CREATE TRIGGER trigger-2_4_task1
AFTER INSERT ON `table_2`
FOR EACH ROW
BEGIN
.... same task as task1
END //
Can I combine two above trigger like:
CREATE TRIGGER trigger_4_task1
AFTER INSERT ON `table_1` OR `table_2`
FOR EACH ROW
BEGIN
..... task1
END//
Thanks
Can I have a single trigger for multiple tables in MySQL?
No.
But multiple triggers could invoke the same stored procedure.

mysql trigger - is there anything like FOR EACH TABLE

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.

MySQL - AFTER INSERT TRIGGER does not run UPDATE query?

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.