How to use foreign key to create a record in another table

Asked

Viewed 148 times

2

I have two tables users and users_info, would like to use foreign key, for when I create a record in the table users, create a record with the empty fields in the table users_info, only with the id_user filled, it is possible?

  • to create another record, you will need a Trigger

  • @Rovannlinhalis Could you give me an example of how you would do it?

  • I believe you already have the answer below. =]

1 answer

3


I think it would look like this, remember that the table fields user_info shall permit null values.

CREATE TRIGGER tg_user
AFTER INSERT ON users
FOR EACH ROW
  INSERT INTO user_info (id_user, outro_campo, exemplo)
  SELECT NEW.id, NULL, NULL
  FROM users
  WHERE id = NEW.id
  • 1

    I’ll test and give you a feedback ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.