0

Ok i have a table called motors. When something is inserted in to that table i want a trigger to run that pulls back the last row added to the table, get the fields id, description, partnum and then insert those fields in to a different table called latest.

how would i write this?

thanks

1
  • Have you tried to create something yourself or you expect something you'll copypaste and forget forever? Commented Jul 12, 2011 at 15:46

1 Answer 1

2
CREATE TRIGGER my_trigger AFTER INSERT ON motors
FOR EACH ROW
INSERT INTO latest (motor_id, motor_description, motor_partnum) 
  VALUES (NEW.id, NEW.description, NEW.partnum);

Although, I feel obligated to tell you that if you're just trying to generate a list of the most recent motors, it'll be easier to maintain if you just select the X newest motors from the motors table.

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

3 Comments

So if i read you right, NEW.id, NEW.description, NEW.partnum are the fields from the motor table after it is inserted?
The reason I ask is that the id field is an auto number and is not passed when the the table is inserted to.
Yes, just change the .id, .description, etc to match your actual field names

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.