How the Insert would look in a table that only has a foreign key

Asked

Viewed 3,699 times

2

I have a user registration and this registration can be done in two ways, by facebook or by email. My question would be how to give the Insert in these tables, because I have a control table that receives the ids of the other two tables by foreign key.

Table:Tabela do meu trabalho

1 answer

4


If the user registers from an email, it would look something like:

INSERT INTO user (id_user, nome, email, senha) VALUES (DEFAULT, "Fulano", "[email protected]", "1234");
INSERT INTO user_controller (id_controller, id_user) VALUES (DEFAULT, LAST_INSERT_ID());

The first line adds user data in user and the second inserts the foreign key into the relation table user_controller. In this case, LAST_INSERT_ID() return the value of id_user generated in the first instruction.

For registration from Facebook would be something very similar:

INSERT INTO user_fb (id_userfb, nome, sobrenome, idfacebook) VALUES (DEFAULT, "Fulano", "Snow", "314159");
INSERT INTO user_controller (id_controller, id_userfb) VALUES (DEFAULT, LAST_INSERT_ID());

In this case, the foreign key would be written in the column id_userfb.

  • Thanks for the help. I have one more question could help me?

  • Just create a new question on the site with her.

Browser other questions tagged

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