Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do I need to create relationships in my database 1

Status
Not open for further replies.

HacH

Programmer
Dec 24, 2002
27
GB
Hi,

I know this is probably a silly question but I'm trying to develop my first php/mysql website and have setup the database tables using phpmyadmin.

Do I need to create the relationships in mysql or do I do that when I write my queries and build that into the queries?

e.g I have a user table and a profiles table. Each user can have multiple profiles.

Thank you for your help.

 
You need some type of connection between the tables. I would assume you have a user ID in your profiles table, that would hold the ID of a user in your users table. That right there is called a relationship.

You can be more explicit and set the id in your profiles table as a foreign key that refers to your users table. This has the benefit of linking the tables, and should you ever delete a user ID from the users table, all related profiles in the profile table would get deleted as well. This is called cascading and helps maintain data consistency so you don't have orphan rows in your profile table.

Whether or not you really need explicit relationships like that only you can know for sure.

You do however require the implicit relationships otherwise there would be no way to construct the queries to get information from both tables in a query.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thank you vacunita, I have included all the foreign keys into the tables but just wondered whether it would be useful to create the relations before or do this when setting up the queries.

You have made a very good point about maintaining integrity. This answers my question.

Thank you for your help and time.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top