I have a question about database ‘style’.
I need a method of storing user accounts. Some users “own” other user accounts (sub-accounts). However not all user accounts are owned, just some.
Is it best to represent this using a table structure like so…
TABLE accounts (
ID
ownerID -> ID
name
)
…even though there will be some NULL values in the ownerID column for accounts that do not have an owner.
Or would it be stylistically preferable to have two tables, like so.
TABLE accounts (
ID
name
)
TABLE ownedAccounts (
accountID -> accounts(ID)
ownerID -> accounts(ID)
)
Thanks for the advice.
I would keep the tables separate.
Self-referencing foreign keys can cause a lot of pain to update/delete.
With the combined table, a cascading delete on the owner will then delete all the owned accounts. (Which may or may not be desirable.) With the separate table, a cascade delete will only delete the relationship that the accounts were owned, and not the accounts themselves.