I am trying to update a column ( called DateModified ) before update happens on the table row.
so here is my trigger:
CREATE TRIGGER `date_mod_category` BEFORE UPDATE ON `categories`
FOR EACH ROW BEGIN
SET new.DateModified = NOW();
END
BUT I GET THIS DAMN ERROR WHICH I JUST CANNOT FIGURE OUT WHY:
Use DELIMITER
DELIMITER //
CREATE TRIGGER `date_mod_category` BEFORE UPDATE ON `categories`
FOR EACH ROW
BEGIN
SET new.DateModified = NOW();
END //
DELIMITER ;
Related
Here's my trigger. Updating a row in event does not set a date/time string in sf_timestamp. New to triggers so I am not sure how to debug.
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END;;
DELIMITER $$
Suggestions?
When you define a DELIMITER, you need to keep that same delimiter until you are done with the block you are trying to declare. In this case, you are trying to create a new trigger and you need MySQL to interpret the whole block with multiple lines as one and ending them with the default ;
But when you are done, you have to END the block with the DELIMITER you have set earlier as following:
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END$$
DELIMITER ;
Unless your update statement includes a value for sf_timestamp, that field likely doesn't have a value to be formatted. If you are attempting to update the timestamp to represent the time when the record is updated, why not just use the current time?
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp` BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = now();
END;;
DELIMITER $$
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END$$
DELIMITER ;
Followed #pensums suggestion and this worked for me
How can I use MySQL Trigger to update quantity field in items table with quantity-quantity_delivered when quantity_delivered in the requests table is updated ?
Here Is My Query, I have tested but I see its not getting updated, can any one help me please?
DELIMITER $$
CREATE TRIGGER items_update AFTER UPDATE ON requests
FOR EACH ROW
BEGIN
UPDATE items
SET items .quantity = items.quantity - requests.quantity_delivered
WHERE items .item_id = requests.item_id;
END;
$$
DELIMITER ;
I have resolved by myself like this and its working for me thanks All!
DELIMITER $$
CREATE TRIGGER `quantityTrigger` AFTER UPDATE ON `requests` FOR EACH ROW BEGIN
UPDATE items i
SET i.quantity=i.quantity-NEW.quantity_delivered
WHERE i.item_id=NEW.item_id;
END
$$
DELIMITER ;
You should be using the new. values in your trigger like this
drop trigger if exists items_update;
DELIMITER $$
CREATE TRIGGER items_update AFTER UPDATE ON requests
FOR EACH ROW
BEGIN
UPDATE items
SET items.quantity = items.quantity - new.quantity_delivered
where items.item_id = new.item_id;
END;
$$
DELIMITER ;
As an aside should this be an after insert trigger?
I am trying to implement the following trigger but I am getting a MySQL syntax error. The idea is that after each time the table is updated I want to set the updated 'valid' field to 0 if the 'banned' field has been changed to 1. Also is the logic below correct?
Create Trigger `customers` Before Update on `customers` for each row BEGIN
set new.valid = 0 WHERE new.banned = 1;
END;
Yes the trigger should be something as
delimiter //
Create Trigger `customers_update`
Before Update on `customers`
for each row
BEGIN
IF new.banned = 1
THEN set new.valid = 0 ;
END IF ;
END;//
As you can see I have used delimiter in the trigger which is needed for the trigger. There is a nice explanation of this here What does DELIMITER // do in a Trigger?
The default initial value of one column in my database is the same as the row's auto-incremented id. I'm trying to use triggers to set it.
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
BEGIN
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
END
But this keeps throwing a syntax 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 5
I've tried all sorts of permutations of this with no luck. Can anyone see what I'm doing wrong?
As zerkms said, you need to change the delimeter. But since you only use 1 line of code, you don't need the BEGIN and END. And that way, you don't need to change the delimiter either
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
Since you are getting an error you cannot update the row, I suggest the following:
Do NOT perform the update query at all. On default the order value = the ID value. So when the order value changes, you can update it properly.
If you are requesting the data with php, do something like this:
$order = $row['order'];
if ($order == '')
$order = $row['id'];
After you need it updating, you've got the correct value.
I don't think you can do that. An AFTER INSERT trigger cannot modify the same table, neither by issuing an UPDATE nor by something like this:
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
SET NEW.`order` = NEW.id ;
which results in this error:
> Error Code: 1362. Updating of NEW row is not allowed in after trigger
You can't either use a BEFORE INSERT trigger because then the NEW.id is not known (if you modify the above, the order column will get 0 value after the Insert.
What you can do, is use a transaction:
START TRANSACTION ;
INSERT INTO clusters (id)
VALUES (NULL);
UPDATE clusters
SET `order` = id
WHERE id = LAST_INSERT_ID();
COMMIT ;
You get the error because mysql treats ; in line 5 as the end of your trigger declaration, which obviously leads to the syntax error.
So you need to redefine delimiter before you specify the trigger body:
delimiter |
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
BEGIN
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
END;
|
delimiter ;
You can create just BEFORE INSERT TRIGGER, it's works like this:
CREATE TRIGGER `default_order_value`
BeFORE INSERT ON `clusters`
FOR EACH ROW
BEGIN
SET NEW.`order` = NEW.id ;
END
same as below we are using
DELIMITER $$
USE `e_store`$$
DROP TRIGGER /*!50032 IF EXISTS */ `Test`$$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `Test` BEFORE INSERT ON `categories`
FOR EACH ROW
BEGIN
DECLARE vtype VARCHAR(250) DEFAULT NULL;
SET vtype = NEW.name;
IF (NEW.MDNAME IS NULL)
THEN
-- SET NEW.MDNAME = 'NA';
SET NEW.MDNAME=MD5(NEW.name);
END IF;
END;
$$
DELIMITER ;
This worked for me:
CREATE TRIGGER `update_table_2`
AFTER UPDATE ON `table_1`
FOR EACH ROW
UPDATE table2
JOIN table_1
SET table_2.the_column = NEW.the_column
WHERE table_2.auto_increment_field = OLD.auto_increment_field
im using the following trigger to update the user table in another database in mysql 5.0.7
The creation of trigger gives no error but upon updating the user table in the first database the trigger is not working. Any suggestions?
DELIMITER $$
DROP TRIGGER IF EXISTS after_update_user;
CREATE TRIGGER after_update_user;
AFTER UPDATE ON db_test.user FOR EACH ROW;
BEGIN
UPDATE TABLE db_testplus.user;
SET name = NEW.name;
WHERE id = NEW.id;
END
$$
DELIMITER ;
I also used this code without the semicolons but still the same
DELIMITER $$
DROP TRIGGER IF EXISTS after_update_user
CREATE TRIGGER after_update_user
AFTER UPDATE ON db_test.user FOR EACH ROW
BEGIN
UPDATE TABLE db_testplus.user
SET name = NEW.name
WHERE id = NEW.id
END;
$$
DELIMITER ;
Finally the code that worked
delimiter |
DROP TRIGGER IF EXISTS after_update_user|
CREATE TRIGGER after_update_user AFTER UPDATE ON db_test.user
FOR EACH ROW BEGIN
UPDATE db_testplus.user SET name = NEW.name WHERE id = NEW.id;
END;
|
delimiter ;
Could you please check below
AFTER UPDATE ON db_test.user FOR EACH ROW
BEGIN
UPDATE TABLE db_testplus.user
SET name = NEW.name
WHERE id = NEW.id
END;
Try this;
CREATE TRIGGER after_update_user
AFTER UPDATE ON db_test.user FOR EACH ROW
UPDATE TABLE db_testplus.user
SET name = NEW.name
WHERE id = NEW.id;
Omitting begin-end keywords worked for me.
This works for me in MySQL 5.1.73:
CREATE TRIGGER `after_update`
AFTER UPDATE ON `test_table`
FOR EACH ROW UPDATE another_db.updated_table
SET some_name = NEW.some_name
WHERE id = NEW.id