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

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

问题描述

我正在设计一个供 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天全站免登陆