I have two tables
1. Tag
2. Triger_testing
Tag desc
id int, is_active (tinyint)
Trigger_Testing Desc
tag_id (int), is_active(tinyint)
I want to create a trigger on tag table update which update trigger_testing table. So if tag.is_active is set to 0 the trigger must fire and update trigger_testing table and set trigger_testing.is_active=0 where trigger_testing.tag_id=tag.id.
I tried to create a trigger in MYSQL but getting syntax exception. Can someone help me out in resolving that issue.
Here is the code : -
CREATE TRIGGER update_trigger_testing AFTER UPDATE ON tag
FOR EACH ROW
BEGIN
IF NEW.is_active=0 THEN
UPDATE trigger_testing SET is_Active=0 WHERE tag_id=NEW.id
END IF
END$$
DELIMITER;
The error I am getting is :
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 'END IF
END$$
DELIMITER' at line 6
CREATE TRIGGER update_trigger_testing AFTER UPDATE ON tag
FOR EACH ROW
BEGIN
IF NEW.is_active=0 THEN
UPDATE trigger_testing SET is_Active=0 WHERE tag_id=NEW.id;
END IF;
END;
Related
I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following 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 '' at line 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;
I am trying to create in MySQL a 'custom' unique ID based on existing primary key (auto-incremented) 'id' field
delimiter //
CREATE TRIGGER update_id AFTER INSERT ON test
BEGIN
UPDATE test SET PN="PN-"+NEW.id;
END;
//
delimiter ;
I am using PhpMyAdmin and I really don't know if it's a UI problem (I encountered some problems in the past while creating triggers in PhpMyAdmin) or I really do something wrong.
What I need is a custom PN field that is automatically updated when insert new records in table, based on some text prefix "PN-"
id PN other fields
-------------------------
...
...
...
1253 PN-1253
1254 PN-1254
#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 'BEGIN
UPDATE test SET PN="PN-"+NEW.id;
END' at line 2
I read in some other StackOverflow post that this will be impossible (to update) on the same table AFTER insert.
There is a solution with this? Thanks.
Get the new value and add it before insert
delimiter //
CREATE TRIGGER update_id BEFORE INSERT ON test
BEGIN
SET #pn:= ( SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='test'
AND TABLE_SCHEMA=DATABASE() );
SET new.PN= CONCAT('PN-',#pn);
END;
//
delimiter ;
Here
Trying to create a simple trigger that after a table update, it checks if a count (InvalidLogins) is over 4, and if so, sets the field 'LockedOut' to 1. Below is how I am attempting to create the trigger, this is my first trigger for MySQL though and I can't get it to work.
CREATE TRIGGER update_user_lockout after update ON members
FOR EACH ROW SET LockedOut = 1 where invalidLogins < 4
The error that MySQL returns is:
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 '.LockedOut = 1 where invalidLogins < 4' at line 2
delimiter |
CREATE TRIGGER update_user_lockout before update ON members
FOR EACH ROW
IF NEW.invalidLogins < 4 THEN
SET NEW.LockedOut = 1;
END IF;
END
|
delimiter ;
The current record can be referenced with NEW
Choose another delimiter than ;. Otherwise the trigger definition will end at the first ; which is too early
Use a before trigger to change values before updating
I have 2 tables in MySql dealers and dealers_info. I try to create trigger which after deleting info from dealers will delete corresponding rows from dealers info
CREATE TRIGGER del_info AFTER DELETE ON dealers
FOR EACH ROW
BEGIN
DELETE FROM dealers_info WHERE dealer_id = OLD.dealer_id;
END;
But I've got an error:
#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 '' at line 4
change the delimiter to execute the query properly,
DELIMITER $$
CREATE TRIGGER del_info
AFTER DELETE ON dealers
FOR EACH ROW
BEGIN
DELETE FROM dealers_info WHERE dealer_id = OLD.dealer_id;
END $$
DELIMITER ;
I am trying to set up a trigger on a table to copy the contents of a row into another table.
I have the following:
CREATE TRIGGER story_deleted BEFORE DELETE ON stories
FOR EACH ROW BEGIN
INSERT INTO stories_backup SET story_id = OLD.story_id;
END;
This returns the following error though:
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 '' at line 3
I can't work out where I'm going wrong with this. Any ideas?
Try changing the delimiter
DELIMITER $$
CREATE TRIGGER story_deleted BEFORE DELETE ON stories
FOR EACH ROW
BEGIN
INSERT INTO stories_backup SET story_id = OLD.story_id;
END $$
DELIMITER ;
and as far as your privileges go, run this query
SHOW GRANTS;
If SUPER is not there, you could
request your DBA to add that privilege for you
have your DBA create the trigger for you