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.
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.
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)
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.
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