I am very new to php and mysql so sorry if this is a silly question,
I am importing some measurement data into the database via PhP My Admin.
The CSV has values going from 1 to 3000, some have decimals and some don't.
Due to this I don't think the table likes the mix up.
Most of the values show correctly, apart from when the values go over 999, one of the measurement values shows 999.99 for a value of 1138.
Is there any way to stop it doing this?
Thanks for any advice!
Well, that's the problem, data type. As per the docs:
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The
ranges of values for the arguments in MySQL 5.6 are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
So your DECIMAL(5,2) column type can hold numbers up to 999.99. You'll have to alter the table and make the column larger.
(As about why MySQL prefers to corrupt your data rather than reporting it, well, that's been its philosophy from the early years. You can play with SQL modes to fix that.)
Related
I need to set a column that can only accept integers ranging from 0 to 10 inclusive in SQL. It is for a rating system.
My current method is ratingSore int(2,0) not null
Will this method work, as I will also need to find the average rating using this column
I am afraid that if I try to find the average, it will round to an integer instead of giving me decimals
With MySQL 8.0.16 or higher, you can define a check constraint:
create table sometable (
-- ...
ratingSore int not null check (ratingSore between 0 and 10)
-- ...
)
See: example on dbfiddle
Important: MySQL versions before 8.0.16 supported check constraints in the syntax, but ignored them.
EDIT: as said by Mark, recent versions of MySQL (8.0.16+) support a CHECK constraint.
Regarding the average computation, the int column type only affect the data stored in this column.
When you will ask to compute the average, you will be able to get decimals as this will only be a "result", not stored in the column.
Your syntax is not correct. The int data type does not take two parameters. MySQL allows one, which is for display purposes only -- the parameter does not affect what actually gets stored.
You seem to want a numeric/decimal data type:
ratingSore numeric(2, 0) not null
The "2" says there. are two digits of precision. The "0" says that none of the digits are after the decimal point. Together, these say that the column stores any integer value between 0 and 99 -- which seems to be what you are asking for.
I save very large numbers in my database (usually with 50+ digits) and then need to run queries like (id is the label of column where the numbers are saved):
WHERE id % 2 = 0
I tried to use varchar data type for this column and although no error is generated while running the query, the returned result is mathematically wrong (the returned ids are not even).
does MySQL convert varchar to int while running my query and so is overflow the reason of the mistake in results?
what is the best choice for saving such large numbers on which I can do arithmetic operation latter? if decimal is the best candidate then what if I need to save the numbers with 100 digits?
Your only choice is to cast to a numeric/decimal value explicitly. In MySQL, that supports up to 65 digits of precision.
Here is a db<>fiddle with an example. Or an example using %.
What data type should i use for a SQL column to store Product version eg.
Version
0.1
0.1.1
0.2
1.1
1.1.647
2.0
.....
In query i should be able to sort them based on version number and i want an optimal query to find highest number.
Thanks
I would consider storing each part of the number in a separate TINYINT/SMALLINT field.
Perhaps have three or four numeric fields to the version table:
Major, Minor, Revision, Build
A good solution would be to use an integer building the value to store like so:
MAJOR * 10000 + MINOR * 100 + Revision
Assuming each one can range from 0..99. If you want to go 0..999 use
MAJOR * 1000000 + MINOR * 1000 + Revision
This will sort properly, will query easily, is compact (1 int column), is easily decomposed and can even be decomposed visually.
Storing in separate numeric fields is a good idea. Storing as a string in a single field will break sorting when one of the parts reaches 1000. For example, 1.2.999 will appear before (or shown as newer than) 1.2.1000 when it should appear after.
You may want to store the versions in two separate columns. One as VARCHAR which would store the version value verbatim (to display in UI) and one as DECIMAL for sorting.
How to store a version as a DECIMAL in MySQL which is sortable
Assuming each part of a version can be a maximum of 4 digits, a version can be stored as:
"#{major}.#{format(minor)}#{format(revision)}"
Where the format() function is defined as:
sprintf("%0.4d", i)[0,4]
This value can be stored in a DECIMAL(12,8) column.
E.g., version 2021.7.115 would be stored as 2021.00070115.
If each part of a version can be maximum 5 digits (sprintf("%0.5d", i)[0,5]), above format would require a DECIMAL(15,10) column.
E.g., version 2021.7.115 would be stored as 2021.0000700115.
Why DECIMAL and not INTEGER data type?
The advantage of DECIMAL is that more digits can be appended to the right side in future without affecting sorting, which is not possible for INTEGER columns. E.g., {major}.{minor}{revision} can be changed to {major}.{minor}{revision}{morestuff} without affecting sorting (which wouldn't be possible for INTEGER columns without updating existing values).
PS: How to store a version as VARCHAR in MySQL which is sortable
Tricks from above can also applied to store the version in a string column.
E.g., a format
"#{format(major)}#{format(minor)}#{format(revision)}"
can store version values as a string in a VARCHAR column. Storing versions in a VARCHAR column would require more storage space than DECIMAL but it might be a necessity if version values include non-numeric characters.
E.g.,
Version 1.2.4 would be stored as "000100020004".
Version 2021.7.115 would be stored as "202100070115".
Good day, I am confused with the datatype for MySQL.
I am using decimal as apparently it is the safest bet for accuracy in a business application. However, I find that when fields are returned I have values of 999999999.99, where my datatype is DECIMAL(10,2). So the actual value has overflowed outside the (10, 2) parameter.
Would it be correct that even though I have specified 10 places before the comma and 2 places after the comma. MySQL still stores the complete number?
Also would it be possible to turn off the maximum amount of digits displayed before and after the comma?
Would it be correct that even though I have specified 10 places before the comma and 2 places after the comma. MySQL still stores the complete number?
No, it wouldn't.
First, you specified 10 digits altogether; two are to the right of the decimal point, and eight are to the left.
Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.
Second, MySQL will silently convert the least significant digits to scale if there are more than two. That will probably look like MySQL truncates, but the actual behavior is platform-dependent. It will raise an error if you supply too many of the most significant digits.
Finally, when you're working with databases, the number of digits displayed has little to do with what a data type is or with what range of values it stores.
I have a MySQL database where I want to store phone numbers among other things.
The fieldtype is INT(10)
When I try to insert a number starting with a 0, like 0504042858 it's stored like 504042858. This only happens with phone numbers with leading zeros. When the number start with any other number, it's stored correctly.
What am I doing wrong?
You should probably store phone numbers as a varchar. Phone numbers are only numeric by accident.
You may also be interested in checking out the following Stack Overflow posts:
What datatype should be used for storing phone numbers in SQL Server 2005?
Common MySQL fields and their appropriate data types
You can give length INT(11) with attribute value UNSIGNED_ZEROFILL. it will fill all 11 digits and if any digit length is less than 11, it will add zero itself before the value.
This might solve your problem.
it is removing the leading zero because mathematically they are the same and removing the leading zero is a quick storage optimization. In addition it also makes the numbers easier to read imagine a number padded with several leading zeros in a column of several hundred numbers.
I agree with Daniel change your column to a varchar.