数据库设计:多种用户类型的不同字段

发布于 2024-10-04 04:21:17 字数 679 浏览 1 评论 0原文

我正在使用 MySQL 5.0.77 构建一个应用程序,其中包含

a) 不同的用户类型(例如木匠、机械师、...、管道工)
b)每种用户类型的不同输入字段,例如用户选择木匠,并显示与该职业相关的字段,其中每个职业的字段不同

我的想法是:

表:用户
用户 ID
user_name

表:木工
用户 ID
木制品_评级
metalwork_ rating

表:管道
用户 ID
中央供暖等级
Bathroom_ rating

等等...

但这看起来不太好,因为我最终可能会在具有不同字段的多个表中存在大量表和用户。

我非常喜欢元标签表的想法(就像我们在 WordPress 中看到的那样),以便存储每个用户字段条目,例如

Table: user_info
用户 ID
领域
value

所以我们会有例如

1 |木制品评级 |中级
1 |金属制品评级 |高级
2 |木制品评级 |高级

我的问题是,如何构建一个数据库,该数据库具有多个用户的多个字段,而每个用户仅填写一类可用字段?

谢谢

I am building an application using MySQL 5.0.77 that contains

a) different user types (e.g. carpenter, mechanic, ..., plumber)
b) different input fields for each user type, e.g. user selects carpenter and is presented with fields pertaining to that profession, where the fields for each profession are different

My thinking is along the lines of:

Table: users
user_id
user_name

Table: carpentry
user_id
woodwork_rating
metalwork_rating

Table: plumbing
user_id
central_heating_rating
bathroom_rating

And so on...

This does not seem very good though since I could potentially end up with lots of tables and users existing in multiple tables with different fields.

I quite like the idea of a metatags table (like we see in Wordpress) so that each users field entry is stored, e.g.

Table: user_info
user_id
field
value

So we would have for example

1 | woodwork_rating | intermediate
1 | metalwork_rating | advanced
2 | woodowork_rating | advanced

My question is, how would you structure a database that has multiple fields for multiple users for which each user only fills in one category of the available fields?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗琴音 2024-10-11 04:21:17

表Users:

 UserID: Autoinc PRIMARY KEY
 (More user data columns here)
 UserType: CHAR(5)

表UserTypes

 UserType: CHAR(5) PRIMARY KEY
 Description: VARCHAR(50)

表UserRatingList

 UserRatingCode: CHAR(5) PRIMARY KEY
 UserType: CHAR(5) REFERENCES UserTypes
 Description: VARCHAR(50)

表UserRatings

 UserID: INTEGER PRIMARY KEY / REFERENCES Users
 UserRatingCode: CHAR(5) PRIMARY KEY / REFERENCES UserRatingList
 Rating: INTEGER (or whatever you prefer)

表UserRatingList 建立了可应用于每种用户类型的评级模式。 UserRatings 包含实际评级。我使用 CHAR(5) 提供可读代码,而无需加入说明字段,但如果需要,您可以将它们更改为 INTEGER。

这种结构还可以修改为允许每个用户拥有多种类型;只需创建一个包含 UserID 和 UserType 的附加 UserTypeLinks 表即可。

Table Users:

 UserID: Autoinc PRIMARY KEY
 (More user data columns here)
 UserType: CHAR(5)

Table UserTypes

 UserType: CHAR(5) PRIMARY KEY
 Description: VARCHAR(50)

Table UserRatingList

 UserRatingCode: CHAR(5) PRIMARY KEY
 UserType: CHAR(5) REFERENCES UserTypes
 Description: VARCHAR(50)

Table UserRatings

 UserID: INTEGER PRIMARY KEY / REFERENCES Users
 UserRatingCode: CHAR(5) PRIMARY KEY / REFERENCES UserRatingList
 Rating: INTEGER (or whatever you prefer)

The table UserRatingList establishes the pattern of ratings that can be applied to each user type. UserRatings contains the actual ratings. I use CHAR(5) to provider readable codes without having to join in the Description fields, but you can change them to INTEGER if you want.

This structure can also be adapted to allow each user to have multiple types; simply create an addition UserTypeLinks table with UserID and UserType.

城歌 2024-10-11 04:21:17

我想依赖“迈克”的最后回答。
我正面临这个问题。我正在创建一个具有以下类型的网络,

Enterprise {name, mail, adress, etc ...}
Employee {name, mail, branch, jobdescription, etc ...}
Individual {name, mail, surname, age, town, etc...}

怎么样:

表用户

userID (autoincr)
userType
mailadress
password
...    

表用户类型

typeID
typeName
typeDescr

表企业

entID (autoincr)
userID
field 1
field 2
...

表员工

empID (autoincr)
userID
...

表个人

indID (autoincr)
userID
...

这对您有意义吗?

i would like to rely on 'mike' s last answer.
i am facing this exact problem. i am creating a network with the following types

Enterprise {name, mail, adress, etc ...}
Employee {name, mail, branch, jobdescription, etc ...}
Individual {name, mail, surname, age, town, etc...}

how about:

Table Users

userID (autoincr)
userType
mailadress
password
...    

Table Usertypes

typeID
typeName
typeDescr

Table Enterprises

entID (autoincr)
userID
field 1
field 2
...

Table Employee

empID (autoincr)
userID
...

Table Individuals

indID (autoincr)
userID
...

does this make sense to you?

沫离伤花 2024-10-11 04:21:17

所有字段都将是具有相同数据类型的“评级”吗?如果是这样,我喜欢 Larry Lustig 的解决方案。

或者是否可能存在具有不同数据类型的不相关字段?日期、字符串、小数、整数?如果是这样,第一个解决方案是使用与用户连接的 1 对 1 相关表。只要不需要在运行时动态添加新字段即可。

Are all the fields going to be "ratings" with the same datatype? If so I like Larry Lustig's solution.

Or could there be unrelated fields with different data types? dates, strings, decimals, integers? If so, the first solution of having 1 to 1 related tables that join with Users is OK. As long as you don't need to dynamically add new fields at run time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文