MySQL Question - How to handle multiple types of users - one table or multiple?

前端 未结 4 1551
独厮守ぢ
独厮守ぢ 2020-12-08 08:39

I\'m designing a database for an organization that has multiple \"types\" of users. At first, I created only one user table. However, while all users share some common infor

4条回答
  •  旧巷少年郎
    2020-12-08 09:13

    You didn't say if you were using a high-level language, so I'll just give a general example with DB-like example:

    Database design is hard. So, this will be a quick and simple answer.

    Your question is a basic question about data relationships, and database design. Search out some basic how-to guides to help this answer. It may help to think of how your information is grouped, and link "back" to the primary set (table) from the other sets (tables).

    So, users are users -- thats your table. It should contain the main, common elements (columns) of data associated with a user.

    Then, this other set of information (e.g., permissions or something) is another table.

    Just make sure this other table has a value (column) that points back to the user, to which it refers. You will probably want to tell your database to create an "index" between them (to improve lookup performances, etc.)

    E.g., A kind of "permission" table for users:

      - integer "id"        <--- unique, index column, auto-increment
      - integer "user_id"   <--- this is which user this belongs
      - ...
      - Boolean "can_write"         <--- example data column
      - Boolean "can_read"          <--- example data column
      - Boolean "can_reboot_system" <--- example data column
      - etc, whatever you want
    

    So, you could "SELECT * FROM user_table WHERE first_name = 'joe' (or such) ... to get a user. In there, I'd hope you have some kind of 'id' value to identify that row.

    Now, just do a 'SELECT * FROM permissions WHERE user_id = 'nnnn' (whatever that user's id is).

    If a user has only 1 permission set, then you could just have that user_id without the additional "id" column.

提交回复
热议问题