Trigger not checking if condition - mysql

I have created a trigger , and this trigger updating the field value , but not checking the if condition ,
DELIMITER $$
create trigger `njsystem`.`test` BEFORE UPDATE on `njsystem`.`tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
UPDATE tbl_users SET user_active = 0;
END IF;
END; $$
DELIMITER ;

What I understand from your problem statement is that, you wanted to set user_active to 0 if user_failed_logins count goes above 3. Here is solution for that, you can also change values using new.
DELIMITER $$
create trigger `test` BEFORE UPDATE on `tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
SET NEW.user_active = 0;
END IF;
END; $$
DELIMITER ;

Related

create a trigger need to update table 2 on update of table1

Would like to create a trigger in mysql need to
update t2.orderstatus as 'inactive' on update of t2.inactivedate and
update t2.orderstatus as 'active' on update of t2.activedate. Tried to adapt below one. but failed
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE TRIGGER trigger_on_stockhistory_update
AFTER update ON stockhistory.inactivedate
FOR EACH ROW
BEGIN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END;
$$
DELIMITER;
You can not set trigger on field change. It may be only on table level:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
IF OLD.inactivedate <> NEW.inactivedate THEN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END IF;
IF OLD.activedate <> NEW.activedate THEN
UPDATE mastersku
SET ordernow ='Active'
WHERE `SSKU` = NEW.SSKU;
END IF;
END$$
DELIMITER ;
Or a little elegant query using CASE statement:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
UPDATE mastersku
SET ordernow = CASE
WHEN OLD.inactivedate <> NEW.inactivedate THEN 'InActive'
WHEN OLD.activedate <> NEW.activedate THEN 'Active'
ELSE ordernow
END
WHERE
SSKU = NEW.SSKU;
END$$
DELIMITER ;

trigger two according to data

I have three tables and I have one trigger from table D_ISI in table 2 to trigger MAX_ISI value to write to the other table. Can you help me solve the trigger problem?
DELIMITER $$
CREATE TRIGGER `ISI_YUKSEK_ISE` BEFORE INSERT ON `TB1` FOR EACH ROW BEGIN
DECLARE
D_ISI INT ;
SET
D_ISI = NEW.D_ISI ; IF
(SELECT D_ISI = NEW.D_ISI FROM TB1,TB2 WHERE TB2.CIHAZ_ID=TB1.CIHAZ_KODU AND TB1.D_ISI >= TB2.MAX_ISI OR TB1.D_ISI <= TB2.MIN_ISI) THEN
INSERT
INTO
TB3(CIHAZ_KODU,D_ISI)
VALUES(NEW.CIHAZ_KODU,NEW.D_ISI) ;
END IF ; END
$$
DELIMITER ;

Mysql Trigger Error with IF THEN

I have a problem with trigger.. I would like to change a value to false if new returnBike is not null.
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
IF (NEW.returnBike <> null) THEN
UPDATE Bike SET isRented = false
WHERE id = (select Bike from Rent); -- but there is Error (missing semicolon)
END IF;
Any advice?
I would be inclined to just do:
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
UPDATE Bike
SET isRented = false
WHERE id = new.Bike AND NEW.returnBike IS NOT NULL;
END; $$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
IF (NEW.returnBike IS NOT NULL) THEN
UPDATE BikeSET isRented = false
WHERE id = new.Bike;
END IF;
END; $$
DELIMITER ;
so to remove problem with syntax semicolon added delimiter and $$ after end;
Anyway thanks to give me some advice with if, comparing!

Problems creating TRIGGER in MySQL

I am first time using trigger concept in my MYSQL workbench like below query,
First I created people table by using below query,
CREATE TABLE people (age INT, name varchar(150));
Then I used below query for trigger
DELIMITER
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people FOR EACH ROW IF NEW.age < 0 THEN SET NEW.age = 0; END IF;
DELIMITER ;
But agecheck trigger not creating to people table. Its does not show any error message when I run this query.
Here is my table images,
Updated : based on the answer
Try with this.
USE `raptor1_5`;
DELIMITER $$
DROP TRIGGER IF EXISTS raptor1_5.agecheck$$
USE `raptor1_5`$$
CREATE TRIGGER agecheck BEFORE INSERT ON people
FOR EACH ROW
BEGIN
IF NEW.age < 0 THEN
SET NEW.age = 0;
END IF;
END$$
DELIMITER ;
The standard trigger syntax should be as
DELIMITER //
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people
FOR EACH ROW
BEGIN
IF NEW.age < 0 THEN
SET NEW.age = 0;
END IF;
END;//
DELIMITER ;
The above is when you run on mysql cli, however in phpmyadmin or workbench check if you have an option to set the delimiter if yes choose that and remove the delimiter part from the above.

MYSQL trigger error 1064

I would like to check if a record is being created with a user id, if not then generate one. I can't figure out what I'm doing wrong?
delimiter $$
DROP TRIGGER IF EXISTS init_uuid_users;
CREATE TRIGGER init_uuid_users BEFORE INSERT ON `users`
FOR EACH ROW BEGIN
IF (NEW.username IS NULL) THEN
SET NEW.username = UUID();
END IF;
END;
$$
delimiter ;
DROP TRIGGER IF EXISTS init_uuid_users;
put it before delimiter.
And remove ; after END.