3

All the examples I've found seem the same as my code, yet I am having a hard time getting it to work. Here's the table,

CREATE TABLE `samfoo` (
`test` varchar(10) default NULL,
`trig_field` varchar(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1

And here's the trigger:

DELIMITER $$

DROP TRIGGER IF EXISTS footrigger$$

CREATE TRIGGER samtrigger
BEFORE INSERT ON samfoo
FOR EACH ROW
BEGIN
  SET trig_field = 1;
END$$

DELIMITER ;

When I run this, I get the error Unknown system variable 'trig_field'. Any ideas?

1 Answer 1

6

It should be SET NEW.trig_field := 1;

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

5 Comments

THANK YOU. For some reason I could not find an example or tutorial, and the documentation (as usual) was way too verbose. What does the := operator mean?
My actual code is SET NEW.date_string := date(time_stamp);, and I'm getting an error: `unknown column 'time_stamp' in 'field list'. Any ideas?
what is time_stamp? If it's a field in the same table, you need to access it like NEW.time_stamp or OLD.time_stamp (in your case since it's before insert trigger OLD. doesn't make sense, it should be NEW.time_stamp).
Wonderful! So what exactly do NEW and OLD refer to? And what should I read to really understand this stuff? I hate hacking it together like this. You've been extremely helpful, by the way.
new and old are pseudo-rows that are available inside row-level triggers and allow accessing new and old values of the modified record respectively. new makes no sense in delete triggers, old - in insert triggers. To summarize, in insert triggers you can access only new, in update - new and old, in delete - only old.

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.