Common MySQL fields and their appropriate data types

前端 未结 5 2071
庸人自扰
庸人自扰 2020-12-04 04:45

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 k

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 05:32

    Since you're going to be dealing with data of a variable length (names, email addresses), then you'd be wanting to use VARCHAR. The amount of space taken up by a VARCHAR field is [field length] + 1 bytes, up to max length 255, so I wouldn't worry too much about trying to find a perfect size. Take a look at what you'd imagine might be the longest length might be, then double it and set that as your VARCHAR limit. That said...:

    I generally set email fields to be VARCHAR(100) - i haven't come up with a problem from that yet. Names I set to VARCHAR(50).

    As the others have said, phone numbers and zip/postal codes are not actually numeric values, they're strings containing the digits 0-9 (and sometimes more!), and therefore you should treat them as a string. VARCHAR(20) should be well sufficient.

    Note that if you were to store phone numbers as integers, many systems will assume that a number starting with 0 is an octal (base 8) number! Therefore, the perfectly valid phone number "0731602412" would get put into your database as the decimal number "124192010"!!

提交回复
热议问题