Skip to content
Advertisement

Create a new record for every user in a database based on a column

I have a MySQL database with a user table, after a new requirement I had to create a new table called social_media and from now on every new user will be created with a social_media_id column that holds a reference to their social media.

 +===================+ +===================+
 |   user            | |   social_media    |
 +===================+ +===================+
 |PK id              | |PK id              |
 |FK social_media_id | |   instagram       |
 |   first_name      | |   facebook        |
 |   last_name       | |   twitter         |
 |   email           | +===================+
 +===================+

I want to update my database so that every user that didn’t had a social media reference before gets one (even if the values inside are null), so they can update them if they wish, is there something I can do to make a new social_media record for every user that doesn’t have one, and add the correct social_media_id foreign key for that user?

Advertisement

Answer

Ok @Jorche, this is too long to be a comment, but I do want to help.

First off, this is probably what youre data structure should look like:

enter image description here

Second, to be able to tell you how you enter these records is very difficult for me at this moment because I have absolutely ZERO requirements or any other business logic that would help me to help you on how to pinpoint the best approach to doing so. Odds are, you would have to work hand in hand with application developers, or ETL developers (that might even be you though) to figure out what that approach is. Maybe its a stored procedure that gets called, maybe its a trigger set up, hard to say for sure without additional context, ya know?

All we know at this point is that users exist and sometimes they have relational data related to social media entities. Your job is literally to understand that process flow and make the appropriate decisions on how to log that data in a way that makes sense from both an operational perspective and a database design perspective.

Hate to say it hombre, but the questions you have now are all entirely dependent on details you haven’t provided.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement