Database design: 1 table or 2?

后端 未结 4 1663
我寻月下人不归
我寻月下人不归 2021-01-23 21:22

I have seen a few database designs where it has all user information in an account table, including password, email, DOB, First Name, Last Name etc.

I have seen some o

4条回答
  •  攒了一身酷
    2021-01-23 21:32

    The difference between the two designs is mostly one of flexibility. If the account and user data share a single table, then each user must have an account, and each account can have only one user (unless you add another table to allow child users to be added in addition to the user that lives with the account data, or unless you add new records with duplicate account details in each one, which is very bad and antithetical to what databases are supposed to do).

    With two tables, you can easily have multiple users in each account, and might also choose to allow circumstances where an account has no users, or where a user does not have an account, if doing so would benefit your use-case.

    The tradeoff is that if you want to do something like determine the account for a user (or the user(s) in an account), you have to do a join if you are using two tables. If you have one table all you have to do is fetch the row to get this information.

提交回复
热议问题