snagcheol
snagcheol

Reputation: 61

How to set a trigger which update a field after insertion

I have a table called list which has a id,name and size. Size stores the length of the name field. I want to update this field after insertion. In other words, Trigger after insert ( yes I can do in the code but I just curios about it) Any suggestions?

Upvotes: 1

Views: 126

Answers (1)

Elad
Elad

Reputation: 3130

You should use a BEFORE INSERT trigger. Use the keyword new to refer to the columns about to be inserted:

CREATE TRIGGER my_trigger BEFORE INSERT
ON list FOR EACH ROW BEGIN
SET NEW.size = LENGTH(NEW.name)

Upvotes: 1

Related Questions