MySQL数据库的基本结构

发布于 2024-10-18 09:03:50 字数 538 浏览 2 评论 0原文

我对 MySQL 相当陌生,我需要帮助解决一个相对基本的问题。

假设我有一个自动增量表,按行列出个人。我在列中包含了每个人的所有基本信息,例如姓名、年龄、种族等。但假设我也想包括人们的朋友名单。由于这些列表是动态的,并且据我所知,您不能在一个表中拥有两个自动增量变量,因此不可能在该特定表中包含好友列表,因为不存在子表或任何其他内容之类的东西。 MySQL 中的排序(再次据我所知)。如果您想要动态的朋友列表,您将必须创建一个专门用于此目的的新表。

我的这个想法正确吗?或者我错过了什么?

这是我目前的总体想法(我相当不喜欢):

table people_list {
  person_id (auto-increment)
  name
  age
  race
  ...
}

table friends_lists {
  friendship_id (auto-increment)
  person_id1
  person_id2
}

请注意,我只是为了演示而编造了 MySQL 本质上的语法。

还有更好的办法吗?

I'm fairly new to MySQL and I need help with a relatively basic question.

Say I have an auto-increment table that lists individual people by row. I include all of the basic information about each person such as name, age, race, etc in the columns. But say I want to include lists of the people's friends as well. Since these lists would be dynamic and to my knowledge you cannot have two auto-increment variables in a single table, it would not be possible to include the friends lists in that specific table as there are no such things as sub-tables or anything of the sort in MySQL (again to the best of my knowledge). If you wanted dynamic friends lists you would have to make a new table solely dedicated to that purpose.

Am I right in this thinking? Or am I missing something?

Here is my current general idea (which I rather dislike):

table people_list {
  person_id (auto-increment)
  name
  age
  race
  ...
}

table friends_lists {
  friendship_id (auto-increment)
  person_id1
  person_id2
}

Note that I just made up the syntax in essence of MySQL for demonstration.

Is there any better way?

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

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

发布评论

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

评论(2

喜爱纠缠 2024-10-25 09:03:50

您的方法是正确的...除了辅助表(您的场景中的 friends_lists )之外,没有其他方法可以做到这一点。这就是如何在两个表之间实现“多对多”关系。

在你的例子中,这两个表是相同的(people_list),但是,从概念上讲,它们可以被认为是“朋友”和“人”

但是,我可以给你一些关于这种方法的提示吗?

1 - 每个表在某种程度上都是一个“列表”。那么,为什么要加上后缀“_list”呢?不要,出于同样的原因,我们不使用复数表名(它的产品,而不是产品*s*。当然,哪里会有很多;)

2 - 而不是在朋友处使用自动增量 id ,将 person_id1 和 person_id2 都设为主键。你去掉了一个无用的列,这样你就可以强制每一对人 X - 朋友 Y 都是唯一的。

3 - 根据上下文给出 person_id1 和 2 个有意义的名称,例如“person_id”和“friend_id”

总结一下:

table person {
  person_id (auto-increment, primary key)
  name
  age
  race
  ...
}

table friend {
  person_id (foreing key from person, primary key)
  friend_id (foreing key from person, primary key)
}

Your approacch is correct... theres no other way to do this other than an auxiliary table (friends_lists in your scenario). Thats how one achieve a "many-to-many" relationship between two tables.

In your case, the two tables are the same (people_list), but, conceptually, they can be thought as "friends" and "people"

But, may i give you a few hints about this approach?

1 - Every table is, in a way, a "list". So, why the suffix "_list" ? Dont, for the same reason we dont use plural for table names (its product, not product*s*. Of course where will be many ;)

2 - Instead of using an auto-increment id at friend, turn both person_id1 and person_id2 into the primary key. You get rid of a useless column, AND this way you enforce that each pair Person X - Friend Y is unique.

3 - Give person_id1 and 2 meaningful names, based on context, like "person_id" and "friend_id"

To sum it up:

table person {
  person_id (auto-increment, primary key)
  name
  age
  race
  ...
}

table friend {
  person_id (foreing key from person, primary key)
  friend_id (foreing key from person, primary key)
}
终陌 2024-10-25 09:03:50

为什么不

table people_list {
  person_id (auto-increment)<
  name
  age
  race
  ...
}

table person_friend {
  person_id(of person)
  person_id(of friend)
}

看看这个 更好地理解一对多关系。

Why not

table people_list {
  person_id (auto-increment)<
  name
  age
  race
  ...
}

table person_friend {
  person_id(of person)
  person_id(of friend)
}

Take a look at this to understand better about one to many relationships.

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