In my current project in PHP and MySQL, I have a database of user and a database of projects. I want to be able to assign projects to users so that they only have access to those that they have joined (by whatever means). Is there a more efficient way of doing this other than having some sort of array within a field of a user?
1 Answer
Create a table with two foreign keys: one pointing to the primary key of the user table and the other pointing to the primary key of the project table. This is the standard way of implementing so called m:n-relations (a user can have multiple projects and a project consists of multiple users) in relational databases. Read up on database normalization for reasons why.
5 Comments
Sebastian
Thanks for your reply. Do the foreign keys have to be called anything specific or are they just numbers?
Sebastian
Also, I'll be adding a sort of shoutbox and various other dynamic content on each project page - where is the best place to store all all of this?
Oswald
The type and name of foreign keys is up to you, but they must have the same type as the primary key that they refer to and should be named such that it is clear to the reader which primary key they refer to.
Oswald
Do not think in terms of pages when designing the database. Think in terms of entities, their attributes and relations to other entities. See Entity-relationship model for details.
Sebastian
After linking them, how do I correspond one to the other. For example, how do I change what projects a user is subscribed to?