Hi I am a bit confused regarding relational databases.
This is what I understand so far:
Let’s say we have two tables users and profile
users : username , password
profile: address , telephone , country , city etc.
Oviously a relationship should be created beetween this two tables because each user has an addres ,telephone etc. But how does MySql or Sql-server know witch addres belongs to what user.
I have seen in some tutorials that an aditional field is created in this case users_id for the profile.What I do not understand is if the items in the field are automaticly generated in regards to the user table?Also how can I be sure I am not retrieving the wrong users profile?
Can anyone please explain this concept to me or point me on some tutorials pls?
What you will need is a Foreign key in any of the table. Since it’s a 1-1 relationship in the example you mention (based on the logic that a user can only have one profile) I would have a field called user_id in the profile table.
When you create a user use it’s inserting-id number when inserting to the profile table.
Example of some bad php code ( do not use this code):