1

I am working on an application that allows users to manage their hosting accounts. At the moment, the database is just a users table and a hostingaccounts table. However, I do not know what is the best approach to link these to tables together.

Should the users table have a column named "owned_accounts" where the data is inserted like "5,18,28" and then split it in the application? What suggestions do you have?

5 Answers 5

2

if only one user can be the owner of a hosting account, use a foreign key on hostingaccounts like ownerid.

if your hosting account can be owned by multiple users, add an extra table like hostingaccountsownerships with ownershipid, userid, hostingaccountid and fetch from there.

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

4 Comments

your answer and @pablichan's are very similar. Why is it that you suggest creating a ownershipid column? How could it be useful?
the ownershipid column just comes in handy if you want to add some additional information which makes the table useful independently, like how many rights the user has on the hosting accounts. it isn't necessary for your table to work. The answers are similar because this is the known and only way to use n:m relationships in sql, if you need a 1:n relationship, the part of my answer which isnt included in the one of @pablochan is the right way to do.
just clarification: 1:n means 1 user can own some hostingaccounts, n:m means some users share an account and each account has multiple owners.
Marking this as accepted as it was the easiest for me to understand. Thank you!
0

Create a third table that has 2 columns: user_id, account_id. That way one user can be connected to several accounts and vice versa. That way you can easily join these three tables to find out which accounts are available to the user etc.

Comments

0

if a user can have more than one hostingaccount you need a linking table UserId+HostingAccountID as key.

Comments

0

Your solution would work fine if the user to hostingaccounts record was a 1 to 1 relationship, but if there is a chance a user could have more than 1 hostingaccount then the column "user_id" should be in the hosting account table.

If you think you'll need a many to many relationship, then you could create a third table that holds user_id and hosting_account_id.

The best answer really depends on the goals and constraints of your system.

Comments

0

Using your description I see the following entities and relations:

users(
   user_id (PK)
)

accounts(
   account_id (PK)
)

user_accounts(
   user_id    references users(user_id)
  ,account_id references accounts(account_id)
)

Depending on your requirements you can define the key on user_accounts in different ways to enforce the constraints:

1. primary key(user_id)

A user may only own one account. An account may be owned by multple user.

2. primary key(account_id)

A user may own multiple accounts. An account can only be owned by one user.

3. primary key(user_id, account_id)

A user may own multiple accounts, and an account can be owned by multiple users.

If none of the above fit your requirements, you have to give additional information.

Comments

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.