database column int limit - mysql

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.

Related

MySQL and phMyAdmin

I'm new to this website and using Mysql and phpMyAdmin. I need help with one of my table and I would really appreciate it. So, I created a table that has an Integer column I want to be able to limit it to only 7(Seven) digits I'm not quiet sure if this is possible using Mysql or phpMyAdmin.
I haven't tried any query on it. I want to limit the Integer type to only 7(Seven) digits.
This might not be the best possible solution but I think that if you were to store the integer as string in the format char(7) to limit the number of characters able to be entered it would get the job done.
I'm not familiar with Mysql in particular but here's some documentation on it : https://dev.mysql.com/doc/refman/8.0/en/char.html
I hope this helped.
In MySQL <8.0.16 You can't restrict the number of digits for an Integer. That has no meaning.
You can, however, use a DECIMAL type that allows you to specify the number of digits and the number of decimal places.
For example, DECIMAL(7,0) will define what you want.
Your CREATE statement becomes something like
CREATE TABLE IF NOT EXISTS myTable (
id INT AUTO_INCREMENT PRIMARY KEY,
someText VARCHAR(255) NOT NULL,
decimalValue DECIMAL(7,0)
) ;
If you're using MySQL 8.0.16 or later you can use a CHECK constraint to limit the value (as distinct from limiting the number of digits).
The example above becomes
CREATE TABLE IF NOT EXISTS myTable (
id INT AUTO_INCREMENT PRIMARY KEY,
someText VARCHAR(255) NOT NULL,
decimalValue INT,
CONSTRAINT `decValue_chk` CHECK (`decimalValue` <= 9999999))
) ;

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.

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.

#1264 Out of range value fix?

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.