Jump to content

Trying To Understand Mysql Table Relations.


TheGallery

Recommended Posts

Hello again everyone :) I am pretty inexperienced with SQL and i got a question. Recently i've heard about database relationships and i wanted to clear some things out. Do you have to define foreign keys or we just name them like that because of their use? I'll provide a sample of my database so you can let me know if i got this whole thing right and if i am using the relationships correctly. Moving to more specific questions now. When a user registers, he will be prompted to leave a message, so data should be inserted to "the_people" and "the_messages" the same time. Will the uid_the_people automatically take the value of uid or i should use a different query to insert data to the_messages having as a reference the uid? I am pretty confused right now so this post may be confusing too. You are welcome ask for more details on my question if you need some.

post-72542-0-24730000-1322236306_thumb.png

Link to comment
Share on other sites

You don't need to explicitly define a foreign key, the major advantage to doing that is so that the database enforces that constraint. Some storage engines don't even support foreign keys. The default storage engine in MySQL, MyISAM, does not support foreign key constraints. You would need to use InnoDB or another storage engine if you want to define foreign key constraints.

Will the uid_the_people automatically take the value of uid
No. The queries don't happen at the same time, you don't send 2 queries to MySQL to execute simultaneously. You run one query, then the other one. You should insert the new person first, get their ID, and then insert the new message with the new user ID.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...