Babua Pantu
Babua Pantu

Reputation: 31

How do I correct this trigger function?

create trigger trig1
    -> after insert on student
    -> for each row
    -> when (new.name="rgb")
    -> begin
    -> insert into class ("ug1",1)
    -> end;

it tells me that something is wrong near when statement. Can you help me in finding it out?

desc class;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(40) | YES  |     | NULL    |       |
| section | int(11)     | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.06 sec)

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| roll  | int(11)     | YES  |     | NULL    |       |
| name  | varchar(40) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

these are the descriptions for both the tables.

Upvotes: 0

Views: 25

Answers (3)

Babua Pantu
Babua Pantu

Reputation: 31

Right solution is:

DELIMITER @
-> create trigger trig1
-> after insert on student
-> for each row
-> begin
-> if ( new.name = 'rgb')
-> then
-> insert into class values ('ug1',1);
-> end if;
-> end;
 @

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44864

You are missing the delimiter while creating the trigger via the CLI

Here is the syntax to create it -

delimiter //
create trigger trig1 
after insert on student
for each row 
begin
  if new.name='rgb' then
    insert into `class` ('ug1',1);
  end if ;
end//

delimiter;

Upvotes: 0

Rahul
Rahul

Reputation: 77896

Your trigger statement should be like below. See Trigger Syntax and Examples

create trigger trig1
    -> after insert on student
    -> for each row
    -> begin                        //begin should come first
    -> if (new.name='rgb')          //use IF condition
    -> insert into class values('ug1',1); //missing values keyword here
    -> end if;
    -> end;

Upvotes: 1

Related Questions