I am trying to implement a trigger in MySQL that will automatically add/modify a column when another one is added/modified. Basically I want it to save a type as a slug in the type_sanitized column when a value is added/modified into type.
This is my initial try:
DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;
CREATE TRIGGER 'insert_sanitized_article_type'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;
CREATE TRIGGER 'update_sanitized_article_type'
AFTER UPDATE
ON 'ArticleTypes'
FOR EACH ROW
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
After seeing suggestions online, I have tried adding delimiters like this:
DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;
DELIMITER $$
CREATE TRIGGER 'insert_sanitized_article_type'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
BEGIN
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;
DELIMITER $$
CREATE TRIGGER 'update_sanitized_article_type'
AFTER UPDATE
ON 'ArticleTypes'
FOR EACH ROW
BEGIN
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
END$$
DELIMITER ;
I get the following error for both of them:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''insert_sanitized_article_title'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
BE' at line 1
This is my first time creating triggers in MySQL so it might be obvious but I haven't been able to figure it out in 2 hours of searching so any help would be greatly appreciated.
Remove the apostrophes.
DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;
DELIMITER $$
CREATE TRIGGER insert_sanitized_article_type
AFTER INSERT
ON ArticleTypes
FOR EACH ROW
BEGIN
SET #type_sanitized = LOWER(REPLACE(TRIM(#type), ' ', '-'));
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;
DELIMITER $$
CREATE TRIGGER update_sanitized_article_type
AFTER UPDATE
ON ArticleTypes
FOR EACH ROW
BEGIN
SET #type_sanitized = LOWER(REPLACE(TRIM(#type), ' ', '-'));
END$$
DELIMITER ;
Related
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$$
I'm trying to create trigger in Mysql, and get the error:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TRIGGER `foo`.`test` AFTER INSERT ON `foo`.`test` FOR EACH ROW BE' at line 2
in this trigger:
DELIMITER $$
SET #name2:='w';
CREATE TRIGGER `foo`.`test`
AFTER INSERT
ON `foo`.`test`
FOR EACH ROW BEGIN
SELECT name1 INTO name2;
END;
$$
DELIMITER ;
What is wrong with line 2?
try this:
DELIMITER $$
CREATE TRIGGER `foo`.`test` AFTER INSERT
ON `foo`.`test`
FOR EACH ROW BEGIN
SET #name2:='w';
SELECT name1 INTO #name2;
END$$
DELIMITER ;
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.
I am trying to fetch some values from php page and perform trigger in mysql database. Which is as below:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger` AFTER INSERT OR UPDATE ON `table2`
FOR EACH ROW BEGIN
IF(NEW .flag=0) THEN
INSERT INTO temp(sent,pcount,ncount) VALUES (NEW.sent,NEW.pcount,NEW.ncount);
ELSE
UPDATE temp SET pcount=NEW.pcount AND ncount=NEW.ncount WHERE id = NEW.id;
END IF;
END;
$$
DELIMITER ;
Which gives error:
error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'or update ON `table2`
FOR EACH ROW BEGIN
if(NEW .flag=0) then
INSERT INTO' at line 3
I tried to change = with =: but error persist. Any idea whaat the prob?
You can't define a trigger for update and insert at the same time. You have to define 2 triggers:
CREATE TRIGGER `Mysql_insert_Trigger` AFTER INSERT ON `table2` ...
CREATE TRIGGER `Mysql_update_Trigger` AFTER UPDATE ON `table2` ...
I count do it. There was problem in syntax. Here is the correct which perform update insert at same moment in different table and update-inset on same table but with if else:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger2`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger2` AFTER INSERT ON `table2`
FOR EACH ROW BEGIN
IF 'flag'=1 THEN
UPDATE record SET sent=NEW.sent WHERE id=NEW.id;
ELSE
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp VALUES ()
END IF;
END;
$$
DELIMITER ;
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 ;