Jump to content

Question on numeric data types in Mysql


ktsirig

Recommended Posts

Hello all!

I have a field in my Mysql db which can store various numeric types, like 123, -45.74423, 23e-56 etc, and for those numbers, I can't know if they will be float, integers, positive or negative or how many decimal digits they may have.

My question is, should I use something like VARCHAR (100) as data type for this column?

If, in a query, I want to select all numbers that are >50 for example, will the comparison work or there will be a problem because I would have stored the numbers as VARCHAR?

 

Please advice!

Link to comment
Share on other sites

You shouldn't use a float unless you are storing numbers like 1.22223039540329 - you get the idea.

 

Varchar would work, but the index would be much longer. To give you an idea - each character in varchar needs 8 bytes. The largest data number in mysql is 8 bytes long (bigINT) goes past 1 billion numbers. Use the numbers.

 

More importantly, a numeric column forces the data to be a number. - keeps data cleaner.

 

Consider calling your numeric column types "unsigned" - this gives you twice as much numbers as signed. Unsigned means only positive numbers. So instead of going -128 to 128 UNSIGNED tinyint would go 0 - 256.

Link to comment
Share on other sites

You shouldn't use a float unless you are storing numbers like 1.22223039540329 - you get the idea.

 

Varchar would work, but the index would be much longer.

 

WTH are you talking about?

 

Run this query

SELECT 10 < 2

 

and this

SELECT "10" < "2"

 

The second one is how VARCHAR will work

Link to comment
Share on other sites

Hi, thanx to you all for your answers!

But my question remains: will these data types you suggested be able to store numbers like -1235456.754 or 645E-45 or 23.6 without any problem? As I said before, I CAN'T know beforehand what kind of number will the user enter, how many integer digits will it have, how many decimal places or if it will be negative or positive... And I MUST store the number exactly as it will be provided.

Link to comment
Share on other sites

You shouldn't use a float unless you are storing numbers like 1.22223039540329 - you get the idea.

 

Varchar would work, but the index would be much longer.

 

WTH are you talking about?

 

Run this query

SELECT 10 < 2

 

and this

SELECT "10" < "2"

 

The second one is how VARCHAR will work

 

WTH are you talking about? Varchar index is much larger than a numeric column data index. Of course the way the query is setup it will work both ways, but it is inappropriate database design to store numbers as varchar when only numbers would go into that column.

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.