常见的 MySQL 字段及其相应的数据类型
我正在建立一个非常小的 MySQL 数据库,用于存储名字、姓氏、电子邮件和电话号码,并且正在努力为每个字段找到完美"的数据类型.我知道没有完美的答案,但是对于诸如此类的常用领域,必须有某种通用约定.例如,我确定一个未格式化的美国电话号码太大而不能存储为 unsigned int,它必须至少是一个 bigint.
I am setting up a very small MySQL database that stores, first name, last name, email and phone number and am struggling to find the 'perfect' datatype for each field. I know there is no such thing as a perfect answer, but there must be some sort of common convention for commonly used fields such as these. For instance, I have determined that an unformatted US phone number is too big to be stored as an unsigned int, it must be at least a bigint.
因为我相信其他人可能会觉得这很有用,所以我不想将我的问题仅限于我上面提到的领域.
Because I am sure other people would probably find this useful, I dont want to restrict my question to just the fields I mentioned above.
哪些数据类型适合常见的数据库字段?电话号码、电子邮件和地址等字段?
What datatypes are appropriate for common database fields? Fields like phone number, email and address?
推荐答案
有人会发布比这更好的答案,但只是想说明我个人永远不会在任何类型的整数字段中存储电话号码,主要是因为:
Someone's going to post a much better answer than this, but just wanted to make the point that personally I would never store a phone number in any kind of integer field, mainly because:
- 你不需要用它做任何算术运算,并且
- 迟早有人会尝试(做类似的事情)在他们的区号周围加上括号.
但总的来说,我似乎几乎只使用:
In general though, I seem to almost exclusively use:
- INT(11) 用于 ID 或引用另一个 ID 的任何内容
- 时间戳的日期时间
- VARCHAR(255) 用于保证少于 255 个字符的任何内容(页面标题、名称等)
- 几乎所有其他内容的文本.
当然也有例外,但我发现这涵盖了大多数可能性.
Of course there are exceptions, but I find that covers most eventualities.
相关文章