#1264 Out of range value fix? - mysql

When I try to insert the below into my MySQL
INSERT INTO `rooms` (`id`, `ip`) VALUES ('131213', '-259857341');
I fails with the follow error:
Warning: #1264 Out of range value for column 'ip' at row 1
I am looking around but haven't found how to fix or work it out...
My field is unsigned int which should work just fine for that entry.
What is the problem and how do I solve ?
I am using unsigned int because I wanted to store ips using inet_ntoa/aton.
EDIT:
I am using unsigned INT as recommend in MySQL website:
To store values generated by
INET_ATON(), use an INT UNSIGNED
column rather than INT, which is
signed. If you use a signed column,
values corresponding to IP addresses
for which the first octet is greater
than 127 cannot be stored correctly.
See Section 10.6, “Out-of-Range and
Overflow Handling”.
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html

A negative number is out of range for an UNSIGNED INT. Read the documentation for a full list of allowed values for a given data type.

Unsigned integer means non-negative value at least.
Not sure if this is what you need but you can try to convert signed integer to 4 bytes unsigned integer as your ipconverter does (http://www.silisoftware.com/tools/ipconverter.php):
INSERT INTO `rooms` (`id`, `ip`) VALUES ('131213', '-259857341' & 0xffffffff);

Firstly, by definition, you cannot insert a negative number into a unsigned int field. Change the field to int instead (or if possible use non-negative numbers).
Secondly i think that you should remove the single-quotes around the inserted number to that the the value is treated as an int and not a string.

Change your INT field into BIGINT and it will work for me smoothly.

Related

Does the maximum length of INT need to be specified in MySQL when creating a column?

I am writing an SQL query for creating a new column in a table. Part of my query includes this:
`phone` INT(8) UNSIGNED NOT NULL DEFAULT 0
The phone must have exactly 8 digits because that is how phone number works for the countries that I am working with. But I am not sure if I need to use INT(8) or only INT. The other way to write that portion would be:
`phone` INT UNSIGNED NOT NULL DEFAULT 0
I know for VARCHAR for example, you specify the length. For example: VARCHAR(50). But in the case of INT I am not sure, since in MySQL, it is already known that INT can store a maximum value of 4294967295 when it is unsigned, so using INT(8) may not make sense because INT will always be INT and by definition, able to store a maximum value of 4294967295 when using UNSIGNED (https://dev.mysql.com/doc/refman/8.0/en/integer-types.html). Thank you.
INT(8) is an obsolete formatting construct. Its best to ignore those on int types.
VARCHAR is probably better for PHONE types that don't have numberic operations done on them.

Why can't I change my INT from unsigned to signed? Is signed Zerofill possible?

I have a field called NUMBER in a table called TEST
NUMBER is int(8) Unsigned Zerofill Not Null
I need negative numbers in this field now and I'd still like them zerofilled
For example -00023419
I can change it to signed, but can't seem to get zerofilled.
if I do: alter table test modify number int(8) signed zerofill not null -
It stays unsigned zerofill
if I do: alter table test modify number int(8) zerofill not null -
It stays unsigned zerofill
if I do: alter table test modify number int(8) signed not null -
I get signed but no zerofill
When I had it set to signed, I put an a negative number then tried to change to zerofill and the number changed to 00000000 and everything was set to unsigned again. It it impossible to have signed zerofilled numbers?
https://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html says:
If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
Reported as a bug in 2006, closed as "Not a Bug".
https://bugs.mysql.com/bug.php?id=24356
Re your comment:
So does this mean no one out there has negative zero filled data in a mysql database?
Right. The most common use case for ZEROFILL is to make sure things like zip codes or bank account numbers use a fixed number of digits. These cases don't support negative values anyway, so why bother with ugly strings like -00001234?
can you convert it to zerofilled with PHP?
Yes, you can format numbers with zero-padding in PHP.
<?php
$n = -1234;
printf("Number is %08d\n", $n);
Output:
Number is -0001234
Read http://php.net/manual/en/function.sprintf.php for more neat formatting things you can do with printf()/sprintf().
ZEROFILL implies UNSIGNED.
MySQL Reference Manual https://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html
If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
With all the potential problems, I avoid using the non-standard MySQL ZEROFILL attribute.
Well, if your column is a foreign key or a primary key, MySQL doesn't allow this modification to happen. You have to do this beforehand.

What is the actual range of a MySQL INT column in this situation?

Ok, so I know that the INT type in MySQL has a range of either 0 to 4,294,967,295 or of -2,147,483,648 to 2,147,483,647
MySQL reference
My question is if I have a single row in an INT column with a value of -1 and all other rows are positive numbers, what is the actual range of that column? Because it has a single negative number, does the column automatically have a range of -2,147,483,648 to 2,147,483,647, or is the range more dynamic and instead is -1 to 4,294,967,294?
So to be more specific about the INT datatype; you can have either INT UNSIGNED or INT SIGNED. This is set when the column is created. I believe it defaults to SIGNED if you don't specify. So any INT column is either SIGNED or UNSIGNED and this is NOT dynamic.
INT UNSIGNED has the range 0 to 4,294,967,295
INT SIGNED has the range -2,147,483,648 to 2,147,483,647
So if even one row has a negative value, you have to use INT SIGNED. Giving the column the range -2,147,483,648 to 2,147,483,647.
The ranges you are seeing in the MySQL reference are describing whether or not you add in SIGNED (the default if left blank) or UNSIGNED as an attribute in your column definition. E.g., CREATE TABLE my_table(column_1 INT UNSIGNED...).
If you specify UNSIGNED you extend the upper bound of the data type (assuming the data type in question has a SIGNED/UNSIGNED option), but you also lose the lower end of the range. If you specify SIGNED or don't specify anything at all (then the default SIGNED will apply) then your upper bound is the upper bound of the SIGNED range.
So, for a SIGNED column of type INT (or if no attribute is specified), your range is -2,147,483,648 to 2,147,483,647. If you specify UNSIGNED your range is 0 to 4,294,967,295, and you would not actually be able to put -1 in that column and have it be properly stored as -1. Per http://dev.mysql.com/doc/refman/5.1/en/out-of-range-and-overflow.html, MySQL "stores the value representing the corresponding endpoint of that range" for integers. So inserting -1 into an UNSIGNED integer column will store 0 instead. If you need to store negative integer values, you need to use SIGNED. If you need an extended upper range and don't need to store negative numbers, you would want to use the UNSIGNED attribute.
The range of 0 to 4,294,967,295 is for UNSIGNED. As an example:
...
`type` int(10) UNSIGNED NOT NULL DEFAULT '0'
...
You have to specifically set the unsigned state for the column otherwise it is considered signed which will have the range of -2,147,483,648 to 2,147,483,647
No, the ranges are not dynamically adjusted to the filled data. SIGNED and UNSIGNED ranges are raw ranges, regardless what the table already contains.
CREATE TABLE `testing` (
`x` INT(11) NULL DEFAULT NULL
)
ENGINE=InnoDB
;
INSERT INTO testing VALUES (-1);
SELECT * FROM testing;
INSERT INTO testing VALUES (4000000000);
SELECT * FROM testing;
Returns "Out of range error", and data will be "-1 ; 2147483647" (greatest SIGNED INT 32 bits)

database column int limit

How can I limit my database column's integral input to a specific number of digits ?
CREATE TABLE tab (id INT <1 digit> not null, value INT <10 digits> not null);
Thank you
Add a check constraint (SQL Server) or trigger (MySQL, doesn't support CHECK constraints)
SQL Server example:
CREATE TABLE tab (
id tinynot null CHECK (id BETWEEN 0 AND 9),
value INT not null CHECK (id BETWEEN 1000000000 AND 9999999999)
);
If you only want one digit though, then use tinyint
If you aren't storing numbers (eg "123456789 bananas") but, say, phone numbers then use a varchar type. See https://stackoverflow.com/a/8284674/27535
Edit, you'd need a trigger in MySQL
The short version is using TINYINT UNSIGNED NOT NULL will be a more suitable data type, but it can't limit the values stored.
The longer version is that you may wish to read up on MySQL integer data types. You'll see that TINYINT is sufficient for your purpose as that is a 1-byte column that stores values from -128 to +127, or 0 to +255.
Secondly if you define it as TINYINT(1) then you are defining it as being a TINYINT with a display width of 1 digit. This will not prevent values larger than 10 being stored though. For more reading on this behaviour check numeric type attributes.

mysql : How can i protect to make field value negative

i am creating a table. and i am using some update queries on it.
this table has placeId, noOfGirls, noOfBoys. due to some update queries the values of these two fields noOfGirls and noOfBoys becomes negative.
Is there a way so i can convert these negative values to 0. means any constrains or something else so i can protect to be value negative.
Thank you in advance.
Easiest way is to use an unsigned integer type:
CREATE TABLE foo(bar INTEGER UNSIGNED)
The MySQL "numeric types" documentation has more information on what this means.
Declare the column as unsigned int type.
The unsigned range is 0 to 4294967295, so the value never become negative.
You can alter the schema of your existing table
alter table table_name modify column noOfGirls int(10) unsigned;
You can read it more here
Declare it unsigned zerofill works, negative values will be converted to 0. you will get the following warning though
Warning: #1264 Out of range value for column 'a1' at row 1
other ways: you may create a trigger
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html