I have a database with some tables. I want to create a trigger to 'Delete a record in tblplayersfield with "pID" of the record which was deleted in tblplayers'
CREATE TRIGGER delete_from AFTER DELETE on tblplayers
FOR EACH ROW
BEGIN
DELETE FROM tblplayerfields
WHERE 'tblplayerfields'.'pID' = OLD.'pID';
END
You need to add a delimiter change first
delimiter |
CREATE TRIGGER delete_from AFTER DELETE on tblplayers
FOR EACH ROW
BEGIN
DELETE FROM tblplayerfields
WHERE 'tblplayerfields'.'pID' = OLD.'pID';
END
|
delimiter ;
The delimiter signals the DB engine the end of your statement. Normally it is ;. But that would end the stored procedure at the first ;. And its definition would be incomplete.
You can change the delimiter and add it to the end of your procedure. After that change the delimiter back to ;
Related
i have a table which contains columns as "barkoda1,barkoda2,barkoda3,#pdfa1,#pdfa2,#pdfa3", what i'm trying to do is when a record assigned to barkoda1 copy same record to #pdfa1,for example if barkoda1 defined as "1234567890" #pdfa1 should be "1234567890.pdf".
i tried to create a trigger for it, but failed;
DELIMITER //
CREATE TRIGGER pdfdeneme AFTER INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
UPDATE finyeb_ba_2369_bio_for_2__r_d_ SET `#pdfa1`=NEW.barkoda1;
END//
DELIMITER ;
when i create this trigger,it doesn't return any error but when i add record to table mysql returns "it is already used by statement which invoked this stored function/trigger".
also tried this way
DELIMITER //
CREATE TRIGGER pdfdeneme BEFORE INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
INSERT INTO finyeb_ba_2369_bio_for_2__r_d_(`#pdfa1`) VALUES(NEW.barkoda1);
END//
DELIMITER ;
But same error happened, what am i doing wrong here?
You need a BEFORE INSERT TRIGGER
And then only use the SET Clause
DELIMITER //
CREATE TRIGGER pdfdeneme BEFORE INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
SET NEW.`#pdfa1` = CONCAT(NEW.barkoda1,'.pdf');
END//
DELIMITER ;
But your column name is a bad choice, it leads on the first glance to false conclusions.
I have two tables & need to write a TRIGGER for this scenario.
1. Parameters_Table(int pkid, string param_name, string param_value)
2. Tokens_Table(int pkid,string item,string validity)
I have a requirement of deleting all entries in Tokens_Table if a particular row where param_name='Enterprise' in Parameters_Table changes its 'param_value'. The value is changed from UI. eg., row <7,enterprise,60> changes to <7,enterprise,25> in Parameters_Table then delete all entries from Tokens_Table.
How should I write the trigger. Please guide. (I need to write this for Informix Database, but mysql queries would also help)
DELIMITER $$
CREATE TRIGGER after_update AFTER UPDATE ON Parameters_Table
FOR EACH ROW
BEGIN
IF OLD.param_name ='Enterprise' THEN
IF NEW.param_value != OLD.param_value THEN
DELETE FROM Tokens_Table WHERE pkid = NEW.pkid;
END IF;
END IF;
END$$
Just try above code.Hope this will helps.
You can try the following syntax/format:
DELIMITER $$
CREATE TRIGGER delete_tokens AFTER INSERT ON Parameters_Table
FOR EACH ROW
BEGIN
IF NEW.param_name = 'enterprise' THEN
BEGIN
DELETE FROM Tokens_Table WHERE pkid = NEW.pkid;
END;
END IF;
END$$
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 am first time using trigger concept in my MYSQL workbench like below query,
First I created people table by using below query,
CREATE TABLE people (age INT, name varchar(150));
Then I used below query for trigger
DELIMITER
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people FOR EACH ROW IF NEW.age < 0 THEN SET NEW.age = 0; END IF;
DELIMITER ;
But agecheck trigger not creating to people table. Its does not show any error message when I run this query.
Here is my table images,
Updated : based on the answer
Try with this.
USE `raptor1_5`;
DELIMITER $$
DROP TRIGGER IF EXISTS raptor1_5.agecheck$$
USE `raptor1_5`$$
CREATE TRIGGER agecheck BEFORE INSERT ON people
FOR EACH ROW
BEGIN
IF NEW.age < 0 THEN
SET NEW.age = 0;
END IF;
END$$
DELIMITER ;
The standard trigger syntax should be as
DELIMITER //
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people
FOR EACH ROW
BEGIN
IF NEW.age < 0 THEN
SET NEW.age = 0;
END IF;
END;//
DELIMITER ;
The above is when you run on mysql cli, however in phpmyadmin or workbench check if you have an option to set the delimiter if yes choose that and remove the delimiter part from the above.
I am attempting to create a trigger to update quantities between 2 separate databases. This query runs successfully, but when I show triggers in mysql, it brings up an empty set. Any help would be much appreciated.
delimiter $$ CREATE TRIGGER `quantity_to_clb` AFTER UPDATE ON product
FOR EACH ROW BEGIN UPDATE cl_boutique.product AS clb
LEFT JOIN cherrylane.product AS cl
ON clb.model = cl.code SET clb.quantity = cl.available
WHERE clb.model = cl.code
END $$
delimiter ;
That's because your code has two synax errors:
delimiter $$ --delimiter statements need to be on separate lines
CREATE TRIGGER `quantity_to_clb` AFTER UPDATE ON product
FOR EACH ROW BEGIN
UPDATE cl_boutique.product AS clb
LEFT JOIN cherrylane.product AS cl
ON clb.model = cl.code SET clb.quantity = cl.available
WHERE clb.model = cl.code; -- ; was needed here
END $$
delimiter ;