我应该有一个专用的主键字段?

编程入门 行业动态 更新时间:2024-10-23 14:24:42
本文介绍了我应该有一个专用的主键字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在设计一个要由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.

更多推荐

我应该有一个专用的主键字段?

本文发布于:2023-11-16 06:23:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1601783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   有一个   主键

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!