I am setting up company pages. One of the fields is 'Revenue'. So this can vary from $0 to i guess hundreds of billions. So this field should have a bigint data type? Or is there a better way to store revenue in the database? Using MySQL.
Have a look at the DECIMAL data type.
I would use bigint and store your revenue figures in pennies rather than using decimals. Then you will avoid rounding issues.
Try Numeric or decimal. The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data.
check this out: MySQL data types
Related
I'm trying to store a currency value in MySQL (InnoDb) and I need to write queries to aggregate some records (sum) and I'm having problem with the precision of the the output!
I've set the field's type to double and my values are some what precise but the MySQL's operators are not as precise as I need. For what it is worth, PHP's default operators are not precise enough either but there's bc* functions in PHP which can do the trick.
I was wondering if there's any way to tune the precision of MySQL operators? Including aggregation functions?
For the record, storing to and retrieving from MySQL won't affect my values which means double is an ideal type for my fields.
Since money needs an exact representation don't use data types that are only approximate like double which is a floating-point. You can use a fixed-point numeric data type for that like
numeric(15,2)
15 is the precision (total length of value including decimal places)
2 is the number of digits after decimal point
See MySQL Numeric Types:
These types are used when it is important to preserve exact precision, for example with monetary data.
when i was a noob, I developed a website to store daily expenses, and the expenses was saved as varchar. now I want to just alter the structure of the database (with all the thousands data inside) to decimal (which i found out is the best way to store currency stuff). my question is, will all the expenses data be intact? or will it corrupt anything?
I'd suggest a layered approach:
Alter the database structure and add a decimal field.
Select the data from the database into the programming language of your choice.
Convert the data with the language, write a SQL-script to insert (or insert them directly).
Remove the VARCHAR field
Rename the DECIMAL field to the name the VARCHAR field had.
Depending on how many thousands do you have...? If you have constants decimals (always 2 decimals for example) then you can change the type to decimal(8,2) 6 integers and 2 decimals. If the data have less than 2 decimal 2.1 => 2.10 but in the other hand if you have more than 2 decimals then it will be 2.118 => 2.11 So be careful with this.
Make some CAST test, you may want to read this question: Problem convert column values from VARCHAR(n) to DECIMAL
I am importing a database, in the read me that came with the csv, it describes he necessary table column names as well as the datatypes that should be used for each.
For one of the columns, the datatype "number" is used? My database client manager (HeidiSql) doesn't show any such option. The closest I see is enum. Is that what I should use?
Also no length was given in the description. If I should use enum what should I enter as length?
I'd recommend integer if that number does not contain a decimal point, float or double if it does.
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
I think FLOAT or DECIMAL would be the best option because NUMBER datatype is not supported in MySQL.
Not recommending "integer" because you would get possible loss of precision of data
But in case if you are sure about not losing data, then go for bigint type in MySQL if the size of data is greater than 9 digits
NUMBER datatype could be used in ORACLE SQL
Here is an overview of all of the MySQL numeric data types. It depends what type of numeric data you have as to which one you should use, Integers for whole numbers etc.
Enum is not a numeric type this is an Enumeration.
There are several numeric types and also the enum type.
I think DECIMAL or FLOAT would be two options in MySQL.
A friend of mine said that they experienced oddities with using the decimal column to store money formats such as saving 1000.00 which would result in it being stored as 999.99.
However I just tested and 1000.00 is stored as 1000.0000 on decimal(19,4) / MySQL 5. Can anyone offer insight into why they may have experienced issues? Perhaps it was an old MySQL bug, improper calculation on the application side before saving it to the database?
This is for an ROI field and I'm storing money values which can be up in the thousands, FYI.
You should use the DECIMAL data type for exact numeric representations, and not DOUBLE. Quoting from the MySQL Documentation on Numeric Types:
MySQL supports all of the standard SQL numeric data types. These types include the exact numeric data types (INTEGER, SMALLINT, DECIMAL, and NUMERIC), as well as the approximate numeric data types (FLOAT, REAL, and DOUBLE PRECISION).
...
Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section B.5.5.8, Problems with Floating-Point Values.
On 1.14.2. DECIMAL Data Type Changes where changes to the decimal type are discussed it says this:
In older versions of MySQL, decimal values could have up to 254 digits. However, calculations were done using floating-point and thus were approximate, not exact.
This could be the source of the error your friend talked about. If the value of 1000.00 was calculated it would be prone to floating point errors.
I would choose decimal for storing monetary values as the calculations will be more accurate than using double.
I am wondering what data type I should use for storing 11.1234 in mySql?
I am not sure if I should use varchar or int. There is also some decimal data types.
FLOAT or DECIMAL is probably best.
From the MySQL manual:
The FLOAT and DOUBLE data types are
used to represent approximate numeric
data values [...] The DECIMAL and
NUMERIC data types are used to store
exact numeric data values. In MySQL,
NUMERIC is implemented as DECIMAL.
These types are used to store values
for which it is important to preserve
exact precision, for example with
monetary data.