This is my first attempt at creating a trigger so any help would really be appreciated. I have two tables that look like this:
likes(username, comment_id)
user(username, password, points)
I am trying to create a trigger such that when a user likes a comment (new entry in likes), the point value for that user is incremented by 1. This is what I have attempted:
DELIMITER $$
create trigger update_points
after insert on likes
for each row
begin
SET #username = new.username;
UPDATE user
SET points = points + 1
WHERE username = #username
end$$
I keep getting MySQL errors. Can anyone help me?
I think you are missing a DELIMITER call at the end. And also a semi-colon:
DELIMITER $$
create trigger update_points after insert on likes
for each row begin
SET #username = new.username;
UPDATE user SET points = points + 1 WHERE username = #username;
end$$
DELIMITER ;
Related
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$$
i have written a trigger in mysqlworkbench that should update the product stock after a new line had been added to the order table(my code is in dutch so thats why im explaining this) But it doesn't work, when i try to apply it mysqlworkbench says its sql code contains errors.
Here's the trigger:
CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT` AFTER INSERT ON `bestelregel` FOR EACH ROW
BEGIN
if Product.productnr = NEW.productnr
then
Update Product
Set Hoeveelheid = (Hoeveelheid - NEW.aantal)
Where productnr = NEW.productnr;
end if;
END
What is Product in the if statement? That is, no doubt, generating a syntax error, because it is unrecognized.
I think you intend for the body to be simply:
Update Product p.
Set Hoeveelheid = (Hoeveelheid - NEW.aantal)
Where p.productnr = NEW.productnr;
The if irrelevant.
I would expect the full version to look something like:
DELIMITER $$
CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT`
AFTER INSERT ON `bestelregel` FOR EACH ROW
BEGIN
UPDATE Product p
SET Hoeveelheid = (Hoeveelheid - NEW.aantal)
WHERE p.productnr = NEW.productnr;
END $$
DELIMITER ;
You have delimiter trouble. You need
DELIMITER $$
CREATE ..... TRIGGER ...
BEGIN
END$$
DELIMITER ;
I have two tables from Xenforo:
xf_user
xf_user_authenticate
Table xf_user stores all the info of a user except password hashes, which are stored in xf_user_authenticate.
Both tables have the same column called user_id.
When data is inserted into xf_user_authenticate I need to get the user_id from the new inserted row, then use that user_id to get the username from xf_user and set its value to xf_user_authenticate.
I tried this code, but it doesn't work:
CREATE TRIGGER name_sync AFTER INSERT ON xf_user_authenticate
begin
SELECT 'username' INTO #username FROM xenforo.xf_user WHERE 'user_id'=NEW.user_id;
UPDATE xenforo.xf_user_authenticate SET 'username' = #username;
end
You have few syntax related issues, in addition you are trying to update the same table where you have after insert trigger. You may need to change the trigger to before insert as
delimiter //
create trigger name_sync before insert on xf_user_authenticate
for each row
begin
declare user_name_sel varchar(100);
select user_name into user_name_sel
from xf_user
where user_id = NEW.user_id;
set new.username = user_name_sel;
end;//
delimiter ;
CREATE TRIGGER name_sync BEFORE INSERT ON xf_user_authenticate
BEGIN
DECLARE cUsername VARCHAR(255);
SELECT username INTO cUsername FROM xenforo.xf_user WHERE user_id=NEW.user_id;
SET NEW.username = cUsername;
END
hey guys can someone point me whats wrong in my mysql code
im trying to create a stored procedure called in a trigger
were a user insert a new book in the books table, the bookqty table insert if callNumber does not exist and updates when exist, but in some point the insert query is not working
but the update query works fine
thank you in advance
use librarydb;
drop procedure if exists intoBooksQty;
delimiter $$
create procedure intoBooksQty(in newcallNumber varchar(10))
begin
if not exists (select * from books where callNumber = newcallNumber) then
insert into librarydb.bookqty(callNumber,bookQty,bookqtyOut) values (newcallNumber, 1,0);
else
update bookqty set bookQty = bookQty + 1 where callNumber = newCallNumber;
end if;
end$$
delimiter ;
One option is to make a UPSERT.
DELIMITER //
CREATE PROCEDURE `intoBooksQty`(`newcallNumber` VARCHAR(10))
BEGIN
INSERT INTO `bookqty` (`callNumber`, `bookQty`, `bookqtyOut`)
SELECT `newcallNumber`, 1, 0
FROM `books`
WHERE `callNumber` = `newcallNumber`
ON DUPLICATE KEY UPDATE `bookQty` = `bookQty` + 1;
END//
DELIMITER ;
SQL Fiddle demo
I trying execute an trigger on mysql database.
The command executes successfully, but the trigger not working.
DELIMITER #
CREATE TRIGGER generate_coupon AFTER INSERT ON order
FOR EACH ROW
BEGIN
DECLARE userid, couponvalue INT;
DECLARE couponcode VARCHAR;
SELECT idUser INTO userid FROM indication WHERE email = NEW.email;
SET couponvalue = 20;
SET couponcode = 'abc123';
INSERT INTO coupon(idUser,idOrder,couponvalue,couponcode) values(userid, NEW.id, couponvalue, couponcode);
END#
DELIMITER ;
I suspect your problem arises from the collisions between your variables couponvalue and couponcode with the same-named columns in your coupon table. As documented under Local Variable Scope and Resolution:
A local variable should not have the same name as a table column.
You could simplify your trigger to the following and, in so doing, avoid this problem entirely:
CREATE TRIGGER generate_coupon AFTER INSERT ON order FOR EACH ROW
INSERT INTO coupon
(idUser, idOrder, couponvalue, couponcode)
SELECT idUser, NEW.id, 20, 'abc123'
FROM indication
WHERE email = NEW.email
;