gpt4 book ai didi

mysql - 既然可以用用户名搜索,为什么还要在 MySQL 中使用 ID?

转载 作者:行者123 更新时间:2023-12-02 01:01:34 26 4
gpt4 key购买 nike

在许多关于 MySQL 的教程中,他们经常使用用户创建帐户时自动生成的 ID。稍后,该 ID 用于搜索该配置文件或更新该配置文件。

问题:MySQL可以用用户名搜索,为什么还要用ID?

我也可以使用用户名在 MySQL 表中进行搜索,那么使用 ID 的优缺点是什么?

更新:

非常感谢您的 react !假设用户想要登录网站。他将提供用户名和密码。但是对于我的代码,我首先必须进行查询才能知道 ID,因为用户不知道 ID。这是正确的还是有其他方法可以做到这一点?

如果我将用户的 ID 存储在 cookie 中,当用户登录时,我首先查看 ID 是否与用户名匹配。然后检查密码是否正确。然后我可以使用该 ID 进行查询。这是个好主意吗?当然,我将对所有这些使用准备好的语句。

最佳答案

请引用this post .

1 - It's faster. A JOIN on an integer is much quicker than a JOIN on a string field or combination of fields. It's more efficient to compare integers than strings.

2 - It's simpler. It's much easier to map relations based on a single numeric field than on a combination of other fields of varying data types.

3 - It's data-independent. If you match on the ID you don't need to worry about the relation changing. If you match on a name, what do you do if their name changes (i.e. marriage)? If you match on an address, what if someone moves?

4 - It's more efficient If you cluster on an (auto incrementing) int field, you reduce fragmentation and reduce overall size of the data set. This also simplifies indexes needed to cover your relations.

关于mysql - 既然可以用用户名搜索,为什么还要在 MySQL 中使用 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28021857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com