我正在设计一个数据库表,问自己这个问题:名字字段应该多长?

谁有最常见的字段(如名字、姓氏和电子邮件地址)的合理长度的列表?


当前回答

W3C推荐:

If designing a form or database that will accept names from people with a variety of backgrounds, you should ask yourself whether you really need to have separate fields for given name and family name. … Bear in mind that names in some cultures can be quite a lot longer than your own. … Avoid limiting the field size for names in your database. In particular, do not assume that a four-character Japanese name in UTF-8 will fit in four bytes – you are likely to actually need 12. https://www.w3.org/International/questions/qa-personal-names

对于数据库字段,VARCHAR(255)是一个安全的默认选择,除非您确实有充分的理由使用其他选项。对于典型的web应用程序,性能不是问题。不要过早地优化。

其他回答

我想说的是,宁可走偏高的那条路。因为您可能会使用varchar,所以您允许的任何额外空间实际上都不会占用任何额外空间,除非有人需要它。我会说,对于名字(名或名),至少要有50个字符,对于电子邮件地址,至少要有128个字符。有一些很长的电子邮件地址。

我喜欢做的另一件事是访问Lipsum.com,让它生成一些文本。这样,您就可以很好地了解100个字节是什么样子。

W3C推荐:

If designing a form or database that will accept names from people with a variety of backgrounds, you should ask yourself whether you really need to have separate fields for given name and family name. … Bear in mind that names in some cultures can be quite a lot longer than your own. … Avoid limiting the field size for names in your database. In particular, do not assume that a four-character Japanese name in UTF-8 will fit in four bytes – you are likely to actually need 12. https://www.w3.org/International/questions/qa-personal-names

对于数据库字段,VARCHAR(255)是一个安全的默认选择,除非您确实有充分的理由使用其他选项。对于典型的web应用程序,性能不是问题。不要过早地优化。

如果你需要考虑本土化,并且这在你的环境中是可行的,我建议:

为名称的每个组件定义数据类型——注意:某些区域性有两个以上的名称!然后为全名设置一个类型,

然后本地化就变得简单了(就名称而言)。

同样适用于地址,顺便说一句-不同的格式!

我几乎总是使用2的幂,除非有很好的理由不这样做,比如在面向客户的界面上,其他一些数字对客户有特殊的意义。

如果你坚持使用2的幂,那么你就可以在一个有限的常见大小范围内,这本身就是一件好事,它可以让你更容易猜测你可能遇到的未知物体的大小。我看到很多人都在这么做,而且这种做法也很有美感。当我看到这些时,我通常会有一种良好的感觉,这意味着设计师像工程师或数学家一样思考。不过如果只用质数,我可能会担心。:)

我通常会选择:

名字:30个字符 姓氏:30个字符 电子邮件:50个字符 地址:200个字符

如果我担心名称字段太长,我有时也会将名称字段设置为50,因为现在存储空间很少成为问题。