When defining datatypes in a database, I have always had a problem with choosing whether to use integers or strings to store certain 'numerical' data.
Say I am building Yet Another Address Book and there is a post code field. Provided that post codes are always a 4 digit number, which data type do I store it as? Integer or string? Technically it is an integer, but I'm not doing any sort of calculations on it, I'm just spitting it out into a table. Would your opinion change if I want to sort the table by post code?
Now, I'm not stupid. I do recognize a valid need for integers, such as page views and unique users or logged in users and guest users. But what about for storing how many files are in a torrent? Integer or string?
Integer is a numeric value, while String is a character value represented in quotes.
You are doing the right thing - identity field should be numeric and not string based, both for space saving and for performance reasons (matching keys on strings is slower than matching on integers).
Common Database Data Types Integer – is a whole number that can have a positive, negative or zero value. It cannot be a fraction nor can have decimal places. It is commonly used in programming especially for increasing values.
STRING is a database of known and predicted protein-protein interactions. The interactions include direct (physical) and indirect (functional) associations; they stem from computational prediction, from knowledge transfer between organisms, and from interactions aggregated from other (primary) databases.
In my country, post-codes are also always 4 digits. But the first digit can be zero.
If you store "0700" as an integer, you can get a lot of problems:
Technically, our post codes is actually strings, even if it is always 4 digits.
You can store them as integers, to save space. But remember this is a simple DB-trick, and be careful about leading zeroes.
But what about for storing how many files are in a torrent? Integer or string?
That's clearly an integer.
I always use the following rule:
If you plan on performing mathematical calculations on it (adding/subtracting/etc) make it an integer or other numerical data type.
If you do not plan on performing any types of mathematical calculations on the field, store it as a string.
In the instance of Zip codes, you should never have a time where you need to add to a zip code, or subtract, or multiply two zip codes together. Mathematical functions generally are not used on ZIP codes because they are used as identifiers and not quantities. Therefore you should store your zip code as a string datatype
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With