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 ;
Related
DELIMITER $$
CREATE TRIGGER arrange_serial
AFTER DELETE ON db.table
FOR EACH ROW
BEGIN
UPDATE db.table set serial=serial-1 where serial > DELETED.serial;
END$$
DELIMITER ;
On delete I'm getting the error that db.DELETED does not exist. Can anyone suggest me where I'm wrong.
You are supposed to reference the old value of the deleted row. Use OLD in the place of DELETED;
DELIMITER $$
CREATE TRIGGER arrange_serial
AFTER DELETE ON db.table
FOR EACH ROW
BEGIN
UPDATE db.table set serial=serial-1 where serial > OLD.serial;
END$$
DELIMITER ;
First table - rm_desc
Second table - room_cat_mapping
Want to achieve : while deleting record from rm_desc automatically it should delete related record from room_cat_mapping as well.
BUT BELOW TRIGGER IS NOT WORKING, ANY clue new to trigger
DROP TRIGGER IF EXISTS DELETE_ROOM_TYPES;
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `DELETE_ROOM_TYPES` BEFORE DELETE
ON `rm_desc`
FOR EACH ROW
BEGIN
DELETE FROM room_cat_mapping
WHERE room_cat_mapping.prop_id = rm_desc.res AND room_cat_mapping.room_cat_id = rm_desc.rm_cat;
END$$
DELIMITER ;
This should do:
DROP TRIGGER IF EXISTS DELETE_ROOM_TYPES;
DELIMITER $$
CREATE
TRIGGER `DELETE_ROOM_TYPES` AFTER DELETE
ON `rm_desc`
FOR EACH ROW
BEGIN
DELETE FROM room_cat_mapping
WHERE room_cat_mapping.prop_id = OLD.res AND room_cat_mapping.room_cat_id = OLD.rm_cat;
END$$
DELIMITER ;
I changed the trigger to AFTER and referenced the deleted row values using OLD.
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 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.
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 ;