0

In MySQL, is it possible to have two tables linked by a foreign key and when a record is inserted into the parent table to create a corresponding record in the child table?

I have a website that handles member registrations. One table contains the member's details such as name and email address, and is linked to a settings table via member ID. What I would like is for a corresponding record to be entered into the settings table automatically when I create a new member. Is this possible? Or will that involve a stored procedure?

2 Answers 2

4

I think what you might need is a trigger.

http://dev.mysql.com/doc/refman/5.0/en/triggers.html

CREATE TRIGGER ins_settings AFTER INSERT ON members
  FOR EACH ROW BEGIN
    INSERT INTO settings SET member_id = NEW.member_id
    ...
  END;
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks to Greg, I managed to arrive at the solution, which is:

CREATE TRIGGER ins_settings AFTER INSERT ON members
FOR EACH ROW
INSERT INTO settings SET member_id = NEW.member_id;

1 Comment

you can click on the tick just to the left of his answer to thank him properly :)

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.