我应该有一个专用的主键字段? [英] Should I have a dedicated primary key field?

查看:97
本文介绍了我应该有一个专用的主键字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个要由Web应用程序使用的小型SQL数据库。

I'm designing a small SQL database to be used by a web application.

我们假设一个特定的表有一个Name字段,不会有两行允许有相同的价值。但是,用户可以随时更改名称字段。

Let's say a particular table has a Name field for which no two rows will be allowed to have the same value. However, users will be able to change the Name field at any time.

此表中的主键将用作其他表中的外键。因此,如果将Name字段用作主键,则需要将任何更改传播到其他表。另一方面,唯一性要求将被自动处理。

The primary key from this table will be used as a foreign key in other tables. So if the Name field was used as the primary key, any changes would need to be propagated to those other tables. On the other hand, the uniqueness requirement would be handled automatically.

我的本​​能是添加一个整数字段作为主键,可以自动填充数据库。有这个领域有什么意义,还是浪费时间?

My instinct would be to add an integer field to act as the primary key, which could be automatically populated by the database. Is there any point in having this field or would it be a waste of time?

推荐答案

我会使用一个生成的PK,只是因为你提到的原因。此外,索引和整型比较比通过字符串比较更快。您可以在名称字段上放置一个唯一索引,而不必将其设为主键。

I would use a generated PK myself, just for the reasons you mentioned. Also, indexing and comparing by integer is faster than comparing by strings. You can put a unique index on the name field too without making it a primary key.

这篇关于我应该有一个专用的主键字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