MySQL Triggers to Disable A User Account - mysql

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;

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 can I keep the DDL for my Trigger in Harmony with my Trigger code?

I'm trying to create a Trigger in Database Workbench (Lite, for MySQL) with this:
delimiter //
CREATE TRIGGER employeesTableInsert AFTER INSERT ON EMPLOYEES FOR EACH ROW
SET NEW.CREATED = CURRENT_TIMESTAMP; END IF;//
When I try to create the new Trigger, though, I get this err msg:
This seems odd; I look at the DDL tab in Database Workbench, and it has this:
Why is it doubling and mangling my Trigger code there in the (read-only) DDL?
As shown at the outset, my Trigger code only contains what is shown above:
So is the problem with my code, or is it because the DDL tab is confused? Or is my code CAUSING the DDL tab to become confused, or what?
If you want to update only the value you need following.
you can't use SET without a BEGIN and END, it is not a query.
I am wondering why there is a END IF in your code
Edit i also changed your TRIGGER to BEFORE INSERT, because on there you can change the values of a NEW column (see Bill Karwin comment)
And actually you don't need a trigger see manual
DELIMITER //
CREATE TRIGGER employeesTableInsert BEFORE INSERT ON EMPLOYEES
FOR EACH ROW
BEGIN
SET NEW.CREATED = CURRENT_TIMESTAMP;
END//

MySQL AFTER UPDATE Trigger Not Firing

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.

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.