MySQL char vs. int

后端 未结 4 1394
情话喂你
情话喂你 2021-01-22 09:55

I\'m currently having a discussion in my class about the datatypes char and int in MySQL. We have a phone number of 8 individual numbers. However, we c

相关标签:
4条回答
  • 2021-01-22 10:30

    I started using VARCHAR for storing phone numbers because it gives you more range in formats of different countries. Storage is cheap.

    0 讨论(0)
  • 2021-01-22 10:44

    Don't know about the US exactly, but in Europe the national access code is a leading 0 , and international access code is a leading 00 or +. So that's a con to using INT, as the leading 0's would be lost. Further more you also have phone numbers that contain names and even though these names can be converted to numbers, it would probably be nice to keep them as a name. That's a second con to using INT. Last con is you can also have extension numbers, etc. All goes into favor of VARCHAR.

    0 讨论(0)
  • 2021-01-22 10:45

    If you have a fixed size then you can use char(8) -> 8B , mysql works much faster with fixed size fields, but if you chose int -> 4B you will save space and also if you will have a index on that field it will work faster then the char.

    You can do a simple benchmark to test the speed of writes and reads using char(8) and int

    There is no point in using variable length type like varchar or varbinary if you have a fixed size because performance will decrease

    0 讨论(0)
  • 2021-01-22 10:57

    Depends on how you want to represent the phone number, do you need area codes, country codes and stuff like that, and do you want to save it as a single column or do you want to split it up?

    Personally, I would choose to represent area codes, country codes, and the phone number as 3 columns with the datatype int, as this would make it easier to find all phone numbers in one area, and so on. But if it's only purpose is to be a like a string value, the char would be sufficient, however i would consider using the varchar instead, if you have phone numbers for several countries.`

    0 讨论(0)
提交回复
热议问题