HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

MySQL: How to make Trigger to insert row in another table?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
triggerinsertmakemysqlanotherhowrowtable

Problem

Please read everything, included the notes and edits at the end

Problem

I have a MySQL DB with 3 different table:

  • To save users



mysql> describe users;
+-------------+-----------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------------------------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| firstname | varchar(255) | NO | | NULL | |
| lastname | varchar(255) | YES | | NULL | |
| username | varchar(255) | NO | UNI | NULL | |
| pswd | varchar(255) | NO | | NULL | |
| permissions | enum('admin', 'student') | NO | | NULL | |
| active | tinyint(1) | NO | | 1 | |
+-------------+-----------------------------------+------+-----+---------+----------------+
7 rows in set (0,01 sec)


  • To save students




mysql> describe students;
+--------------+--------------+------+-----+--------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+--------------+-------------------+
| id | int | NO | PRI | NULL | |
| firstname | varchar(255) | NO | | not assigned | |
| lastname | varchar(255) | YES | | NULL | |
| id_number | int | NO | UNI | -1 | |
| registration | date | NO | | curdate() | DEFAULT_GENERATED |
+--------------+--------------+------+-----+--------------+-------------------+
5 rows in set (0,01 sec)


  • To s

Solution

I want to create an after insert trigger for users to insert a row in
one of the others, based in the field users.permissions.

This should be a simple if else condition.

The trigger verifies the permissions inserted value, if equal to admin then insert into admins table otherwise insert into the students table, as your datatype was enum and I expect the other value to be student .

create trigger insert_admin_or_srudent after insert on users for each row
begin
  if new.permissions='admin'
    then insert into admins (firstname,lastname) values (NEW.firstname,NEW.lastname);
     else insert into students (firstname,lastname,username) values (NEW.firstname,NEW.lastname,NEW.username);
End if;
end


Data example

create table users (
    id int  ,                     
    firstname varchar(255),        
    lastname varchar(255),
    username varchar(255),
    pswd varchar(255),
    permissions enum('admin', 'student'),
    active tinyint(1)
 );
 
 create table students (
    id int  ,                     
    firstname varchar(255),        
    lastname varchar(255),
    username varchar(255),
    id_number int,
    registration date 
 );
 
  create table admins (
    id int  ,                     
    firstname varchar(255),        
    lastname varchar(255),
    registration date 
 );

insert into users values 
(1,'test1','test1','tst1','tst1','admin',1),
(1,'test2','test2','tst2','tst2','student',1);


Working example

Code Snippets

create trigger insert_admin_or_srudent after insert on users for each row
begin
  if new.permissions='admin'
    then insert into admins (firstname,lastname) values (NEW.firstname,NEW.lastname);
     else insert into students (firstname,lastname,username) values (NEW.firstname,NEW.lastname,NEW.username);
End if;
end
create table users (
    id int  ,                     
    firstname varchar(255),        
    lastname varchar(255),
    username varchar(255),
    pswd varchar(255),
    permissions enum('admin', 'student'),
    active tinyint(1)
 );
 
 create table students (
    id int  ,                     
    firstname varchar(255),        
    lastname varchar(255),
    username varchar(255),
    id_number int,
    registration date 
 );
 
  create table admins (
    id int  ,                     
    firstname varchar(255),        
    lastname varchar(255),
    registration date 
 );

insert into users values 
(1,'test1','test1','tst1','tst1','admin',1),
(1,'test2','test2','tst2','tst2','student',1);

Context

StackExchange Database Administrators Q#324356, answer score: 2

Revisions (0)

No revisions yet.