MySQL AFTER UPDATE Trigger Not Firing - mysql

I am trying to set-up a trigger that will essentially archive information based on when a certain column is updated. What this should be doing is moving schedule_id and login from the schedule table to the schedule_archive table every time the term_date_schedule column is changed. However, that isn't happening. I am extremely new to MySQL, so I am probably missing something obvious.
CREATE DEFINER=`user`#`%` TRIGGER employee_term
AFTER UPDATE ON schedule
FOR EACH ROW
BEGIN
IF NEW.term_date_schedule <=> OLD.term_date_schedule THEN
INSERT INTO schedule_archive(schedule_id, login) VALUES(old.schedule_id, old.login);
END IF;
END

What is <=>? Use <> instead.

Related

Using a trigger to automatically assign a value to specific column on a new insert

I'm struggling to find proper information on SQL Triggers.
My previous question got removed for it not being specific enough so hopefully this will do better.
I am trying to create a trigger that will assign RoleID 1 to every newly inserted row to my users table.
But I can't seem to figure it out.
AFTER INSERT on users
on EACH ROW
insert into users.RoleID values(1);
This doesn't seem to work.
And the examples and or information regarding triggers all focus on alerts or sending emails after an insert/drop or update.
Can it actually be done?
Any help would be much appreciated.
Cheers!
It looks like you aren't creating you sql trigger with the correct keyword.
Try this
drop trigger if exists before_insert_users;
DELIMITER $$
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
SET NEW.RoleID = 1;
END$$
DELIMITER ;
Note: RoleID will need to actually be a column on your table for this to work.
However, a trigger is not the best way to do this... See this SO post : Add a column with a default value to an existing table in SQL Server

Error Code: 1442. Can't update table 'customer' in stored function/trigger because it is already used by statement which invoked this stored function

I'm writing a query that creates a trigger to soft delete a row in the table customer using the a flag called "IsDelete" when the flag is '0' it is not deleted and when the flag becomes 1 the row has been marked as deleted.
When the query is run the error code 1442c is generated. can anyone explain why??
DELIMITER $$
CREATE TRIGGER SOFT_DELETE_TRIGGER
BEFORE DELETE ON customer
FOR EACH ROW
BEGIN
IF OLD.IsDelete = 0 THEN
CALL cannot_delete_error;
UPDATE customer
SET IsDelete = 1;
END IF;
END
$$
Deleting a row in the table to test the trigger.
DELETE FROM customer
WHERE C_username = 'testuser'
Yes, triggers don't allow you to INSERT/UPDATE/DELETE against the same table that spawned the trigger, because that could run invoke the trigger again, and then that trigger might do another INSERT/UPDATE/DELETE, and so on. You have too high a chance of causing an infinite loop.
I'm afraid MySQL doesn't support an "instead of" trigger like some other brands of SQL database do. You can't make a trigger on DELETE that does an update instead.
You can use SIGNAL to make it throw an error, and that blocks the DELETE, but it doesn't do an UPDATE instead.
To implement soft deletes as you are doing, you'll just have to make the client use UPDATE instead of DELETE.

How to create a trigger for before/after delete with update/insert in mysql?

I have two tables like as:
fee_master(id,cTId,feeType,amount,startDate,lastDate,fine_last_date,fine,status)
payroll(id,emId,date,loan,netSalary)
I am trying to create a trigger like as:
DELIMITER $$
DROP TRIGGER IF EXISTS test
DELIMITER $$;
CREATE TRIGGER test
BEFORE DELETE ON fee_master
FOR EACH ROW
UPDATE payroll SET loan=OLD.amount,netSalary=OLD.fine WHERE id=18;
DELIMITER $$;
delete from fee_master where id='18';
When I have run this trigger, the data is deleted from fee_master, but payroll is not updated, also I have tried to insert payroll but not working.Every times the data is deleted from fee_master.
If I change the update and delete query position with trigger then It is ok. Actually, It is not working on trigger operation.
What is the problem ?
Your syntax for UPDATE is incorrect. Multiple assignments are separated by ,, not AND.
UPDATE payroll SET loan=OLD.amount, netSalary=OLD.fine WHERE id=18;
May be you are new on triggering.
According to your question, I recommend you first read basics of triggering from here http://www.sitepoint.com/how-to-create-mysql-triggers/
Remind that, It is a stored process. You do not need to run the trigger every times,I hope you are confuse here. After creating a trigger, You have to run the master query then the trigger automatically run the next operation.
Your code is ok. And the code of Barmar also ok.The main problem your understanding.

MySQL: trigger with clause "instead of update"

