Jump to content

Best data type for 1-digit numbers?


dannyb785

Recommended Posts

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? ;)

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.