How can I determine why my trigger does not run? - mysql

create database triggers;
use triggers;
create table if not exists Customers(
custID INT unsigned not null auto_increment,
age int,
name varchar(30),
primary key(custID)
);
delimiter //
create trigger age_verify
before insert on customers
for each row
if new.age < 0 then set new.age = 0;
end if; //
insert into Customers
values (101, 27, 'James'),
(102, -40, 'Ammy'),
(103, 32, 'Ben'),
(104, -39, 'Angela');
select * from Customers;
For some reason my trigger in MySQL workbench does not run and when I run select * from customers it prints the negatives still and does not update the value. How can I debug this?

Your trigger is syntactically incorrect. Multiple-statement trigger code must be enclosed with BEGIN-END block:
delimiter //
create trigger age_verify
before insert on customers
for each row
BEGIN
if new.age < 0 then
set new.age = 0;
end if;
END //
DELIMITER ;
But in your particular case you do not need in BEGIN-END and DELIMITER, use simple
CREATE TRIGGER age_verify
BEFORE INSERT ON customers
FOR EACH ROW
SET NEW.age = GREATEST(NEW.age, 0);

delimiter //
create trigger age_verify before
insert on Customers
for each row
begin
if new.age < 0 then set new.age = 0;
end if;
end
//
delimiter ;

Related

how to declare variable phpmyadmin mysql triggers properly?

