Should numeric identifiers like serial numbers be stored as integers

best practicesdatabase-designdatatypes

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?

Best Answer

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.