选择主键

发布于 2024-12-22 13:27:50 字数 682 浏览 2 评论 0原文

我正在做一个项目,我必须制作一个几乎像维基百科一样的网站,但是在制作数据库时我偶然发现了一个问题。

我似乎无法弄清楚我的主键应该是什么。我尽力在下面说明我的表结构,以便您可以看到问题。

name            | type        | content   |
--------------------------------------
John Doe        | overview    | some text |
----------------------------------------
John Doe        | background  | some text |
----------------------------------------
John Doe        | height      | some text |
----------------------------------------
Fred Flintstone | overview    | some text |
---------------------------------------
Fred Flintstone | background  | some text |

我认为仅仅创建一个 id 列并自动递增它是没有意义的,因为每条记录单独都是无用的。

你怎么认为?是否需要主键?如果是的话应该是什么?

I'm doing a project where i have to make a site almost like wikipedia, but while making the database i've stumbled across a problem.

I can't seem to figure out what my primary key should be. I tried my best to illustrate my table structure below, so you can see the problem.

name            | type        | content   |
--------------------------------------
John Doe        | overview    | some text |
----------------------------------------
John Doe        | background  | some text |
----------------------------------------
John Doe        | height      | some text |
----------------------------------------
Fred Flintstone | overview    | some text |
---------------------------------------
Fred Flintstone | background  | some text |

I dont think it would make sense to just make an id column, and auto increment it, as each record is useless alone.

What do you think? is a primary key needed? If so what should it be?

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

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

发布评论

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

评论(3

心碎无痕… 2024-12-29 13:27:50

我当然会选择自动增量主ID。这就是它的用途,它使维护和更新表变得更容易,并且也更容易链接到关系设置中的其他表。

查找记录时还有性能问题。

总结:

  • 维护表之间的关系
  • 查找记录时的

  • 性能
  • 更容易在更改内容时编写更新和删除查询

请参阅:http://code.openark.org/blog/mysql/reasons-to-use-auto_increment-columns-on-innodb

I would certainly go for an Auto Increment primary ID. That's what its for, it makes it easier to maintain and update the table, and also a lot easier to link to other tables in a relational setup.

There is also the matter of performance when looking up records.

To sum up:

  • Maintaining relations between tables
  • Performance when looking up records.
  • Easier to write update and delete queries when changing the content

See: http://code.openark.org/blog/mysql/reasons-to-use-auto_increment-columns-on-innodb

情感失落者 2024-12-29 13:27:50

我建议您创建一个 id 列作为主键,并再创建一个表来映射概述、背景、高度等记录,

id overview  background height 
1      1           3         5

尽管这种类型的模式尚未完全规范化。否则,您可以为概述、背景和高度制作不同的表格,然后为它们创建一个像上面这样的关系表。

I would suggest you create an id column as primary key and create one more table that will map the records like overview, background, height

id overview  background height 
1      1           3         5

though this type of schema is not fully normalize. other wise you can make different tables for overview, background and height and then create a relation table like above for them.

倾听心声的旋律 2024-12-29 13:27:50

在进行更多工作之前,请先访问 mediawiki.org

在示例数据中,{name, type} 唯一标识每一行。但你的样本数据不具有代表性。名称在现实世界中并不唯一,因此您可能需要其他方法来识别人员。仅仅添加一个自动递增的 ID 号作为主键是行不通的,因为它允许这样的废话。

id  | name            | type        | content   |
--------------------------------------
1   | John Doe        | overview    | some text |
2   | John Doe        | overview    | some text |
3   | John Doe        | overview    | some text |
4   | John Doe        | overview    | some text |

有多少个不同的人? (有 3 个。 ID 号为 1 和 3 的 John Doe 是同一个人。)对 {name, type} 的 UNIQUE 约束也无济于事,因为这将只允许一个名为“John Doe”的人出现在数据库。

那么您还知道什么可以用来识别人员呢?电子邮件? (不要在这里寻找灵丹妙药。没有灵丹妙药。甚至电子邮件地址都可以共享。您只是在寻找比“名称”更好的东西。)

Before you do any more work, take a look at mediawiki.org.

In your sample data, {name, type} uniquely identifies each row. But your sample data isn't representative. Names aren't unique in the real world, so you probably need some other way to identify people. Just adding an autoincrementing ID number as the primary key won't work, because it allows nonsense like this.

id  | name            | type        | content   |
--------------------------------------
1   | John Doe        | overview    | some text |
2   | John Doe        | overview    | some text |
3   | John Doe        | overview    | some text |
4   | John Doe        | overview    | some text |

How many distinct people are there? (There are 3. The John Does with id numbers 1 and 3 are the same person.) A UNIQUE constraint on {name, type} won't help, either, because that would allow only one person named "John Doe" in the database.

So what else do you know that you can use to identify people? Email address? (Don't look for a magic bullet here. There isn't one. Even email addresses can be shared. You're just looking for something better than "name".)

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