Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to program a MySQL trigger to insert row into another table?

I'm looking to create a MySQL trigger on a table. Essentially, I'm creating an activity stream and need to log actions by users. When a user makes a comment, I want a database trigger on that table to fire and:

  1. Grab the ID of the last inserted row (the id of the comment row).
  2. perform an INSERT into an activities table, using data from the last inserted row.

I'll essentially replicate this trigger for deleting comments.

Questions I had:

  1. Is LAST_INSERT_ID() the best way to grab the id?
  2. How do I properly store the data from the last inserted comment row for use in my "INSERT into activities" statement?
  3. Should I be using a combination of stored procedures as well as the trigger?
  4. What would the basic structure of the trigger look like?

Thanks! It's been a few years since I've touched anything to do with DB triggers, procedures and functions.

like image 227
Eric Avatar asked Jan 20 '11 23:01

Eric


People also ask

How do you insert values into a table using triggers?

To create a trigger, we need to change the delimiter. Inserting the row into Table1 activates the trigger and inserts the records into Table2. To insert record in Table1. To check if the records are inserted in both tables or not.

How do you create a trigger insert?

First, we will specify the name of the trigger that we want to create. It should be unique within the schema. Second, we will specify the trigger action time, which should be AFTER INSERT clause to invoke the trigger. Third, we will specify the name of a table to which the trigger is associated.

How do you create a before and after triggers insert query?

In this syntax: First, specify the name of the trigger that you want to create in the CREATE TRIGGER clause. Second, use BEFORE INSERT clause to specify the time to invoke the trigger. Third, specify the name of the table that the trigger is associated with after the ON keyword.


1 Answers

drop table if exists comments; create table comments ( comment_id int unsigned not null auto_increment primary key, user_id int unsigned not null ) engine=innodb;  drop table if exists activities; create table activities ( activity_id int unsigned not null auto_increment primary key, comment_id int unsigned not null, user_id int unsigned not null ) engine=innodb;  delimiter #  create trigger comments_after_ins_trig after insert on comments for each row begin   insert into activities (comment_id, user_id) values (new.comment_id, new.user_id); end#  delimiter ;  insert into comments (user_id) values (1),(2);  select * from comments; select * from activities; 

Edit:

mysql> \. d:\foo.sql  Database changed Query OK, 0 rows affected (0.10 sec)  Query OK, 0 rows affected (0.30 sec)  Query OK, 0 rows affected (0.11 sec)  Query OK, 0 rows affected (0.35 sec)  Query OK, 0 rows affected (0.07 sec)  Query OK, 2 rows affected (0.03 sec) Records: 2  Duplicates: 0  Warnings: 0  +------------+---------+ | comment_id | user_id | +------------+---------+ |          1 |       1 | |          2 |       2 | +------------+---------+ 2 rows in set (0.00 sec)  +-------------+------------+---------+ | activity_id | comment_id | user_id | +-------------+------------+---------+ |           1 |          1 |       1 | |           2 |          2 |       2 | +-------------+------------+---------+ 2 rows in set (0.00 sec) 
like image 141
Jon Black Avatar answered Sep 27 '22 22:09

Jon Black