MySQL out of range value for decimal column - mysql

After searching, I found plenty of out of range problems with people not knowing that the first digit, m, in decimal(m,n) is the total amount of digits. However, that is not my problem.
For the column in question I have the following:
Field | Type | Null | Key | Default | Extra
preco | decimal(50,2) unsigned | NO | | 0.00 |
The setting decimal(50,2) is way more than I really want or need. I really only want 10 digits total. Its a price, so anything over 100 million is probably ridiculous, so decimal(10,2) would probably be more appropriate. However, MySQL Is not accepting the following query:
INSERT INTO `produto` (`tipo_id`, `preco`, `qtd`, `opcao1`) VALUES (110, '77888555.43', '10', 'Azul')
I tried the query through CodeIgniter, phpMyAdmin and directly in the MySQL command line client. I also tried it without the quotes on the decimal value but I always get the same error:
"Out of range value for column 'preco' at row 2"

You are sending the value for preco as a string which is accepted by MySQL in the same way as a numerical value.

I have tried it with SQLyog, it is working perfect. I tried to change both ways in SQLyog directly through Table Data tab and using Query. It is just working. it seems there is no problem in Decimal Range, but problem is some where else. See the screenshots attached
Directly through Table Data Tab
Through insert query
Table Structure
Hope it help...

Related

How to tackle data truncation error when copying rows from one table to another, both tables having same schema?

Database has two tables with same schema.
date VARCHAR(20),
track VARCHAR(150),
race_number INT,
horse_number INT,
early_last5 FLOAT(10,1),
PRIMARY KEY (track, race_number, horse_number, date)
One is named sectional_table and another is window_sectional_table.
I want to copy all contents of sectional table to window_sectional_table.
I do the most logical thing possible.
INSERT INTO window_sectional_table SELECT * FROM sectional_table;
Unfortunately, I am terrorized by this error.
Data truncated for column 'early_last5' at row 1
I investigate row 1. Row 1 looks like this.
+------------+----------+-------------+--------------+-------------+
| date | track | race_number | horse_number | early_last5 |
+------------+----------+-------------+--------------+-------------+
| 2021-05-03 | GUNNEDAH | 1 | 1 | 0.0 |
+------------+----------+-------------+--------------+-------------+
How do I proceed? I believe the value 0.0 should have been auto filled for null value.
The Data truncated for column 'early_last5' at row 1 error is referring to your INSERT statement rather than which specific value in the database is being truncated.
I believe the value 0.0 should have been auto filled for null value.
Nope. That’s not defined in your INSERT and it’s not defined in the table creation statement.
How do I proceed?
The simplest method would be to convert the FLOAT value to a DECIMAL for the INSERT, complete with an IFNULL if you want NULL to be converted to 0.0. The FLOAT data type has long been a thorn in the side of many people who expect it to work like a typical decimal.
Here’s a quick SQL statement that should get you moving again:
INSERT INTO `window_sectional_table` (`date`, `track`, `race_number`, `horse_number`, `early_last5`)
SELECT `date`, `track`, `race_number`, `horse_number`,
CAST(IFNULL(`early_last5`, 0) AS DECIMAL(10,1))
FROM `sectional_table`
ORDER BY `track`, `race_number`, `horse_number`, `date`;

Why are numbers inserted into a VARCHAR column without the ' ' but string texts aren't?

So I tried to experiment with the INSERT INTO code. I tried using the statement,
INSERT INTO cats (name, age)
VALUES ( 8, '3');
just to try it out and see what happens. It turns out it's still able to store both values. Under name, was 8, and under age was 3. I also tried to insert the name, Momo, without any quotes but it did not work. How come the number 8, worked without the ' ' and Momo didn't'? Also, how come when I use ' ' for the integer, 3, it still took that value as age?
Last part of the question that I just want to clarify. I tried inserting 'Mimi' in the age value and the table showed 0. I just assumed that meant a null value because 'Mimi' isn't an integer. Is that correct? If so, then why did '3' work?
For more clarification, my cats table looks like this
mysql> SHOW COLUMNS FROM cats;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(100) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
I'm a true beginner so I'm sorry for the very simple question
In SQL, string constants need to be surrounded by single quotes. Otherwise, they are interpreted as an identifier -- presumably a column name in most contexts, but it could also be a variable name.
A values() statement has no way to reference columns, so Momo will generate an error that the identifier is not recognized. Note that double quotes are an exception in some databases, including MySQL. They can surround either identifiers or string values.
In a numeric context, such as when inserting a value into a numeric column, SQL converts strings to numbers. In most cases, the SQL will generate an error, if the string does not convert cleanly to a number.
I think the assumption you are making is that '8' is a string, and 8 is an integer.
Making this assumption is usually a good one, in most programming languages. The SQL language however is more lenient. It already knows what the column type is, so it can figure out how to store the value and what you meant.
So in a lot of cases 8 and '8' and "8" are identical.