I have a database where whenever residential address update in user table I want it to store in history table of user. For that I'm trying to write triggers but failing miserably in phpmyadmin. Also it's not giving me proper reason why so I can correct it. This is what I have done so far.
DROP TRIGGER IF EXISTS `record_history`;
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
DECLARE date_current datetime;
DECLARE residential_address varchar(1000);
SET #date_current = NOW();
SET #residential_address = NEW.residential_address;
IF (#residential_address <> OLD.residential_address AND #residential_address != "" AND #residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, #residential_address, 1, #date_current, #date_current);
END IF;
END;
delimiter ;
A cleaner version of your code
DROP TRIGGER IF EXISTS `record_history`;
delimiter $$
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
IF (new.residential_address <> OLD.residential_address AND new.residential_address <> "" AND new.residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, new.residential_address, 1, now(), now());
END IF;
END $$
delimiter ;
If you are still having problems please add sample data from s_user as text to the question.

After Insert MySQL Trigger to update values or insert a new row in another table

I have two tables,
RawFeed , SellerList.
When a new row is inserted in RawFeed, I need to insert a row in SellerList if a corresponding SellerID does not exist in SellerList,
Otherwise, I need to increment two column values in the SellerList table for that SellerID.
Here is my Trigger code-
CREATE TRIGGER `SellerListUpdate` AFTER INSERT ON `RawFeed`
FOR EACH ROW
BEGIN
--CHECK SELLER ID EXISTS OR NOT.
DECLARE SELLEREXISTS INT;
SELECT COUNT(*) INTO SELLEREXISTS FROM SellerList WHERE SellerList.SellerID=NEW.seller_id;
IF SELLEREXISTS=0 THEN
--INSERT ROW
INSERT INTO SellerList(`SellerID`, `Total`, `Active`) VALUES(NEW.seller_id, 1, 1);
ELSE
--UPDATE ROW
UPDATE SellerList SET Total=Total+1, Active=Active+1 WHERE SellerList.SellerID=NEW.seller_id;
END IF;
END
And I am receiving this error-
SQL Error(1064): You have an error in your SQL Syntax;
Try changing the delimiter
DELIMITER $$
CREATE TRIGGER `SellerListUpdate` AFTER INSERT ON `RawFeed`
FOR EACH ROW
BEGIN
--CHECK SELLER ID EXISTS OR NOT.
DECLARE SELLEREXISTS INT;
SELECT COUNT(*) INTO SELLEREXISTS FROM SellerList WHERE SellerList.SellerID=NEW.seller_id;
IF SELLEREXISTS=0 THEN
--INSERT ROW
INSERT INTO SellerList(`SellerID`, `Total`, `Active`) VALUES(NEW.seller_id, 1, 1);
ELSE
--UPDATE ROW
UPDATE SellerList SET Total=Total+1, Active=Active+1 WHERE SellerList.SellerID=NEW.seller_id;
END IF;
END
$$
DELIMITER ;

Resultset from trigger MySQL

This is my first time using trigger and I'm having a bit of trouble.
I'm creating a notification system that when a new notification is created it will add a row to the notify table. What is added depends on what notification type it is.
So far I've tried:
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
SELECT #group := notify_group FROM notification_types WHERE type=NEW.type;
IF (#group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
INSERT INTO notify (user_id, notification_id) VALUES(
SELECT ID, NEW.id FROM user_customer WHERE clientID=NEW.user_reference
);
END IF;
END; $$
DELIMITER ;
I searched around and then changed it to:
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
DECLARE insert_user_id INT(11);
SELECT #group := notify_group FROM notification_types WHERE type=NEW.type;
IF (#group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
SELECT ID INTO insert_user_id FROM user_customer WHERE clientID=NEW.user_reference;
INSERT INTO notify (user_id, notification_id) VALUES(insert_user_id, NEW.id);
END IF;
END; $$
DELIMITER ;
I've also tried mixing the above 2 up and all I'm getting is either can not return result set or there is a syntax error with my select query.
(Also, I'm assuming that the NEW keyword is predefined for triggers?)
Your first try was actually on the right track, but you did not write the result of your SELECT into a variable. Returning a result set from a trigger is not possible.
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
DECLARE v_group INT;
SELECT notify_group INTO v_group FROM notification_types WHERE type=NEW.type;
IF (v_group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
INSERT INTO notify (user_id, notification_id) VALUES(
SELECT ID, NEW.id FROM user_customer WHERE clientID=NEW.user_reference
);
END IF;
END $$
DELIMITER ;

Executing query in MySQL Trigger not working

I have two table 'testing1' with fields 'firstname' and 'lastname'. Another table 'testing2' with field 'firstname'.
So the trigger checks first whether the 'NEW.firstname' exists in 'testing2' table or not. If it does then it doesn't execute the INSERT query but if it doesn't exist then the INSERT query is executed and 'NEW.firstname' is added in 'testing2' table.
Here's the trigger that i created ... but I'm getting error in the IF loop ...
DELIMITER $$;
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SET #rowCount := ( SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname );
IF (rowCount)
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END $$
I'm unable to figure out where did I made the mistake... Any help ??
Try
DELIMITER $$;
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SET #rowCount := ( SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname );
IF (rowCount) THEN
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END IF;
END$$
DELIMITER ;
But maybe you shoud use IF (rowCount)>0
This worked for me :
DELIMITER //
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname INTO rowCount;
IF rowCount = 0 THEN
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END IF;
END //
DELIMITER ;

MySQL ERROR 1054:UNKNOWN COLUMN 'amount' in field list

I want to create a trigger so that:
if the AMOUNT field < 100 the AMOUNT field automatically inserts 100.
If AMOUNT > 100 the entered value should be inserted in the AMOUNT field.
Table:
CREATE TABLE DB_triggers.acc(
C_ID INT(3) UNIQUE NOT NULL AUTO_INCREMENT ,
customer_name CHAR(30),amount FLOAT(10,2) UNSIGNED,
date_created DATE,PRIMARY KEY(C_ID,customer_name)
) AUTO_INCREMENT=100;
Trigger:
DELIMITER $$
CREATE TRIGGER trig_acc
BEFORE INSERT ON acc
FOR EACH ROW
BEGIN
IF amount <100 THEN
INSERT INTO acc VALUES(new.c_id,new.customer_name,100,new.now());
ELSE
INSERT INTO acc VALUES(new.c_id,new.customer_name,new.amount,new.now());
END IF;
END $$
DELIMITER ;
BUT, when I insert a value :
INSERT INTO
acc(c_id,customer_name,amount,date_created)
VALUES
(DEFAULT,'ABC',1000,NOW());
gives error 1054
Change IF amount <100 THEN to IF new.amount <100 THEN then it will work.
Try this:
DELIMITER $$
CREATE TRIGGER trig_acc
BEFORE INSERT ON acc
FOR EACH ROW
BEGIN
IF new.amount <100 THEN
INSERT INTO acc VALUES(new.c_id,new.customer_name,100,new.now());
ELSE
INSERT INTO acc VALUES(new.c_id,new.customer_name,new.amount,new.now());
END IF;
END $$
DELIMITER ;