dannyb785 Posted July 30, 2012 Share Posted July 30, 2012 I've thought it would be CHAR(1) but then I've also read that queries can search easier when the data type is an integer so maybe TINYINT? If the data will only ever be 0-9, what's the best data type to use? Quote Link to comment https://forums.phpfreaks.com/topic/266467-best-data-type-for-1-digit-numbers/ Share on other sites More sharing options...
Christian F. Posted July 30, 2012 Share Posted July 30, 2012 A number, that does not include decimal places, is called an "integer" or "whole number". A "char" (which is short for "character") is a single glyph from a defined set of characters (also known of "charsets", "character sets" or "code page"). This character can be any number of bytes and/or bits, depending upon what charset is used. So, if you have a number with a length of 1 digit, and no decimal places, then I think it's fairly obvious what data type should be used. No? Quote Link to comment https://forums.phpfreaks.com/topic/266467-best-data-type-for-1-digit-numbers/#findComment-1365508 Share on other sites More sharing options...
fenway Posted July 30, 2012 Share Posted July 30, 2012 The real question is what data you're trying to represent that you think will only ever be 1 digit. Quote Link to comment https://forums.phpfreaks.com/topic/266467-best-data-type-for-1-digit-numbers/#findComment-1365553 Share on other sites More sharing options...
dannyb785 Posted July 31, 2012 Author Share Posted July 31, 2012 I should rephrase my question. Which data type takes up less space? TINYINT or CHAR(1)? The real question is what data you're trying to represent that you think will only ever be 1 digit. Is that really the question? Not really, but I'll answer anyway. A user's level where 1=regular user, 2=admin, etc. I just want to take up as little space as possible in the database. Quote Link to comment https://forums.phpfreaks.com/topic/266467-best-data-type-for-1-digit-numbers/#findComment-1365597 Share on other sites More sharing options...
Christian F. Posted July 31, 2012 Share Posted July 31, 2012 If you've read the MySQL documentation, you'd know that both will take one byte. You can use a bitfield to use only 2 bits, but this is micro optimization and that is a Bad Thing So, use the correct data type, and don't worry too much about saving 6 bits for every user. I mean; Even if you have 1 million users, we're only talking about 732,4 KB of space saved. Quote Link to comment https://forums.phpfreaks.com/topic/266467-best-data-type-for-1-digit-numbers/#findComment-1365672 Share on other sites More sharing options...
dannyb785 Posted July 31, 2012 Author Share Posted July 31, 2012 If you've read the MySQL documentation, you'd know that both will take one byte. You can use a bitfield to use only 2 bits, but this is micro optimization and that is a Bad Thing So, use the correct data type, and don't worry too much about saving 6 bits for every user. I mean; Even if you have 1 million users, we're only talking about 732,4 KB of space saved. Thank you for your help. I knew they were both 1 byte, but I wasn't sure if integers were maybe faster or more efficient to work with than a CHAR or if it didn't really matter. Quote Link to comment https://forums.phpfreaks.com/topic/266467-best-data-type-for-1-digit-numbers/#findComment-1365776 Share on other sites More sharing options...
fenway Posted August 4, 2012 Share Posted August 4, 2012 In either case, you'd want ENUM. Quote Link to comment https://forums.phpfreaks.com/topic/266467-best-data-type-for-1-digit-numbers/#findComment-1366811 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.