Is it better to have many columns or a single column bit string for many checkboxes

I have the following scenario:
A form with many checkboxes, around 100.
I have 2 ideas on how to save them in database:
1. Multicolumn
I create a table looking like this:
id | box1 | box2 | ... | box100 | updated| created
id: int
box1: bit(1)
SELECT * FROM table WHERE box1 = 1 AND box22 = 1 ...
2. Single data column
Table is simply:
id | data | updated | created
data: varchar(100)
SELECT * FROM table WHERE data LIKE '_______1___ ... ____1____1'
where data looks like 0001100101010......01 each character representing if value was checked or not.
Considering that the table will have 200k+ rows, which is a more scalable solution?
3. Single data column of type JSON
I have no good information about this yet.
Or...
4. A few SETs
5. A few INTs
These are much more compact: about 8 checkboxes per byte.
They are a bit messy to set/test.
Since they are limited to 64 bits, you would need more than one SET or INT. I recommend grouping the bits is some logical way, based on the app.
Be aware of FIND_IN_SET().
Be aware of (1 << $n) for creating the value 2^n.
Be aware of | and & Operators.
Which of the 5 is best? That depends on the queries you need to run -- for searching (if necessary?), for inserting, for updating (if necessary?), and for selecting.
An example: For INTs , WHERE (bits & 0x2C08) = 0x2C08 would simultaneously check for 4 flags being 'ON'. That constant could either be constructed in app code, or ((1<<13) | (1<<11) | (1<<10) | (1<<3)) for bits 3,10,11,13. Meanwhile, the other flags are ignored. If you need them to be 'OFF', the test would be WHERE bits ^ 0x2C08 = 0. If either of these kind of test is your main activity, then Choice 5 is probably the best for both performance and space, though it is somewhat cryptic to read.
When adding another option, SET requires an ALTER TABLE. INT usually has some spare bits (TINYINT UNSIGNED has 8 bits, ... BIGINT UNSIGNED has 64). So, about one time in 8, you would need an ALTER to get a bigger INT or add another INT. Deleting an option: suggest just abandoning that SET element or bit of INT.

Mysql float type issue

I have posted a question here earlier and got an awesome answer!
Stuck in building mysql query
But it lacks a one moment!
I am having a column in mysql table, defined as:
`bet_price` float(10,2) NOT NULL DEFAULT 0.00,
For a certain row the value is: 10000
A query like: SELECT bet_price, MIN(bet_price) AS min_price WHERE ID = :id
Will return a data like this:
bet_price | min_price
---------------------
10000 | 10000.00
And in queries this part fails for me.
I've tried to use a functions like FORMAT and TRUNCATE - but this did not help me.
could be there are some implicit conversion of some conversion in output rendering so if you need always same value then cast properly
cast(a.bet_price as decimal(10,2), MIN(b.bet_price)
or
a.bet_price , cast(MIN(b.bet_price) as UNSIGNED)

MySQL Inserting invalid dates

again, this qn was from the practice qns in the mysql certification guide book ...
QUESTION
Here's the structure of a table typetest with three columns (number, string, and dates). As- sume the server is running in MySQL's “forgiving” SQL mode.
mysql> DESCRIBE typetest;
+--------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| number | tinyint(3) unsigned | YES | | NULL | |
+--------+---------------------+------+-----+---------+-------+
You perform the following INSERT operation on table typetest:
INSERT INTO typetest VALUES (1000,'yoodoo','999-12-31');
What data values will actually be stored in the table? Provide a short explanation.
ANSWER
+--------+--------+------------+
| number | string | dates |
+--------+--------+------------+
| 255 | yoodo | 0000-00-00 |
+--------+--------+------------+
The inserted number 1000 is too big to fit in the TINYINT UNSIGNED column, so the highest pos- sible value (255) is inserted. 'yoodoo' is too long for a CHAR(5) column and is thus truncated to five characters. '999-12-31' is a date that is earlier than the earliest possible DATE value ('1000-01-01'). This is interpreted as an invalid date, so the “zero” date is stored.
when i tried inserting '999-12-31' into a date i get 0999-12-31. i read some where in the certification guide that mysql may be able to insert dates out of the range 1000-01-01 to 9999-12-31. but in the exam what do i answer?
You can read the Constraints on Invalid Data section. Unfortunately it doesn't give a straight answer for your question. It says "If the date is totally wrong (outside the server's ability to store it), the special “zero” date value '0000-00-00' is stored in the column instead.", and I would say storing 0999 isn't impossible...
Found this reported as a bug. The official response is that you can do it, but it is not supported, and you are on your own.
Exams and real life are different things. It's difficult to prove your opinion, if in book writed another. But i would answer that i tested by my own.
Mysql Date Range: 1000-01-01 to 9999-12-31 (ref)
Here you are trying to insert '999-12-31'. Since this date is not in the above range mysql defaults the value to 0