0

Many of our databases store numeric tracking codes (like serial numbers) as integers. There is little chance of these codes every getting alphabetic characters, but it still seems like an incorrect data type since you would never be performing mathematical operations on them.

What is appropriate data type for these types of identifiers?

3

One of the biggest advantages to using an INT over say a CHAR or VARCHAR(10) is size. The INT value will be 4 bytes and the CHAR/VARCHAR value will be 10 bytes. NCHAR/NVARCHAR would be even larger at 20 bytes. Because of this indexes on INT values are smaller and thus faster, sometimes considerably so, than a corrisponding CHAR/VARCHAR index. Also your row sizes are smaller requiring less reads per row. This is all aside from actual space storage of course. Thomas LaRock has written several blogs on "right sizing" your data types. Does this datatype make my column look fat and How to: Right sizing the datatypes currently in the buffer pool for example.

All of this of course assumes that you in fact will not be using characters in your tracking codes. If you are then of course you have no choice but to use CHAR\VARCHAR.

1

One of the problems with int is: they can't start with a "0". So whenever there is a risc of a starting 0 (postal zipcode, pin, ...), then don't use int.

  • I have to disagree with this being considered a risk. This leading zero problem can easily be solved with formatting. To use your own zip code example, I live in Puerto Rico and our zip codes are the perfect example because they start with "00". If you really wanted to store my zip code "00918" you could store it as a 918 int and present it to the end user padded with zeroes up to 5 chars. – AbeyMarquez Apr 10 '18 at 23:44
  • Every usage of the zip code (input, output, print on a label, print on a letter) would need to convert it to the right code, hopefully restoring the original value. I would see no good reason for this. We have codes like 06108 and 50188 in Germany, I would always suggest string for it. – flaschenpost Apr 15 '18 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.