所有数据库表都应该有主键吗? [英] Should all database tables have a primary key?
问题描述
是否是给每个数据库表一个主键的好习惯?在我看来,如果主键不是明确需要,那么它只是在我的数据库中更多的杂乱。
Is it good practice to give every database table a primary key? It seems to me that if the primary key is not explicitly needed, then it would just be extra clutter in my database.
推荐答案
在OLTP数据库您几乎总是(在我的情况下总是)有某种主键。有时候,Guid,有时是自动编号/身份字段,有时由应用程序或客户设置。有时甚至是多个字段的组合。这是因为你通常希望唯一标识,从表中的任何给定行。
When you probably WOULD:
In an OLTP database you'd almost always (in my case always) have a primary key of some sort. Sometimes Guid, sometimes autonumber/identity fields, sometimes set by the application or the customer. Sometimes even a combination of more than one field. This is because you'll typically want to uniquely identify any given row from the table.
此外,主键是由查询优化应该提高性能使用限制对于查找并加入
Also, a primary key is a constraint used by the query optimiser that should improve performance for lookups and joins.
唯一的一次,你就不必主键位于报告表中,可能位于非规范数据仓库中。
The only time you wouldn't have a primary key is in a "reporting" table, probably in a denormalised data warehouse.
这篇关于所有数据库表都应该有主键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!