1

Suppose I have the following table

create table try  ( name varchar(10), username varchar(10))

Is it possible to have a trigger 'ON INSERT' to set username=user() ?

I tried doing

create trigger userIns ON INSERT on try for each row set new.username=user();

but this gives some syntax error

I have seen documentation about BEFORE INSERT,AFTER INSERT. How about ON INSERT?

if not, whats my best bet?

1
  • just use after insert and correct syntax, it's probably what you want. Commented Feb 27, 2013 at 4:11

4 Answers 4

3

The syntax is :

CREATE TRIGGER trigger_name trigger_time trigger_event
ON table_name
FOR EACH ROW
BEGIN
...
END

Your syntax is wrong in trigger_time part.

Trigger activation time can be BEFORE or AFTER. You must specify the activation time when you define a trigger. You use BEFORE keyword if you want to process action prior to the change is made on the table and AFTER if you need to process action after change are made.

Try this :

create trigger userIns BEFORE INSERT on try for each row set new.username=user();

More : MYSQL Trigger Tutorial

Sign up to request clarification or add additional context in comments.

Comments

0

Correct Syntax is

Create Trigger trigger_name trigger_time trigger_event
on table_name
for each row
begin
....

end

Trigger_time => Before | After trigger_event => Insert | Update | Delete

Please check

create trigger userIns INSERT on try for each row begin set new.username=user() end;

Comments

0

None of the above was my problem. I was really looking for an ON INSERT trigger - but it does not exist in MYSQL. So I used BEFORE INSERT and that works fine

create trigger userIns BEFORE INSERT on try for each row set new.CREATED_BY=user();

Comments

0

The problem is you need to set the delimiter to something other than ';' before you begin the trigger code. Put the new delimiter after 'end' and then change the delimiter back to ';' after you are done.

Comments

Your Answer

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.