I'm new to the forum, i have the following problem, I want after I delete a lease table locations I go in the table films and have the column situation becomes as available, but if I have the code of the movie in the table location in another register, I still leave the column state of the table film as leased, how would I do that?
that's what I've done
TRIGGER `tguDelete` AFTER DELETE ON `locations` FOR EACH ROW UPDATE movies SET situation = 'available' WHERE code_location = OLD.code_location
I wanted to post pictures of the tables, but I'm new to the forum
Please help me!
I use phpmyadmin, and I am new to mysql
I think your trigger definition required some work. For instance - you'd want to declare the "trigger function" which should execute AFTER the DELETE operations on locations table. This is how you can do it:
DROP TRIGGER IF EXISTS `tguDelete`;
DELIMITER $$
CREATE TRIGGER `tguDelete` AFTER DELETE ON `locations`
FOR EACH ROW
-- condition for rows for which the trigger would fire WHEN (OLD.code_location <> 0)
-- DECLARE
-- if you had things you wanted to declare
BEGIN
-- here you do the updating of the movies table:
UPDATE movies SET situation = 'available' WHERE code_location = OLD.code_location;
END;$$
DELIMITER ;
If you don't need DELIMITERS (ie: PHPMyAdmin) then skip them such as
DROP TRIGGER IF EXISTS `tguDelete`;
CREATE TRIGGER `tguDelete` AFTER DELETE ON `locations`
FOR EACH ROW
-- condition for rows for which the trigger would fire WHEN (OLD.code_location <> 0)
-- DECLARE
-- if you had things you wanted to declare
BEGIN
-- here you do the updating of the movies table:
UPDATE movies SET situation = 'available' WHERE code_location = OLD.code_location;
END;
Related
I need to create a trigger to update the data on another table when an update statement is made on the present table.
I have two tables, a book table, and a borrowed-book table. On the book table, I have an available copies column and on the borrowed-book table, I have a copy and book-status column.
So presently the trigger I am creating is meant to update the book table available-copies column by adding the values with the data on copies column after an update on the borrowed-book, if the book-status on the update is equal to 'returned'.
what do I do:
BEGIN
DEFAULT #status TEXT
SELECT new.status as status from borrowed_book where borrowed_ID=new.borrowed_ID
if status=="returned"
UPDATE books b
set
b.availableCopies=b.availableCopies + New.copies where b.book_ID=new.book_ID
END
You don't need a query to get the status, just use new.status in the IF statement.
DELIMITER $$
CREATE TRIGGER book_returned AFTER UPDATE ON borrowed_books
FOR EACH ROW
IF new.status = 'returned'
THEN
UPDATE books b
SET b.availableCopies=b.availableCopies + New.copies
WHERE b.book_ID=new.book_ID;
END IF;
$$
DELIMITER ;
After trying to create a new trigger in invoices table to UPDATE `invoices` SET invoices.`owes` = (`owes` - `paid`);
I get an error because I already that another trigger in payments that is updating. (see below)
I'm looking to keep the existing trigger below, but how to modify it to also update owes to (owes - paid) in the invoices table.
CREATE TRIGGER `after_payment_update` AFTER UPDATE
ON `payments`
FOR EACH ROW UPDATE `invoices`
SET invoices.`paid` = (SELECT SUM(payments .`payment`)
FROM payments WHERE payments.`invoice` = invoices.`invoice`)
You can't create a second trigger that "triggers" on the same action as another trigger. Instead you would use a DELIMITER $$ statement like below and fill your trigger with all the relevant code you want executed.
DELIMITER $$
CREATE TRIGGER after_update_payments
AFTER UPDATE ON payments
FOR EACH ROW BEGIN
UPDATE invoices
SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
END $$
DELIMITER ;
You don't actually need a DELIMITER in the trigger above, so I will show you an example where you would need to use it:
DELIMITER $$
CREATE TRIGGER after_update_payments
AFTER UPDATE ON payments
FOR EACH ROW BEGIN
IF (some condition here) THEN
UPDATE invoices
SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
END IF;
END $$
DELIMITER ;
As a general rule, if you need to execute multiple statements that need a ; at the end of them, you need to use a DELIMITER. If this still doesn't make sense, a great explanation for delimiters can be found here.
Now, on a side-note, I don't think this approach is the most optimal one. What I would do in your situation is create a view that combines these tables. For example:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`#`localhost`
SQL SECURITY DEFINER
VIEW invoice_payments_view AS (
SELECT
t1.*,
SUM(t2.payment) as amount_paid,
SUM(t2.owes - SUM(t2.payment)) as amount_owed
FROM invoices t1
JOIN payments t2 ON (t1.invoice=t2.invoice)
GROUP BY t1.invoice
)
Then to access the amount_paid and amount_owed columns, you would simply query the following:
SELECT invoice, amount_paid, amount_owed FROM invoice_payments_view
WHERE invoice=1;
Note that I am by no means an expert on this topic, and I am only showing you how I would approach this situation. Also, I didn't test any of this code, so you might need to modify it slightly. If you have any issues let me know and I can update.
I've the following trigger which is really bugging me.
I want to do an UPDATE inside the trigger, but MySQL doesn't allow it, since the statement "triggering" the trigger refer to the participants table.
What can I do in order to make it work? I've considered stored procedures, but they are also forbidden in this case.
The logic is: I've a table with participants in a meeting. The invariant that people from the 'tap' group should always accept the invitation if someone from the 'vip' group is attending. In this case, if a 'vip' is attending the meeting I must UPDATE all 'tap' attendances, but how is this possible???
DELIMITER $$
CREATE TRIGGER tap_meet_vip
BEFORE INSERT ON participants
FOR EACH ROW BEGIN
IF (NEW.pid IN (SELECT userid FROM people WHERE `group` = 'vip')) THEN # new participant is a member of the vip group
UPDATE participants SET `status` = 'a' WHERE pid IN (SELECT userid FROM people WHERE `group` = 'tap') AND meetid = NEW.meetid ; # update status to `accept` of all tap group participants
ELSEIF (NEW.pid IN (SELECT userid FROM people WHERE `group` = 'tap') AND EXISTS (SELECT * FROM participants INNER JOIN people ON pid = userid WHERE `group` = 'vip')) THEN # new participant is a member of the tap group and meeting has vip participant
SET NEW.`status` = 'a';
END IF;
END;$$
The above is written using MySQL and the version is 5.6.
It seems that you can't do all this in a trigger. Trigger is already locked the table so update statement can't get the lock. According to the mysql:
Within a stored function or trigger, it is not permitted to modify a
table that is already being used (for reading or writing) by the
statement that invoked the function or trigger
So you can create a stored procedure, that inserts into/Updates the target table, then updates the other row(s), all in a transaction. With a stored proc you'll manually commit the changes (insert and update).
DROP PROCEDURE IF EXIST marks;
DELIMITER $$
CREATE PROCEDURE marks(IN marks INT)
BEGIN
INSERT INTO first VALUES(marks);
END $$
DELIMITER ;
START TRANSACTION;
INSERT INTO first VALUES (16);
CALL marks(18);
COMMIT;
I am trying to write a MySQL trigger. I have two tables that look like this:
When a customer makes a purchase a new record is added to each table. I have added column ‘sku_copy’ to Table B, so it does not get populated when a new record is created.
When a new record is created, I want my trigger to copy the ‘sku’ field in Table A to the ‘sku_copy’ field in Table B. However, the problem I am having is how to structure the following condition in the trigger.
IF: ‘order_id’ in Table A matches ‘order_id’ in Table B. THEN: copy ‘sku’ from that Table A record to the record in Table B with the matching ‘order_id’. The data should be added to Table B ‘sku_copy'.
ELSE: don’t do anything.
Can someone show me how to write this into my trigger?
Thanks for any help you can give.
Try this but why do u think of using trigger for this ?
DELIMITER $$
CREATE TRIGGER trigger_name
AFTER INSERT ON tableA
FOR EACH ROW BEGIN
INSERT INTO tableB
SET sku_copy = OLD.sku,
order_id = OLD.order_id,
order = OLD.order;
END$$
DELIMITER ;
I want to post the trigger that was constructed with examples offered here and on another forum. Thanks to everyone who helped with this.
DELIMITER $$
CREATE TRIGGER sku_AfterInsert
AFTER INSERT ON uau3h_virtuemart_order_items
FOR EACH ROW BEGIN
UPDATE uau3h_virtuemart_orders
SET order_item_sku_copy = NEW.order_item_sku
WHERE virtuemart_order_id = NEW.virtuemart_order_id;
END$$;
DELIMITER ;
so i've edited my code
i had this problem in another trigger but this time even changing WHERE clause doesn't help
DELIMITER $$
CREATE TRIGGER pic_album_change AFTER UPDATE ON pictures
FOR EACH ROW BEGIN
UPDATE albums SET counter = counter + 1 WHERE albums.id = NEW.album_id;
UPDATE albums SET counter = counter - 1 WHERE albums.id = OLD.album_id;
END $$
DELIMITER ;
error :
<p>Error Number: 1442</p><p>Can't update table 'pictures' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
i dont see any changes on the pictures table in this trigger
i have another trigger that involves these two tables
DELIMITER $$
CREATE TRIGGER album_change
AFTER UPDATE
ON albums
FOR EACH ROW
BEGIN
UPDATE pictures
SET
level = NEW.level
WHERE
pictures.album_id = NEW.id ;
END $$
DELIMITER ;
Your WHERE clause is the wrong way round.
WHERE albums.id = NEW.album_id
would cause albums.counter to be incremented (presumably that's the number of pictures in each album).
It matters because joins on tables are not commutative — the direction of the join is important. Here you need to find the record in albums to update based on the value of the row in pictures.
While in this case there isn't really any ambiguity, SQL needs to follow rules about joins.
The issue with your second trigger is that MySQL is preventing a continuous cycle of updates. You have a trigger pic_album_change which updates albums when pictures is updated. And you have a trigger album_change which updates pictures when albums is updated. Those triggers will trigger each other.
It seems to me that the database design may need to be changed. Do you really need albums.counter when that data can be found with SELECT count(*) FROM pictures WHERE album_id=...? It should be possible to normalise data so that there no circular references in triggers. In fact triggers may not be necessary at all.