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
Related
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
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;
I have a table in MYSQL database with two fields:
Id (auto increment field).
Post_Id.
When I insert a new record both fields should have the same value. So I should update post_id with Id value, and at the same time make sure that I update the field with the right value not with any other new inserted record value.
I tried this SQL statement but it was very slow and I was not sure that I select the right value:
set #auto_id := (SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='table_name'
AND TABLE_SCHEMA=DATABASE() );
update table_name set post_id= #auto_id where id=#auto_id ;
I don't have long experience with MySQL and I cannot change the table structure .
The approach you followed is not transaction safe as well.
The best option I can think about is to use trigger
Edit: According to #lagripe's mentionings
CREATE TRIGGER sometrigger
AFTER INSERT ON sometable
BEGIN
SET NEW.post_id := (SELECT id from sometable order by DESC limit 1) + 1 ; // you may need +1 here. I couldn't test it.
END
or you may consider to use LAST_INSERT_ID
insert into table_name values ( .... );
update table_name set post_id = LAST_INSERT_ID();
but why do you need two columns with the same id at first place?
if you really need why don't you use computed/generated columns?
CREATE TABLE Table1(
id DOUBLE,
post_id DOUBLE as (id)
);
you can use triggers :
CREATE TRIGGER UpdatePOST_id
BEFORE INSERT ON table_db
FOR EACH ROW
SET NEW.post_id := (select id from table_db order by id DESC LIMIT 1)+1 ;
from now on, whatever you insert your as a value in post_id column will be replaced with the id inserted automatically.
Test :
|id|post_id|
|20| 20 |
|21| 21 |
|22| 22 |
|23| 23 |
To drop the trigger :
DROP trigger UpdatePOST_id
I have an employee table (em) that contains, among other things, a floor Id (fl_id) and room Id (rm_id). In certain circumstances (when em_loc_cnt = 0) I want to set the em record's fl_id and rm_id to null. Below is my code so far.
I am not sure how to refer to the fl_id & rm_id in the commented line. Will I run into issues because this trigger is being called as a result of the em record being updated and I am updating that same record in the trigger?
Suggestions?
IF EXISTS (SELECT * FROM [sysobjects] WHERE [name] = 'em_upd_self_serv_t' AND [type] = 'TR')
BEGIN
DROP TRIGGER [dbo].[em_upd_self_serv_t]
END
GO
CREATE TRIGGER [dbo].[em_upd_self_serv_t]
ON [dbo].[em]
AFTER UPDATE
AS
BEGIN
DECLARE #em_id VARCHAR(35),
#em_loc_cnt INT;
SET #em_id = (SELECT em_id FROM inserted);
SET #em_loc_cnt = (SELECT COUNT(*) FROM emlocs WHERE em_id = #em_id);
IF (#em_loc_cnt = 0)
BEGIN
-- I want to set the fl_id and the rm_id to NULL
END
END;
Your fundamental flaw is that you seem to expect the trigger to be fired once per row - this is NOT the case in SQL Server. Instead, the trigger fires once per statement, and the pseudo table Inserted might contain multiple rows.
Given that that table might contain multiple rows - which one do you expect will be selected here??
SET #em_id = (SELECT em_id FROM inserted);
It's undefined - you might get the values from arbitrary rows in Inserted.
You need to rewrite your entire trigger with the knowledge the Inserted WILL contain multiple rows! You need to work with set-based operations - don't expect just a single row in Inserted !
You need to change your code to something like this:
IF EXISTS (SELECT * FROM sys.triggers WHERE [name] = 'em_upd_self_serv_t')
DROP TRIGGER [dbo].[em_upd_self_serv_t]
GO
CREATE TRIGGER [dbo].[em_upd_self_serv_t]
ON [dbo].[em]
AFTER UPDATE
AS
UPDATE dbo.em
SET fl_id = NULL, rm_id = NULL
FROM Inserted i
WHERE em_id = i.em_id
AND NOT EXISTS (SELECT * FROM dbo.emlocs WHERE em_id = i.em_id)
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");