1

I have 4 tables:

event:
event_id(p.k) | uid | circle_id(f.k)

activity:
uid | performed_activity_id(f.k->event_id) | activity_type_id

follow:
follower_id | circle_id(f.k)

notification:
sender_id | receiver_id(follower_id of follow table)

I want to create a trigger which inserts values into the activity and notification tables whenever there is an entry for event table.
I am able to insert values into the activity table because it is directly connected to event table.

However, I am not able to insert into the notification table because the receiver_id field in the notification table is coming from the follow table which is connected to event table by circle_id.

Here I am using select in trigger which is actually wrong.

DROP TRIGGER IF EXISTS `InsertToActivity` ;
CREATE TRIGGER `InsertToActivity` AFTER INSERT ON `event` 
FOR EACH ROW 
begin 
 INSERT INTO activity( uid, performed_activity_id, activity_type_id ) 
 VALUES (new.uid, new.event_id, '1');
 select follower_id from folow where circle_id=new.circle_id;
 insert into notification_table (sender_id,object_id,receiver_id) 
 values (new.uid,new.event_id,new.follower_id);
end;

Is it good way to do this type of work using TRIGGERS?

ypercubeTM
99.7k13 gold badges217 silver badges306 bronze badges
asked Mar 2, 2013 at 7:10
2
  • Is there a object_id column in notification table? Commented Mar 2, 2013 at 9:41
  • yes there is object_id column which will take event_id Commented Mar 2, 2013 at 10:10

2 Answers 2

1

I assume new.follower_id is supposed to be:

select follower_id from folow where circle_id=new.circle_id;

I don't think this will work. If you want to use this approach you need to declare a variable for follower_id and select into that. However, a better approach (IMO) is to do:

INSERT INTO activity( uid, performed_activity_id, activity_type_id ) 
 VALUES (new.uid, new.event_id, '1');
insert into notification_table (sender_id,object_id,receiver_id)
 select new.uid ,new.event_id, follower_id 
 from folow where circle_id=new.circle_id;
answered May 9, 2018 at 14:09
1

The trigger must be created using DELIMTER like this

DROP TRIGGER IF EXISTS `InsertToActivity` ;
DELIMITER $$
CREATE TRIGGER `InsertToActivity` AFTER INSERT ON `event` 
FOR EACH ROW 
begin 
 INSERT INTO activity( uid, performed_activity_id, activity_type_id ) 
 VALUES (new.uid, new.event_id, '1');
 select follower_id INTO @fid from folow where circle_id=new.circle_id;
 insert into notification_table (sender_id,object_id,receiver_id) 
 values (new.uid,new.event_id,@fid);
end $$
DELIMITER ;

or

# Code from Lenhart's Answer
DROP TRIGGER IF EXISTS `InsertToActivity` ;
DELIMITER $$
CREATE TRIGGER `InsertToActivity` AFTER INSERT ON `event` 
FOR EACH ROW 
begin 
 INSERT INTO activity( uid, performed_activity_id, activity_type_id ) 
 VALUES (new.uid, new.event_id, '1');
 insert into notification_table (sender_id,object_id,receiver_id)
 select new.uid ,new.event_id, follower_id 
 from folow where circle_id=new.circle_id;
end $$
DELIMITER ;
answered Dec 29, 2022 at 19:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.