I'm trying to create a database with history in mind (experience shows you'll have to do this one day or another).
I've asked here database-design-how-to-handle-the-archive-problem but there's no better anser than the link here.
My problem is about where to do the code and technically, how (MySQL gives me headaches). First I've started doing this in Php: before doing any insert, duplicate the record mark it as "obsolete" then modify the record.
But there's a dependency problem (manytomany and manytoone associations must be updated as well) which implies coding (one way or another) all the dependancies and updates that come with the tables (which is not acceptable).
So I'm thinking about doing all the work on the database server side. This would greatly simplify my Php code.
The problem is that I have to "archive" the current record before modifying it. To do so, the code must be in a trigger "before update".
Here's my code:
DELIMITER ;;
DROP TRIGGER IF EXISTS produit_trigger_update_before;
CREATE TRIGGER produit_trigger_update_before
BEFORE UPDATE ON produit
FOR EACH ROW BEGIN
/* */
INSERT INTO produit SET
id_origine = OLD.id_origine,
date_v_creation = OLD.date_v_creation,
date_v_start = OLD.date_v_debut,
date_v_end = NOW(),
...
last_record = OLD.last_record;
/* Dependancies : */
SET #last=LAST_INSERT_ID();
UPDATE categorie_produit SET id_produit=#last
WHERE id_produit = OLD.id;
UPDATE produit_attribut SET id_produit=#last
WHERE id_produit = OLD.id;
END;;
DELIMITER ;;
If I get this code working, all my problems are gone. But damn it, it's not working:
mysql> update produit set importance=3;
ERROR 1442 (HY000): Can't update table 'produit' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
mysql> Bye
In this page there's a working sample, which uses INSTEAD OF UPDATE clause in the trigger. MySQL doesn't seem to support this.
So my question is both conceptual (= have you any other "principle" that could work) and/or technical (= can you make this trigger work).
If I get this code working, all my problems are gone. But damn it, it's not working:
As a rule you can't have a trigger on table A trigger inserts into table A - since that could cause an endless loop. (Trigger mutation in Oracle terms)
Personally I would not do this using triggers. Triggers can do "audit logging" - but this is not what you want here.
I suggest you solve it programatically - either with a PHP function or a MySQL stored procedure (whatever your preference) that you call something like "ModifyProduit".
The code would then do basically what you have the trigger above do. (It might be easier to just have the code set date_v_end on the current row, and then insert a completly new row. That way you don't have to mess around with updating your referenced tables)
you can do history of a table with an auxiliary table like this (i've done this for many tables on mysql and the speed is very good):
table produit_history has the same structure as produit + 2 additional columns: "history_start DATETIME NOT NULL" and "history_stop DATETIME DEFAULT NULL".
there are 3 triggers on produit table:
AFTER INSERT: in this trigger there is a simple insert into produit_history of the same data with history_start = NOW() and history_stop = NULL (NULL means the current row is valid)
AFTER UPDATE: this trigger performs two queries. The first is un update like this:
UPDATE produit_history set history_stop = NOW() WHERE id_origine = OLD.id_origine AND history_stop IS NULL;
The second query is an insert identical to the one in the AFTER INSERT trigger.
AFTER DELETE: this triggers there is a simple update which is identical to the one in the AFTER UPDATE.
You can then query this history table and obtain snapshots at whatever time you're interested in with the following where condition:
WHERE (history_start &lt= "interesting_time" AND (history_stop IS NULL OR history_stop &gt "interesting_time"))

MySQL Triggers to Disable A User Account

I am trying to create a MySQL Trigger to disable someone's account if they have logged in to the site 3 times. I have tried to create this trigger using the following code, but it is not setting is_active to 0 no matter what times_logged_in is. Any help would be appreciated.
CREATE TRIGGER updateTrigger AFTER UPDATE ON users
FOR EACH ROW
BEGIN
UPDATE users SET is_active=0 WHERE NEW.is_code=1
AND NEW.times_logged_in>=3
AND NEW.user_id=user_id;
END;
You've hit a limitation of MySQL. Your table users invokes the trigger (AFTER UPDATE ON users), therefore the triggered code cannot modify it. See the MySQL-Manual:
Within a stored function or trigger,
it is not permitted to modify a table
that is already being used (for
reading or writing) by the statement
that invoked the function or trigger.
I'm not sure where user_id comes from in your trigger, looks like an extraneous check, try this:
CREATE TRIGGER updateTrigger AFTER UPDATE ON users
FOR EACH ROW
BEGIN
UPDATE users SET is_active=0 WHERE NEW.is_code=1
AND NEW.times_logged_in>=3
END;
Also, be sure that is_code = 1 is actually matching on the users you're updating, if it's not, it won't update any rows.
I would use a BEFORE UPDATE trigger instead:
CREATE TRIGGER updateTrigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF NEW.is_code=1 AND NEW.times_logged_in>=3 THEN
SET NEW.is_active=0;
END IF;
END;