Help with writing MySQL trigger - mysql

With the following tables:
Party.(PartyId, PartyTypeCode) and Member.(MemberId (FK), Name)
I want to fill in the MemberId column with the value of the PartyId after a new Party is created.
I know the SQL below is wrong but that is why I am looking for help in making it right.
CREATE TRIGGER tgr_member_create AFTER INSERT on Party
FOR EACH ROW BEGIN
IF NEW.PartyTypeCode = 'M' THEN
INSERT Member(MemberId)
VALUES('Party.PartyId')
END IF;
END;
Thanks to Nanne's input this is what works:
DELIMITER $$
CREATE TRIGGER tgr_member_create AFTER INSERT on Party
FOR EACH ROW BEGIN
IF NEW.PartyTypeCode = 'M' THEN
INSERT Member (MemberId,Name)
VALUES(NEW.PartyId, 'someName');
END IF;
END;$$
DELIMITER ;

Your values should call the NEW row, just as you do with the partytypecode I guess? I'm assuming
delimiter $$
CREATE TRIGGER tgr_member_create AFTER INSERT on Party
FOR EACH ROW BEGIN
IF NEW.PartyTypeCode = 'M' THEN
INSERT Member (MemberId,Name)
VALUES(NEW.PartyId, 'someName')
END IF;
END;$$
Pay attention to the delimiter code. Because you are using ; inside, you can't use it as a normal 'end-of-command' token. that's why the &&
For debugging:
Make sure the query works when you perform it outside a trigger:
INSERT Member (MemberId,Name)
VALUES(party-id goes here, 'someName')
There could be a small mistake there. Fill in your partyId obviously.

Try this
CREATE TRIGGER tgr_member_create AFTER INSERT on Party
FOR EACH ROW BEGIN
IF NEW.PartyTypeCode = 'M' THEN
INSERT Member(MemberId)
VALUES('New.PartyId')
END IF;
END;

Related

CREATE TRIGGER to monitor a specific table row

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

MySQL need help setting up trigger

I am trying to set up a TRIGGER to clear empty strings before INSERT. Not rocket science but I can't find the error in my syntax!
Here is the TRIGGER itself
USE `ga_abtest_logging`;
DELIMITER $$
CREATE TRIGGER avoid_empty
BEFORE INSERT ON daily_analytics
FOR EACH ROW
BEGIN
IF profileID = ''
THEN SET profileID = NULL
END IF;
END$$
Here is what workbench is showing:
On hover over the END IF it reads syntax error, unexpected END, expecting ';'
Could I have a problem with the settings on my DB? I have gone through the MySQL docs and I think the trigger looks right! Does anyone see anything obviously wrong?
You should make a few changes:
Use the NEW. prefix when referencing a column value
Add a semi-colon at the end of the line where you set the value
For example:
DELIMITER $$
CREATE TRIGGER tr_b_ins_daily_analytics BEFORE INSERT ON daily_analytics FOR EACH ROW BEGIN
IF (NEW.profileID = '')
THEN
SET NEW.profileID = NULL;
END IF;
END $$
DELIMITER ;
The code below should work.
DELIMITER $$
CREATE TRIGGER avoid_empty
BEFORE INSERT ON daily_analytics
FOR EACH ROW
BEGIN
IF NEW.profileID = ''
THEN SET NEW.profileID = NULL;
END IF;
END;$$
DELIMITER ;

Problems creating TRIGGER in MySQL

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.

Comparing string in mysql trigger

Here is a trigger i created and i want to compare new.utype with employee as given below but dont know the actual way to compare it in mysql .. how can i achieve this and also please tell if condition syntax is correct or not
delimiter $$
CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF(new.utype =='employee') THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$
DELIMITER ;
Use "like" key word as below
IF new.utype like 'employee'
Entire trigger looks like this
delimiter $$
CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF new.utype like 'employee' THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$
DELIMITER ;
I assumed trigger you wrote other than the string comparison is correct
Thanks
You need to use single = instead of ==
IF new.utype = 'employee'
DELIMITER $$
CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF new.utype = 'employee' THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$
DELIMITER ;
You can also use <> for comparing not equal value.
IF new.utype <> 'employee'
DELIMITER $$
CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF new.utype <> 'employee' THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$
DELIMITER ;

mysql trigger not working?

I am trying to create a trigger to insert a new row conditionally based on an insert on another table...I can't seem to nail the syntax.
Here is what I have thus far:
DELIMETER $$
CREATE TRIGGER overPricedCar
AFTER INSERT ON cars
FOR EACH ROW
BEGIN
IF (new.sellPrice > '80000' )THEN
INSERT INTO listings VALUES(new.carName,'GOLD','0',' ');
END IF;
END$$
DELIMETER ;
For some reason I keep getting an error, they syntax seems to be OK, I'm not sure where I may have gone wrong.
EDIT
After correcting the typo, the trigger 'works'.
I have added a comment to be output when the trigger happens.
I have tested it, and the output message gets printed to the screen but the trigger does not actually complete the inserts:
DELIMITER $$
CREATE TRIGGER overPricedCar
BEFORE INSERT ON cars
FOR EACH ROW
BEGIN
IF (new.sellPrice > '80000' )THEN
INSERT INTO listings VALUES(new.carName,'GOLD','0',' ');
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "New Gold car!"; // this line throws it off
END IF;
END$$
DELIMITER ;
Where can I place the messages I want to be printed to the screen when this trigger runs?
Typo:
DELIMETER
^--- should be an I: DELIMITER