How would the insert be in a table that only has foreign key

2

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

Table:

    
asked by anonymous 02.06.2017 / 04:16

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 the user data in user and the second inserts the foreign key in the user_controller relationship table. In this case, LAST_INSERT_ID() will return the value of id_user generated in the first statement.

To register from Facebook would be 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 id_userfb column.

    
02.06.2017 / 04:52