Mysql Trigger: Tried other suggestions but still get Error #1064 - mysql

I've been looking at other stackoverflow questions here but cant seem to find my fault. I think its syntax-error related. On what part did I got wrong?
DELIMITER $$
DROP TRIGGER IF EXISTS keisan
CREATE TRIGGER keisan AFTER INSERT ON profitdb
FOR EACH ROW
BEGIN
DECLARE shinAgentPercent;
SET #shinAgentPercent:=`AgentRisk`-`SubAgentRisk`;
SET NEW.`SubAgentProfit` = `Profit`*(`SubAgentRisk`/100);
SET NEW.`AgentProfit` = `Profit`*(#shinAgentPercent/100);
END;
$$

Firstly, you need a closing statement indication after drop ... command.
DROP TRIGGER IF EXISTS keisan $$
Secondly, I suggest you to go with a BEFORE INSERT trigger to set expression values for other columns of the tables.
Example:
DELIMITER $$
DROP TRIGGER IF EXISTS keisan $$
CREATE TRIGGER keisan BEFORE INSERT ON profitdb
FOR EACH ROW BEGIN
SET #shinAgentPercent := NEW.AgentRisk - NEW.SubAgentRisk;
SET NEW.SubAgentProfit := NEW.Profit * ( NEW.SubAgentRisk / 100 );
SET NEW.AgentProfit := NEW.Profit * ( #shinAgentPercent / 100 );
END;
$$
-- now reset the delimiter to defaut
DELIMITER ;

Don't you need an end-of-statement after your DROP TRIGGER?
DROP TRIGGER IF EXISTS keisan$$

Related

How can I use MySQL Trigger to update quantity field in 'items' table with quantity - quantity_delivered

How can I use MySQL Trigger to update quantity field in items table with quantity-quantity_delivered when quantity_delivered in the requests table is updated ?
Here Is My Query, I have tested but I see its not getting updated, can any one help me please?
DELIMITER $$
CREATE TRIGGER items_update AFTER UPDATE ON requests
FOR EACH ROW
BEGIN
UPDATE items
SET items .quantity = items.quantity - requests.quantity_delivered
WHERE items .item_id = requests.item_id;
END;
$$
DELIMITER ;
I have resolved by myself like this and its working for me thanks All!
DELIMITER $$
CREATE TRIGGER `quantityTrigger` AFTER UPDATE ON `requests` FOR EACH ROW BEGIN
UPDATE items i
SET i.quantity=i.quantity-NEW.quantity_delivered
WHERE i.item_id=NEW.item_id;
END
$$
DELIMITER ;
You should be using the new. values in your trigger like this
drop trigger if exists items_update;
DELIMITER $$
CREATE TRIGGER items_update AFTER UPDATE ON requests
FOR EACH ROW
BEGIN
UPDATE items
SET items.quantity = items.quantity - new.quantity_delivered
where items.item_id = new.item_id;
END;
$$
DELIMITER ;
As an aside should this be an after insert trigger?

I am trying to make a trigger in mysql, but not working

Here s my trigger:
DELIMITER $$
CREATE TRIGGER after_changing_student_year
AFTER UPDATE ON student_details
FOR EACH ROW
BEGIN
SET #old_stud_id = NEW.student_id;
INSERT INTO assigned_groups (student_id) VALUES (#old_stud_id);
END$$
DELIMITER ;
I used mysql variable because direct assignment wasn't working.
But I found this one is working:
DELIMITER $$
CREATE TRIGGER after_changing_student_year
AFTER UPDATE ON student_details
FOR EACH ROW
BEGIN
INSERT INTO assigned_groups (student_id) VALUES (83);
END$$
DELIMITER ;
I also found that renaming student_id column in assigned_groups to student_id_1 will work, but that's not a feasible solution as i have to make many triggers like this.
EDIT: Sorry guys, it turn out to be some problem with my PHP code. When i edit directly in mysql, trigger works correctly. But not when I update it through PHP code. What might be the issue?
You can do it like this, I tested and it is working:
DELIMITER $$
CREATE TRIGGER after_changing_student_year
AFTER UPDATE ON student_details
FOR EACH ROW
BEGIN
DECLARE old_stud_id INT;
SET old_stud_id = NEW.student_id;
INSERT INTO assigned_groups (student_id) VALUES (old_stud_id);
END$$
DELIMITER ;

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.

Drop trigger if exists in MySQL

I need to execute a command similar to the following not inside a procedure but inside a simple sql file for mysql 5.xx
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME = 'tr_fnninio_censopersona_ins') THEN
DROP TRIGGER tr_fnninio_censopersona_ins;
END IF;
Why not just
DROP TRIGGER IF EXISTS tr_fnninio_censopersona_ins;
MySQL drop trigger doc
You can use like this,
DROP TRIGGER IF EXISTS tr_fnninio_censopersona_ins;
DELIMITER $$
CREATE TRIGGER tr_fnninio_censopersona_ins
BEFORE INSERT ON `your_table` FOR EACH ROW
BEGIN
SET NEW.INSERTED= NOW();
END$$
DELIMITER ;

sql trigger is not working help me out

delimiter $$
drop TRIGGER if EXISTS upflttyprateTrig
create TRIGGER upflttyprateTrig
AFTER UPDATE ON flttyprate
FOR EACH ROW BEGIN
INSERT INTO `histflttyprate` (
`flttyprate_Id`,
`flttyprate_Flttyp_Id_Fk`,
`flttyprate_Date_Eff`,
`flttyprate_Date_Ineff`,
`flttyprate_IFR`,
`flttyprate_Single`,
`flttyprate_Multi`,
`flttyprate_Rate_Per_Hr`,
`flttyprate_Night_Surchage`,
`flttyprate_Status`,
`flttyprate_Token`)
VALUES (
NEW.flttyprate_Id,
NEW.flttyprate_Flttyp_Id_Fk,
NEW.flttyprate_Date_Eff,
NEW.flttyprate_Date_Ineff,
NEW.flttyprate_IFR,
NEW.flttyprate_Single,
NEW.flttyprate_Multi,
NEW.flttyprate_Rate_Per_Hr,
NEW.flttyprate_Night_Surchage,
NEW.flttyprate_Status,
NEW.flttyprate_Token);
END$$
is not working why what is actual syntax mysqlversion 5.0
The 'IF EXISTS' clause is not supported in DROP TRIGGER statement.
And add a delimiter after 'DROP TRIGGER...' statement.
EDIT
Read the information about trigger's existance from information_schema.triggers table; then drop trigger. Do it in the application or write a stored procedure.
Example:
DELIMITER $$
CREATE PROCEDURE drop_trigger()
BEGIN
SET #exist = NULL;
SELECT 1
INTO
#exist
FROM
information_schema.triggers
WHERE
trigger_schema = 'test'
AND trigger_name = 'trigger1';
IF #exist IS NOT NULL THEN
DROP TRIGGER test.trigger1;
END IF;
END
$$
DELIMITER ;