I would like to save postcodes in my database. There are postcodes that begin with a 0, and not only in Germany. Unfortunately, the integer format would not work because the 0 would be omitted. So 01234
would then become 1234
. Is varChar(5) the only possibility?
The German ZIP Code is always 5 digits.
CodePudding user response:
Well, alphanumeric (anything with CHAR in the data type name...). Same as for phone numbers or other data that does primarily contain digits, but not only.
CodePudding user response:
Varchar(5) certainly isn't the only possibility. You could save them as integers, then pad them with leading zeroes in whatever application is using the database.
But IMO, saving them as strings (whether varchar or char) is the best option. Even though they're comprised of digits, they're not really numbers (e.g. it doesn't make sense to add them together, and leading zeroes are important). Saving them as strings would also give you flexibility if you do eventually need to use postcodes with letters in them.
CodePudding user response:
If they're always going to be 5 characters, then use a datatype that's exactly 5 characters, i.e. char(5)
. You could even add a column constraint to ensure that anything inserted into the table is exactly 5 characters long and every char is a digit.