Create a trigger to update a table based on another table - mysql

Please I need your help. I have been working on a trigger statement and it seem not to be working.
I need help on writing a trigger to update a table called membership when a column on another table called serialcode is updated.
DETAILS: I have two tables called membership and serialcode. The membership table has the following columns
id fullname email date state sn
and the serialcode table has only
id sn
I want the sn in membership to be updated with the value of sn in serialcode is updated where the id in membership table is same as the id in serialcode table.
Here is what I have been able to write below
CREATE TRIGGER MembershipSerialCode
BEFORE UPDATE ON serialcode
FOR EACH ROW
BEGIN
if :new.sn != :old.sn
then
UPDATE membership m
set membership.sn = :new.sn
where m.id = :new.id;
end if;
END;
Thank you.
Mike

Can you try this one and let me know if you have luck on implementing it?
CREATE Trigger TriggerName
ON SerialNumberTable FOR UPDATE
AS
IF (Update(sn)) -- This is the column on SerialCodeTable that will be updated first
BEGIN
IF EXISTS (Select i.id
FROM Deleted d INNER JOIN Inserted i
ON d.id = i.id
AND d.sn <> i.sn)
BEGIN
UPDATE MembershipTable SET MembershipTable.sn = i.sn
FROM Inserted i
WHERE MembershipTable.id = i.id
END
END

Related

How can I create a trigger in phpmyadmin, that after insert will update a field in anther table?

I have a table called userlikes that has columns give_like_id, receive_like_id.
I also have a users table with count_likes and id columns. Every time a new record is inserted into userlikes table, I want to decrease by -1 in countlikes table.
userlikes: give_like_id, receive_like_id
users: id, count_likes
I tried this code, but it's not working.
CREATE TRIGGER `update_count_likes`
AFTER INSERT
ON `user_likes`
FOR EACH ROW
UPDATE users
SET users.likes_count = users.likes_count - 1
WHERE users.id = user_likes.give_like_id
hey, the answer for it:
CREATE TRIGGER `update_count_likes` AFTER INSERT ON `user_likes`
FOR EACH ROW UPDATE users
SET users.likes_count
= users.likes_count -1
WHERE users.id=NEW.give_like_id

Problem with inserting values after creating a TRIGGER

I am new in mysql and I am facing a problem I can't overcome, so I need your help.
I have created a trigger that looks like this:
delimiter //
CREATE trigger NEWBOOKTRIGGER138
after insert ON NEW_SALES
FOR each row
BEGIN
IF (SELECT COUNT(*) FROM NEW_SALES NS
INNER JOIN NEW_BOOKS NB ON NS.ISBN=NB.ISBN GROUP BY GENRE)>138 then
update GENRE set genre=GENRE|| "BEST.SELLER";
ELSEif (SELECT COUNT(*) FROM NEW_SALES NS
INNER JOIN NEW_BOOKS NB ON NS.ISBN=NB.ISBN GROUP BY GENRE)<=138 then
update GENRE set genre= GENRE;
end if;
END;//
In order to change the genre title(in table new_books) into genre BEST.SELLER if a book appears more than 138 times in the new_sales table, let's say.
I get a message that the trigger was successfully created.
The problem is that when I try to insert a tuple in new_books table (in order to see if the trigger actually works) I get an Error code 1242: Subquery returns more than 1 row.
I try to insert the tuple this way:
INSERT INTO NEW_SALES(ISBN,cid)
VALUES ("6666666666666",555);
When I drop the trigger the above insert command works just fine,
But how can I test the functionality of the trigger if I can't insert a new tuple?
Thank u in advance guys.
Kostas
You seem to be updating the wrong table, I assume you want to update the genre column in the NEW_BOOKS table. And it only seems necessary to update it for the book that you're currently inserting, which is NEW.isbn, so you don't need a JOIN.
Also, MySQL doesn't use || for concatenation, it uses the function CONCAT().
CREATE trigger NEWBOOKTRIGGER138
after insert ON NEW_SALES
FOR each row
IF (SELECT COUNT(*) FROM NEW_SALES WHERE isbn = NEW.isbn) > 138
THEN
UPDATE NEW_BOOKS AS b
SET genre = CONCAT(genre, "BEST.SELLER")
WHERE b.isbn = NEW.isbn;
You don't need an ELSEIF statement, since you don't need to do anything to keep the genre the same.

Trigger AFTER UPDATE for effected rows only

My Prestashop table ps_stock available has a column called quantity where the stock of the product is stored. This table is updated by an app, changing the values of the quantity column.
I need a trigger in that table. Every time the quantity of a product is changed I need to extract which products are effected on that table to insert them in another table
I have tried a trigger like this:
IF (OLD.quantity <> NEW.quantity)
THEN
INSERT IGNORE INTO ps_ebay_product_modified (id_ebay_product_modified,id_ebay_profile,id_product)
SELECT ps_ebay_product.id_ebay_product,id_ebay_profile,id_product
FROM ps_ebay_product INNER JOIN ps_stock_available
WHERE OLD.quantity <> NEW.quantity;
END IF
What I need is a selection of the products that have been changed.
This should work
CREATE TRIGGER mytrigger AFTER UPDATE ON ps_stock
FOR EACH ROW
BEGIN
-- do some stuff here
END;

Set max value as default value

I'm trying to store the order of the products in a database, and I want every new product added to be at the last position. Basically, I want the "rank" field to be set to MAX + 1, and MAX should be the highest value between the entries having the same "type".
I tried to do with a trigger, but since MySQL won't let me do that since the trigger would be executed on the same table :
**#1442 - Can't update table 'products' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. **
Here is what my trigger was like :
CREATE TRIGGER `productInsert`
AFTER INSERT ON `products`
FOR EACH ROW
BEGIN
UPDATE products
SET rank = (SELECT MAX(rank)
from products
where type_id = NEW.type_id)
where id = NEW.id
Is there any other way to do that?
Thanks
You want a before insert trigger:
CREATE TRIGGER `productInsert`
BEFORE INSERT ON `products`
FOR EACH ROW
BEGIN
set NEW.rank = (select max(rank) + 1
from products p
where p.type_id = NEW.type_id
);
END

Update Table A with content from Table B

Im trying to make a trigger in mySQL that will update a colum with data from another table, when i do a insert.
I got table: rtsEP_groups
With: id, name
and table: rtsEP_users
with: id, groups (and alot of others)
And i want to update "groups" in rtsEP_users, with the "id" from rtsEP_groups where "name" is "Student", when there is a new insert in rtsEP_users.
I was working on this trigger, but not sure if its right, and how to finish it.
How to update the right colum.
CREATE TRIGGER defaultGroup
AFTER INSERT ON rtsEP_users
BEGIN
DECLARE _group_id INTEGER;
SELECT id INTO _group_id FROM rtsEP_groups WHERE name = "Student"
UPDATE rtsEP_users SET groups = _group_id WHERE
Use BEFORE INSERT instead of AFTER, so New is represent of inserted row that you can update.
Try this
CREATE TRIGGER defaultGroup BEFORE INSERT ON rtsEP_users
FOR EACH ROW
SET New.groups = (SELECT id FROM rtsEP_groups WHERE name = "Student");