MYSQL trigger error 1064 - mysql

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.

Related

Syntax error on my mysql trigger code. ERROR 1064 (42000):

I have below trigger code but It give me error. I can't figure out what is wrong with my code.
DROP TRIGGER IF EXISTS `user_has_voice_queues_rt_update`;
CREATE DEFINER=`root`#`localhost`
TRIGGER `user_has_voice_queues_rt_update`
BEFORE UPDATE ON `user_has_voice_queues_rt`
FOR EACH ROW begin
if(new.pause='0') then
Set new.penalty = (select max(penalty) from user_has_voice_queues_rt) + 1;
end if;
Try this:
DROP TRIGGER IF EXISTS `user_has_voice_queues_rt_update`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost`
TRIGGER `user_has_voice_queues_rt_update`
BEFORE UPDATE ON `user_has_voice_queues_rt`
FOR EACH ROW begin
if(new.pause='0') then
Set new.penalty = (select max(penalty) from user_has_voice_queues_rt) + 1;
end if;
END $$
DELIMITER ;

Mysql create trigger throws error

I've tried to create trigger for each new post
here is my code
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
INSERT INTO attributes SET attrtext = 'sometext', post_id = NEW.id;
END;
I'm getting this error
You probably just need a delimiter:
DELIMITER $$
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
INSERT INTO attributes SET attrtext = 'sometext', post_id = NEW.id;
END;
$$
DELIMITER ;
However, you are also using a non-standard form of INSERT. I would suggest:
DELIMITER $$
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
INSERT INTO attributes(attrtext, post_id)
VALUES('sometext', NEW.id);
END;
$$
DELIMITER ;
The parser is confused by the ; that terminates the statement inside the trigger's BEGIN...END block. You need to use a DELIMITER statement to define a new delimiter, which you can then use to end the CREATE TRIGGER statement.
Your INSERT command is wrong. It should have been
INSERT INTO attributes (attrtext, post_id) VALUES ('sometext', NEW.id);

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

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$$

Trigger not checking if condition

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 ;

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 ;