database 3 个不同表中的用户、客户、管理员帐户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3511782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
User, customer, admin account in 3 different tables?
提问by never_had_a_name
In my web application I will have three types of accounts.
在我的 Web 应用程序中,我将拥有三种类型的帐户。
- User: for using the web application for free
- Customer: for advertising and getting a Company Logo
- Admin: for editing and deleting stuff
- 用户:用于免费使用网络应用程序
- 客户:用于广告和获得公司标志
- 管理员:用于编辑和删除内容
Should all these three be in separate tables or in one with a column named "account_type" where i can mark it as User, Customer or Admin?
这三个应该在单独的表中还是在一个名为“account_type”的列中,我可以将其标记为用户、客户或管理员?
What are the pros and cons for both? Is there a best practice for this?
两者的优缺点是什么?是否有最佳实践?
Thanks
谢谢
回答by Damir Sudarevic
In general, a person
can be user, customer and admin -- so, I would start with a Person
table with columns IsCustomer
, IsUser
, IsAdmin
. Later (for fast search) you may decide to add separate tables Admin
, Customers
, Users
with FK to the Person
table.
通常,aperson
可以是用户、客户和管理员——因此,我将从一个Person
包含列IsCustomer
, IsUser
,的表开始IsAdmin
。稍后(为了快速搜索)您可以决定将单独的表 Admin
, Customers
,Users
和 FK 添加到Person
表中。
EDIT:
编辑:
A typical case may be:
一个典型的案例可能是:
- 5 million users
- 1000 customers
- 10 admins
- 500 万用户
- 1000个客户
- 10 位管理员
In general, having separate tables for customers and admins should speed-up any admin/customer related query.
一般来说,为客户和管理员设置单独的表应该会加快任何与管理员/客户相关的查询。
回答by David
If a user can only be one type, you'd be better off with one table and a bit field for IsAdministrator, etc.
如果一个用户只能是一种类型,那么最好使用一个表和一个位域 IsAdministrator 等。
If a user can be of more than one account type, you should then have a different table with a foreign key,
如果一个用户可以是多个帐户类型,那么您应该有一个带有外键的不同表,
sample structure (data sypes are SQL Server and suggested only)
示例结构(数据类型为 SQL Server,仅建议使用)
Users table
用户表
- UserID - int
- Username - varchar(25)
- Password - varchar(25)
- Firstname - varchar(50) etc...
- 用户 ID - int
- 用户名 - varchar(25)
- 密码 - varchar(25)
- 名字 - varchar(50) 等...
Roles table
角色表
- RoleId - int
- Role Description - varchar(25)
- 角色 ID - int
- 角色描述 - varchar(25)
User_Roles table
User_Roles 表
- UserId - int (with a foregin key to the Users table)
- RoleId int (foreign key to the Roles table)
- UserId - int(带有用户表的外键)
- RoleId int(角色表的外键)
回答by Nix
Pros and Cons vary based on the size and complexity of your system.
利弊因系统的大小和复杂性而异。
I would break it up into User, Role, UserResources
我会将其分解为 User、Role、UserResources
User(would define basic information)
User Roles
- FK->RoleType
Role_Type(user, admin, customer, possibly permissions or you could break this out further).
UserResources(media)
- FK->User
用户(将定义基本信息)
用户角色
- FK->角色类型
Role_Type(用户、管理员、客户,可能是权限,或者您可以进一步细分)。
用户资源(媒体)
- FK->用户