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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.