需要一些有关 MySQL 模式的输入
我需要一些关于如何构建我的数据库的输入。
假设我想将成员数据存储在成员表中。有两种类型的成员,比方说个人和组织。个人和组织具有不同的信息集,因此我们必须将它们存储到两个不同的表中:假设个人和组织。
我将有以下表格:
成员:
id
member_type_id
status
etc..
member_types:
id
type (person|organization|or any other type if needed)
人员:
id
name
etc
组织:
id
name
etc
我的问题是:哪一个是以下最好的?
- 在人员和组织表中都有member_id
- 在成员表中有一个额外的列来存储person_id或organization_id
- 在成员表中有两个额外的列来存储person_id或organization_id(以适用者为准)
还有一个要求是可能有另一个表引用这两个人和组织表。假设我们有一个名为“访问者”的表,该表也可以是个人或组织。
感谢您的意见。
I need some inputs on how I should structure my db.
Let's say I want to store members data in members table. There are two types of members, let's say person and organization. Person and organization have different set of information so that we have to store them into two different tables: let's say persons and organizations.
I would have the following tables:
members:
id
member_type_id
status
etc..
member_types:
id
type (person|organization|or any other type if needed)
persons:
id
name
etc
organization:
id
name
etc
My question is: Which one is the best of the followings?
- Have member_id in both persons and organizations tables
- Have an extra column in members table to store either person_id or organization_id
- Have two extra columns in members table to store person_id or organization_id whichever is applicable
There is another requirement that there may be another table referring to both persons and organizations table. Let's say we have a table called visitors who can also be a person or an organization.
Thank you for your input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常最好画出你们的关系,这样更容易理解这两个实体之间的关系。这会让你很容易地说这是一对一、多对一、一对多或多对多等。
话虽如此,一旦你弄清楚了,设置就很简单了。
如果您使用 Java 或 .Net,则可以使用 Hibernate/NHibernate 通过指定关系自动构建表。
以下是我根据您概述的内容设计表格的方法:
(请记住,这是遵循 Java 命名模式)
要获取特定组织的所有成员的列表...
再次查看您的问题后,我认为您正在寻找不同的答案。
沃尔特
It is usually best to draw out your relationships so it is easier to understand how the 2 entities relate. This will let you easily say this is a one-to-one, many-to-one, one-to-many, or many-to-many, etc.
That being said once you figure that out, it is trivial to setup.
If you were using Java or .Net, you could use Hibernate/NHibernate to automatically build the tables for you by specifying the relationships.
Here is how I would design the tables from what you outline:
(Keep in mind, this is following the Java naming schema)
To get a list of all the members of a particular organization ...
After looking at your question again, I think you're looking for a different answer.
Walter
您还可以这样做:
会员
ID
姓名
状态
人员(扩展成员)
id(链接到会员 ID)
组织(扩展会员)
访客
ID
ID
日期
member_id(同样,这链接到成员表,您必须执行外部联接才能获取实际的成员数据,无论是个人还是组织)
这使用主键联接列并通过每个子类的单独表进行继承。在 Hibernate 中,查询会自动为您编写/处理,但编写起来并不困难。
让我知道这是否有帮助。
另外,您是否使用 PHP、Java 等,您能否提供更多关于您需要或正在使用它的背景信息?可能有更好的表格设计与此截然不同。
就像我说的,有一些工具可以为您自动执行这些操作,因此您不必担心低级细节。当然,在使用这些工具之前,你需要先了解其中的关系,否则,你的情况不会有任何改善。理解它们固然很好,但这些工具针对您需要的 90% 的内容进行了优化。
沃尔特
You can also do this:
Member
id
name
status
Person (extends Member)
id (links to member id)
Organization (extends Member)
id
Visitor
id
date
member_id (again, this links to the member table, you will have to do an outer join to get the actual member data, either person or organization)
This uses a primary key join column and inheritance through separate table per subclass. In Hibernate, the queries are automatically written / handled for you, but they aren't that difficult to write.
Let me know if that helps.
Also, are you using PHP, Java, etc, and can you provide more background information into what you need or are using this for? There might be a better table design that is very different from this.
Like I said, there are tools out there that automate this stuff for you so you're not worrying about low-level details. Of course, you need to understand the relationship even before using the tools, otherwise, you won't be any better off. It is good to understand them, but the tools are optimized for 90% of the stuff you will need.
Walter