如果我们有一个唯一的值,为什么要在表中使用ID列

时间:2022-09-11 18:43:44

i want to ask a small question here but i really don't know what is the answer of this question.

我想问一个小问题,但我真的不知道这个问题的答案是什么。

i have a accounts table which has

我有一张会计表。

Username | Password

用户名密码|

the username is a primary key so its unique

用户名是主键,所以它是唯一的

so is it necessary to put an ID column to the table ? if Yes, what is the benefit of that ?

那么是否有必要将ID列放到表中呢?如果是,有什么好处呢?

Thanks

谢谢

3 个解决方案

#1


2  

Search by a numeric key is slightly faster (varies from one DB to another). Also, if you have a lot of references to the user table, you save some database space by having the numeric ID as the foreign key, as opposed to a string name.

用数字键搜索会稍微快一点(不同的数据库有不同)。另外,如果您对用户表有很多引用,您可以通过将数字ID作为外键来保存一些数据库空间,而不是字符串名称。

#2


2  

It will make everything else easier, mainly foreign key relationships from other tables. And it allows you to change the username if you want - primary keys are not easy to change.

它将使其他一切变得更容易,主要是来自其他表的外键关系。它允许你改变用户名如果你想-主键不容易改变。

#3


0  

  • Faster in indexes
  • 更快的索引
  • Consumes less disk space (and is again faster) when used as a foreign key
  • 当用作外键时,消耗的磁盘空间更少(而且速度也更快)
  • And, as mentioned a number of times, you can change the username without modifying a host of other tables.
  • 而且,如前所述,您可以在不修改其他表的主机的情况下更改用户名。

#1


2  

Search by a numeric key is slightly faster (varies from one DB to another). Also, if you have a lot of references to the user table, you save some database space by having the numeric ID as the foreign key, as opposed to a string name.

用数字键搜索会稍微快一点(不同的数据库有不同)。另外,如果您对用户表有很多引用,您可以通过将数字ID作为外键来保存一些数据库空间,而不是字符串名称。

#2


2  

It will make everything else easier, mainly foreign key relationships from other tables. And it allows you to change the username if you want - primary keys are not easy to change.

它将使其他一切变得更容易,主要是来自其他表的外键关系。它允许你改变用户名如果你想-主键不容易改变。

#3


0  

  • Faster in indexes
  • 更快的索引
  • Consumes less disk space (and is again faster) when used as a foreign key
  • 当用作外键时,消耗的磁盘空间更少(而且速度也更快)
  • And, as mentioned a number of times, you can change the username without modifying a host of other tables.
  • 而且,如前所述,您可以在不修改其他表的主机的情况下更改用户名。