I have two tables as described below. I made a trigger which must be executed on the sgr_customer_group table after a new insert into sgr_customer table:
After a new insert into sgr_customer, a new record with id_customer (Primary key of sgr_customer) and the constant 4 must be inserted into sgr_customer_group table.
DELIMITER $$
CREATE TRIGGER sgr_customer_trig
AFTER INSERT ON `sgr_customer` FOR EACH ROW
begin
DECLARE id_exists Boolean;
SELECT 1
INTO #id_exists
FROM sgr_customer
WHERE sgr_customer.id_customer= NEW.id_customer;
IF #id_exists = 1
THEN
Insert into sgr_customer_group (id_customer, id_group)VALUES(id_customer,4);
END IF;
END
$$
DELIMITER ;
Table definitions:
sgr_customer_group(id_group,id_customer#,id_group );
id_group:Primary Key
sgr_customer(id_customer,......);
id_customer:Primary key
Great Thanks.
Everything #Uueerdo wrote in their comment to your question is correct, so the trigger can be simplified to:
DELIMITER $$
CREATE TRIGGER sgr_customer_trig
AFTER INSERT ON `sgr_customer` FOR EACH ROW
BEGIN
INSERT INTO sgr_customer_group (id_customer, id_group) VALUES(NEW.id_customer, 4);
END
$$
DELIMITER ;
Related
I'm trying to create a trigger that updates a row in another table on an insert. The last part works but if there is no row with that id, I want to create a row for that id before adding to it.
But I get a #1064 error: 'resource_xpEvents FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT 1 FROM resource_xp '
DELIMITER $$
CREATE TRIGGER test AFTER INSERT resource_xpEvents FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT 1 FROM resource_xp WHERE userId = NEW.userId) THEN
INSERT INTO resource_xp (userId)
VALUES (NEW.userId);
END IF;
UPDATE resource_xp
SET xp = xp + new.delta
WHERE userId = new.userId
END $$
DELIMITER ;
Try this:
DELIMITER $$
CREATE TRIGGER test AFTER INSERT ON resource_xpEvents FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT 1 FROM resource_xp WHERE userId = NEW.userId) THEN
INSERT INTO resource_xp (userId)
VALUES (NEW.userId);
END IF;
UPDATE resource_xp
SET xp = xp + new.delta
WHERE userId = new.userId;
END $$
DELIMITER ;
If you want to experiment:
DELIMITER $$
CREATE TRIGGER test AFTER INSERT ON resource_xpEvents FOR EACH ROW
BEGIN
REPLACE INTO resource_xp (userId,xp) values (NEW.userId,xp+new.delta);
END $$
DELIMITER ;
which is more readable, but performance are worse.
You can try using MySQL's INSERT ... ON DUPLICATE KEY UPDATE syntax here. First, if the userId column is not already unique, make it unique:
ALTER TABLE resource_xp ADD CONSTRAINT UNIQUE (userId);
Then, instead of trying to do a separate conditional insert and update statement, just do the following single statement:
INSERT INTO resource_xp (userId)
VALUES (NEW.userId)
ON DUPLICATE KEY UPDATE xp = xp + NEW.delta
Here is code for the full suggested trigger:
DELIMITER $$
CREATE TRIGGER test AFTER INSERT resource_xpEvents FOR EACH ROW
BEGIN
INSERT INTO resource_xp (userId)
VALUES (NEW.userId)
ON DUPLICATE KEY UPDATE xp = xp + NEW.delta
END $$
DELIMITER ;
I have to do two triggers, first one, after insert in test1 table, to insert in test2 table another row. All right, the problem is that i have to do same trigger in reverse form, after insert row in test2 table, insert new row on test1. I know that trigger can't insert in a table when a trigger is firing, but i need resolve this. I don't have a lot experience with triggers, i hope that this could be resolved.
DELIMITER $$ CREATE TRIGGER `ai_test1_test2` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN INSERT INTO test2 (vtest2) VALUES (new.vtest1)
END$$
DELIMITER ;
Bidirectional trigger:
DELIMITER $$ CREATE TRIGGER `ai_test2_test1` AFTER INSERT ON `test2`
FOR EACH ROW
BEGIN INSERT INTO test1 (vtest1) VALUES (new.vtest2)
END$$
DELIMITER ;
Thanks.
Edit:
The infinite loop can't be resolved. Triggers can't be bidirectional... I think that i must be use a PROCEDURE...
The infinite loop can't be resolved. Triggers can't be bidirectional... I think that i must be use a PROCEDURE...
There is a posible solution for the problem:
DELIMITER $$ CREATE TRIGGER `ai_test1_test2` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN
IF NOT EXISTS( SELECT vtest2 FROM test2 WHERE vtest2 = new.vtest1 ) THEN
INSERT INTO test2 (vtest2) VALUES (new.vtest1)
END IF;
END$$
DELIMITER ;
DELIMITER $$ CREATE TRIGGER `ai_test2_test1` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN
IF NOT EXISTS( SELECT vtest1 FROM test1 WHERE vtest1 = new.vtest2 ) THEN
INSERT INTO test1 (vtest1) VALUES (new.vtest2)
END IF;
END$$
DELIMITER ;
I want to create a trigger in mysql which insert a row in a table change_history when there is any update in another table event_data if event_data.title='event_media'.
DELIMITER $$
CREATE TRIGGER `after_evdata_update`
AFTER UPDATE ON event_data
FOR EACH ROW
BEGIN
if OLD.title <=> 'event_media'
INSERT INTO change_history (badge,city,country,venue,company,
date,rehosted,type,cancelled,name,tagline,description,agenda,pricing,
edition,photo_video,exhibitors,speakers,official_url,twitter_handle,
twitter_hashtag,facebook_url,contact,post_review_id) values(1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,null);
end if;
END $$
DELIMITER ;
I think this should work. Please try.
BEGIN
IF (OLD.title <> 'event_media')
THEN
INSERT INTO `change_history` (`xxx_id`,`and_all_other_fields_similar_way`)
VALUES (old.xxx_id,old.and_all_other_fields_similar_way);
END IF;
END
I have table "book" and "store_order" have relation
I want to make trigger(but it contain error):
DELIMITER $$
CREATE TRIGGER t1
before delete ON store_order
FOR EACH ROW
BEGIN
update book set number = number + NEW.quantity where ISBN = NEW.ISBN;
END
$$
DELIMITER ;
DELIMITER $$
CREATE
TRIGGER t2 AFTER delete
ON library.store_order
FOR EACH ROW BEGIN
update library.book
set library.book.number = (library.book.number + OLD.quantity)
where library.book.ISBN = OLD.ISBN;
END$$
DELIMITER ;
Use OLD instead of NEW when you want to get the deleted object.
for an example of my case. I'm getting the id of the newly added role by calling
NEW.id
and getting the same field's value while deleting by calling
OLD.id
Example:
DELIMITER $$
CREATE TRIGGER after_insert_role
AFTER INSERT ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping`
(`operation_type`, `table_name`, `oid`, `end_point`)
VALUES
('insert', 'role', NEW.id, 'new/role');
END $$
DELIMITER $$
CREATE TRIGGER after_delete_role
AFTER DELETE ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping` (`operation_type`, `table_name`, `oid`, `end_point`) VALUES
('delete', 'role', OLD.id, 'delete/role');
END $$
Whenever we are deleting the data
USE
OLD. instead of NEW.
CREATE TRIGGER user_history_delete AFTER DELETE ON user FOR EACH ROW
INSERT INTO user_history(action , name )
VALUES ("Delete" , OLD.name );
My trigger will not work.
Basically when a user_briefcase is created i want to also create a row in user_complementary_info if it not already exist, where user_complementay.user_id = the newly created user_briefcase.user_id
My trigger
DROP TRIGGER IF EXISTS complementary_user_info
DELIMITER $$
CREATE TRIGGER complementary_user_info AFTER INSERT user_briefcases FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) user_complementary WHERE user_id=NEW.user_briefcases.user_id) = 0 THEN
INSERT INTO user_complementary (user_id)
VALUES (NEW.user_briefcases.user_id)
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS complementary_user_info;
DELIMITER $$
CREATE TRIGGER complementary_user_info AFTER INSERT user_briefcases FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT 1 FROM user_complementary_info WHERE user_id = NEW.user_id) THEN
INSERT INTO user_complementary (user_id)
VALUES (NEW.user_briefcases.user_id);
END IF;
END $$
DELIMITER ;