Tiny int store limited values in db - mysql

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.

Related

Set value lower than 0 in PHP getting 0

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.

MySQL: Using an Integer with Auto Increment as primary key - should I declare it as unsigned?

When creating a table, I often use an INTEGER column with AUTO_INCREMENT as primary key (surrogate key), like so:
CREATE TABLE 'my_table' (
user_id INTEGER AUTO_INCREMENT PRIMARY KEY,
[…]
);
(Note that the ellipsis is only there to focus the example on whats important.)
Now I've read that INTEGER columns can hold an integer from -2,147,483,648 to 2,147,483,647 and from 0 to 4,294,967,295 when unsigned.
Since user_id will always be unsigned (MySQL starts counting from 0 if I am not mistaken), would it make sense to explicitly mark this column as UNSIGNED to allow more users stored in that table (4,294,967,295 instead of 2,147,483,647)?
According to http://dev.mysql.com/doc/refman/5.0/en/integer-types.html, the size of an UNSIGNED INT and a signed INT, so that won't make a difference in your choice. As far as I know, AUTO_INCREMENT starts at 1, so if you think you will need more than 2,147,483,647 rows, go for the unsigned int.
Unsigned allows for improvement in your queries based on that field.
Aside from that, there is no real difference
See the article at this link >> Signed vs Unsigned for more information as to why this is faster for queries.
Suppose the following query, where 'quantity' is the INT field.
SELECT *
FROM customer
WHERE quantity <= 500
If quantity IS NOT unsigned, then and quantity field is an “int” and you have an index of this field, MySQL will define the range as -2147483648 to 500 and it will get the result based on this range.
However, if the quantity field IS unsigned, then that range will be 0 - 500. A much smaller scope.
The only other convention that applies here is the actual storage of information. Unsigned since it's base is 0-4m, requires more storage as the binary count is higher. Where signed, is 0-2m with 1 bit to flag if it is positive or negative. This results in a smaller storage requirement.
SUMMARY
Use Signed if you want to save space.
Use Unsigned if you want speed.

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.

Issues faced with int(11) datatype in MYSQL

I have a table in MYSQL in with a primary key id int(11) (auto-incremented). Now I have a program which reads some text file and enters the value in id column.
So my table should contains :
id
897413
791783
But what happens is that, I can't find big numbers in my table. Is this because of maximum value that int(11) can hold? Increased int(11) to int(20) still facing same problem. I can't change the datatype to big int as I have already implemented a lot of code.
EDIT:
I tried to insert a single record with id as 523826 and it got saved in DB as 450258. Why so?
Definition from mysql manual for the int data type:
A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.
The int type is always 4 bytes (32 bits). The 11 in int(11) is just the "display width", that only matters for UNSIGNED ZEROFILL columns. More details on this blog post.
INT in MySQL is 32 bits. INT(11) likely means you have a signed INT, which for an ID is useless. Changing it to an unsigned INT will automatically double the number of IDs available (since nobody uses a negative ID). Even though 11 "seems" bigger, it's because it takes into consideration the "length" of the number if it's the maximum negative number (-2147483648) which is 11 characters long.
BIGINT will let you go up to 64 bits, signed or unsigned. Again, unsigned will allow you twice as many IDs (>0).
As Andrew mentioned above, if your PHP does not support 64 bit integers then you will not be able to easily use them.
Hope that helps.