1

Hey all, I am setting up a PHP web app that will make use of subdomains for accounts. I am storing subdomains in a MySQL table with the following fields:

subdomain_id | owner_id | name | date_created

owner_id maps back to user_id in the user table The user table has the following fields:

user_id | email_address | etc...

Now I am trying to figure out what is the best way to store which users have access to which subdomain. Is the best to set up another table with the following fields?

id | subdomain_id | user_id

That would contain data such as the following (showing user #6 has access to subdomains 4 & 7):

id | sudomain_id | user_id
1  | 4           | 6
2  | 4           | 23
3  | 7           | 6

Is there a more efficient way?

1 Answer 1

2

That is the correct way to model a many-to-many relationship, but the id column is entirely unnecessary. You don't need to give every table an artificial identifier. The primary key of that table is simply (subdomain_id, user_id).

Sign up to request clarification or add additional context in comments.

5 Comments

I agree with Dan to a certain extent. Surrogate keys are often not necessary but are not a bad practices, especially if that table's role grows to more than just a relation table in the future. Perhaps permissions will be attached to a user's access to a subdomain in the future, then this table will need to have a 1 to many relationship with a permission table. If I were you, I would leave the surrogate key in the table.
@Zoidberg agreed. I intend to leave the surrogate key, but understand what Dan was getting at.
if this answer is satisfactory, be sure to accept it as the correct answer.
@Zoidberg will do, giving me a 6minute wait time to accept however.
I did not know that, learn something new every day, good luck with your database design

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.