News for hackers. Stuff that matters.
Re:Do you make these 5 database design mistakes? by ams6110 on Monday January 02, 2012 @02:52PM (#3417064)
I think more important than obsession over column sizes (in most databases, varchar columns only use the space necessary, not the maximum, more or less) is using correct datatypes. E.g. using varchar for a column that you know will only have numbers. Or using int for timestamps (e.g. using the unix epoch offset) and then having to do a lot of converting. Or sticking serialized objects from your application domain into blob columns.

Most databases have a lot of built in safety and functionality if you are using the correct datatypes, but that is something you can't benefit from if you are treating everything like a blob or a string.

Re:Do you make these 5 database design mistakes? by jbigelow76 on Monday January 02, 2012 @03:44PM (#3417248)
I've got to disagree about not using varchar for a column you you know will only contain numbers. I'm assuming you mean something like the numbers of a phone number (dashes and parens have been scrubbed out). Even if a field will only contain numbers I won't use a numeric based field type unless there is the possibility that I would be using the value in some form of mathematical function. I might add and subtract from an account balance but I would never add or subtract from a Social Security Number or zip code.

(I don't keep to this rule for surrogate keys which will be int/bigint auto incrementing, even though I won't be doing any math on the values themselves)

Re:Do you make these 5 database design mistakes? by onemoreact on Monday January 02, 2012 @03:55PM (#3417304)
There is value in what your saying.

But, if you store a SSN as say 123-45-6789 your using at least 11 bytes to store a 4 byte number. Use it as an index and you waste that space again, store it in a cache and you have less space for useful information, compare it with another SSN and it's slower etc. Now individually it's probably irrelevant but start doing this with several fields on a large application and you can noticeably slow things down even if the extra disk space seems cheap.

PS: 95% of the time it's probably the safest option, but it's still something you should consider based on how that data fit's in with the rest of your application.

Re:Do you make these 5 database design mistakes? by bprieto on Monday January 02, 2012 @04:36PM (#3417483)
Number types are meant to store numbers, not text. Even if you know the text will be just a subset of the alphabet (just 0-9) it is not a number.

Just a couple of problems: some day in the future the field could need another character beyond 0-9. In Spain National Identity Numbers were once just numbers, but they changed and added a letter for validation. If you had stored this as a number you would need to change data types, and suffer a massive application update.

Another problem: if you order a number field you get something like 1, 5, 100, 2000. But if you order a text field you would get 1, 100, 2000, 5. It's not nice to get the "number" order when you are expecting a "text" order.

Re:Do you make these 5 database design mistakes? by kls on Monday January 02, 2012 @04:06PM (#3417347)
Some numeric data types will trim off leading 0's so if you have a number like 00543 when stored in the DB it will be retrieved as 543. Things like social security numbers and phones numbers are unique identifiers that just so happen to be numeric they should be treated as such with regards to data considerations.
Re:Do you make these 5 database design mistakes? by Tsagadai on Monday January 02, 2012 @07:43PM (#3418096)
Phone numbers are not unique identifiers. In several regions around the world, phone numbers are reused. Please keep this in mind because it is very short sighted to say you will never expand globally.
Re:Do you make these 5 database design mistakes? by ListMistress on Monday January 02, 2012 @09:12PM (#3418325)
I agree, neither phone numbers nor social insurance numbers are unique.

But the issue is with storing them in numeric datatypes. They aren't guaranteed to be unique forever (and indeed, are alphanumeric in many jurisdictions) and the leading zero problem is a killer when you go to reconstitute them, both for performance and for logic reasons. Same goes for US ZipCodes.

This is why I have to have a very strong technical reason to make a non-math column numeric, especially with externally set data (like SSNs, SINs, Account codes, etc.) The people who set them could just start adding letters or symbols...and this isn't rare.

Re:Do you make these 5 database design mistakes? by coolgeek on Monday January 02, 2012 @09:49PM (#3418397)
Indeed, Medicare numbers are usually the SSN with a letter appended
Re:Do you make these 5 database design mistakes? by kls on Tuesday January 03, 2012 @12:32AM (#3418733)
Sure perhaps I should not have used the term "unique" but they are identifiers, they identify how to contact a certain individual based on a numeric sequence. Same with SS numbers they identify a living person. I would never use either as a unique key in my database but I am not a natural key person for the most part. I tend to like a uniquely generated key to serve as a surrogate for my unique primary identifier.
Re:Do you make these 5 database design mistakes? by jbigelow76 on Monday January 02, 2012 @08:34PM (#3418230)
Being cognizant of storage and indexing implications is important, but I would still probably go the char/varchar route; a distinction should be made between numeric data and text data that happens to be comprised only of numeric values. In addition whether talking about SSNs, product numbers, whatever, if you try and use a numeric field you run the risk of coming across a value that exceeds the upper bound of numeric type.
Re:Do you make these 5 database design mistakes? by BrentOzar on Tuesday January 03, 2012 @08:54AM (#3419608)
>> But, if you store a SSN as say 123-45-6789 your using at least 11 bytes to store a 4 byte number.

Plus it gets worse because people inevitably end up writing queries that parse the number with dashes, brackets, or other special delimiters. Those queries are CPU-intensive to do on the fly, and with databases like Oracle, DB2, and SQL Server charging by the CPU core, it becomes an expensive proposition as well.

Re:Do you make these 5 database design mistakes? by glhaynes on Monday January 02, 2012 @04:14PM (#3417382)
Rather than the safest option "95% of the time", I'd say doing so is probably best "essentially all of the time except for a few unique circumstances, all of which you'll know when they happen."
Re:Do you make these 5 database design mistakes? by noblethrasher on Monday January 02, 2012 @03:42PM (#3417239)
"using varchar for a column that you know will only have numbers"

I've been bitten too many times by the recommendation of using numeric types for [0-9]+ data. Unless it's fundamentally going to involve arithmetic I say leave it as some kind of string type.

Re:Do you make these 5 database design mistakes? by InclinedPlane on Monday January 02, 2012 @03:34PM (#3417210)
A rule of thumb: if you ever end up with a situation where you want to display data that is essentially just a table and you can't drive that via a fairly simple sql query then that probably means you need to revisit your design.