使用电子邮件地址作为主键? [英] Use email address as primary key?
问题描述
与自动递增数字相比,电子邮件地址是否为主要候选字词?
Is email address a bad candidate for primary when compared to auto incrementing numbers?
我们的网络应用程序需要电子邮件地址在系统中是唯一的。所以,我想到使用电子邮件地址作为主键。但是我的同事建议,字符串比较将比整数比较慢。
Our web application needs the email address to be unique in the system. So, I thought of using email address as primary key. However my colleague suggests that string comparison will be slower than integer comparison.
是否有理由不使用电子邮件作为主键?
Is it a valid reason to not use email as primary key?
我们使用 PostgreSQL
。
推荐答案
字符串比较比int比较慢。但是,如果您只是使用电子邮件地址从数据库中检索用户,则无关紧要。
String comparison is slower than int comparison. However, this does not matter if you simply retrieve a user from the database using the e-mail address. It does matter if you have complex queries with multiple joins.
如果您在多个表中存储有关用户的信息,则用户表的外键将是电子邮件地址。这意味着您多次存储电子邮件地址。
If you store information about users in multiple tables, the foreign keys to the users table will be the e-mail address. That means that you store the e-mail address multiple times.
这篇关于使用电子邮件地址作为主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!