Set value lower than 0 in PHP getting 0 - mysql

I have some table in my database with column with type BIGINT:
The problem is, when I set (by update or insert) value lower that 0 (for example -2) then in DB it is set 0.
Do someone know why?
(I have not any procedures to change this value before insert/update).
Db is MySql

You can read the difference between unsigned bigint and signed bigint.
MySQL Bigint
Unsigned bigint values are : 0 to 18446744073709551615
Whereas signed bigint values are : -9223372036854775808 to 9223372036854775807
So you must use the signed bigint

It looks like what your database is actually using is a BIGINT UNSIGNED, which has a range of 0 to 18446744073709551615. And according to the MySQL Reference Manual:
When an out-of-range value is assigned to an integer column, MySQL stores the value representing the corresponding endpoint of the column data type range.
This means that attempting to store a negative number in any UNSIGNED column will wrap around to 0.
To remedy this, it's best to explicitly set the data type to be BIGINT SIGNED.

Related

mysql sort functions properly when datatype is bigint(8) but fails as bigint(20)

I am using a column to store UNIX timestamp in seconds (divide it by 1000). I found bigint datatype to be large enough to store it. I created it using
...
createTimeStamp bigint,
...
However, when I ran
show create table tablename
It had created it as bigint(20). I did not notice it until my sort started running into problems. I then modified the schema to make it bigint(8) expressly as in:
alter table tablename modify createTimeStamp bigint(8)
and the sort functions well.
I researched and realized that bigint(20) has nothing to do with storage and is only used for display by padding spaces. If so, why is the sort not functioning when using bigint(20)?
Mysql alludes to errors when using numbers that exceed 64 bits.
http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html
BIGINT[(M)] [UNSIGNED] [ZEROFILL]
A large integer. The signed range is -9223372036854775808 to
9223372036854775807. The unsigned range is 0 to 18446744073709551615.
...
...
Some things you should be aware of with respect to BIGINT columns:
All arithmetic is done using signed BIGINT or DOUBLE values, so you
should not use unsigned big integers larger than
9223372036854775807 (63 bits) except with bit functions! If you do
that, some of the last digits in the result may be wrong because of
rounding errors when converting a BIGINT value to a DOUBLE.

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)

Tiny int store limited values in db

I've created field named e2s with tinyint datatype. When I store the value of 500 it converts it into 127. Now I changed it to smallint. It stored value 500. Why?
What is the difference between int, tinyint, smallint, mediumint, bigint to store values.
Take a look at http://dev.mysql.com/doc/refman/5.0/en/integer-types.html. TINYINT only stores 1 byte of data, thus allowing the integer range of storage from -128 to 127.
SMALLINT in other hand uses 2 bytes of storage, having a much wider range from -32768 to 32767.
Be aware of what you are going to store in TINYINT or SMALLINT columns. For instance, it's a bad idea to set an auto_increment PK column to SMALLINT datatype as you could easily overwhelm it's capacity.
Please see MySql reference on integer types:
http://dev.mysql.com/doc/refman/5.1/en/integer-types.html
It shows you the minimum and maximum values you can store.

When to use the different numeric data types - TINYINT / SMALLINT / MEDIUMINT / INT / BIGINT - MySQL

I read the answers given here: What is the difference between tinyint, smallint, mediumint, bigint and int in MySQL? , so I now know how they store the data, but I'm still not sure how to set my database up. For example, if I want a field to be either 0 or 1 (sort of binary, 0 = off, 1 = on), do I use TINYINT with a length of 1?
My main question is, what does the LENGTH setting determine? As each NUMERIC data type already has their own associated data size.
Also, what is the difference between SIGNED and UNSIGNED, and why should I choose one over the other?
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
Diffrence between SIGNED and UNSIGNED is with UNSIGNED you can store only positive numbers.
For example :
about INT (Normal INTEGER) values
The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.
If you are using PK auto_increment value then you should use UNSIGNED in this case.
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
This shows storage and range for each INTEGER types.
For binary fields use BIT.
the length of numerics specifies the precision before and after the comma. See here
An integer variable has 32 bits to store the integer value.
In signed integer the first bit is reserved to store positive or negative sign. So, a signed integer can use only 31 bits to store a value and hence its range will be −2,147,483,648 to +2,147,483,647.
Suppose if your program needs to store only positive integer greater than +2,147,483,647. You need to consider the long integer that will take 8 bits that will cause the wastage of memory.
Instead you can go with unsigned integer. In an unsigned integer no bit is reserved for the sign so now you have 32 bits to store the value. The only limitation with an unsigned integer is that you cannot use it to store negative values. The range of an unsigned integer of 32 bits will be 0 to 4,294,967,295.
Hope it clears your concept of signed and unsigned integer.

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.