How to write a large number in SQL table with Workbench? - mysql

When I tried to fill in a big number, error. How to add the maximum limit of INT?
UPDATE `test`.`number` SET `idNumber` = '36552124313028521236524313028' WHERE (`idNumber` = '365521');

You could try use a BigInt
If you want a number larger than the largest 64-bit unsigned integer 18,446,744,073,709,551,615 then you will need to store it as a varchar or some other Text form
refer to the MySQL data types for further info

It depends on how the column is to be used. For calculations or auto_increment attribute, numerics should be used. As you say you would like to add a maximum limit, by ADD I suppose you would like to define a length value to your liking. However, the whole number types such as small int, int, big int have a predefined maximum range , which can not be changed.(MySQL 8.0 users may try the check option, which is ignored in previous versions) If you need to define the limit for the whole number, there is a workaround by using decimal(n,0) to make the number always appear as a whole number.
For identifiers which do not require numerical calculations, varchar is generally acknowledged for strings that have a dynamic range, and char is more suitable for those having a static length,such as province acronym e.g AZ (Arizona) AR (Arkansas) CA (California). At the first glance of your idNumber column, I reckon it's better used for it's string's nature rather than numerics.
Last but not least. Please refrain from using a varchar for string-looking values that are prone to calculations,such as IP ADDRESS. It appears as a string in its dotted format, but deep inside it has an inherent nature of numerics. For instance, IPV4 has a range from 0.0.0.0 to 255.255.255.255 , which can be treated as a formula of (256 * 256 * 256 * 256) . Thus it is a perfect fit for the unsigned integer type in terms of length and can be calculated when necessary. To display it in its dotted format , use the inet_ntoa() function. e.g select inet_ntoa(3232235777);

Related

bigint not insert correctly

I have a database and a bigint column type. Everytime I insert value to this column I always got the wrong number. for example, I insert value "198705122006041001" and it always insert this value "2147483647".
I use laravel for my project, if I use eloquent to display the bigint it wont display correctly but if I use PDO manually, it will display correctly.
PHP has no bigint data type, it overwflows and uses int.max instead. You have to represent bigints as string or float. Be aware, that float is not precise and can lead to surprises.
Do the mathematical transformations in MySQL and don't forget to cast to bigint when necessary.
You should check this again, because it's definitely int, but not bigint. 2147483647 is a maximum possible value for signed int
See https://laracasts.com/discuss/channels/eloquent/fbid-bigint-with-model-and-eloquent. A Laravel user had the same problem, and eventually had to cast the result to a string.
Another user comment pointed out that XAMPP provides only a 32-bit PHP binary, which limits the size of a PHP integer.
Check your PHP integer size with this code:
<?php
echo "Integer size can be determined using the constant PHP_INT_SIZE="
. (PHP_INT_SIZE * 8)
. " bits, maximum value using the constant PHP_INT_MAX="
. PHP_INT_MAX;
See also http://php.net/manual/en/language.types.integer.php
Install https://laravel.com/docs/5.3/homestead and get a 64-bit PHP binary.

BIGINT max 255 characters?

I need my integer column to be able to go up to 2000, so I made it INT(2000), but it keeps saying;
Display width out of range for column (max = 255)
I have tried using MEDIUMINT(2000) and BIGINT(2000), but both give the same message.
The number used in a SQL type is the width of the type, not the maximum value. When used on a numeric type, it represents the maximum number of base-10 digits used to represent a value in that column: for instance, an INT(5) can represent any value up to 99999.
A number with a maximum value of 2000 can be stored in any numeric column with a width of 4 or greater. But don't worry about the width; just use a normal INT and let the database use whatever size is default for that type. (It will be more than 4, but that's OK.)
BIGINT(255) Means a Big Integer with 255 digits.
And well , It's a very big number especially when the UNSIGNED flag is used.
BIGINT is mostly used for Id of something.
So i don't think that you need a number more than a 255-digit number.
Or if you do need , keep it in a string.

MySQL truncates my first zeros data

I have a MySQL column "phone" , and when data comes from my php form, if there's a zero at the beginning, it will disappear in the table, for example :
"06719823" becomes "6719823"
I first though it was a problem from php, cause I'm using mysql_real_escape_string(), but then I tried to modify the SQL field directly on phpmyadmin, and I can't add a first 0, it always delete it.
Colonne Type Interclassement Attributs Null Défaut Extra
phone int(10) Oui NULL
What's wrong ? Should I assign an "interclassement" utf8_general_ci ?
Change your column type to char(10) for 10 digit phone numbers.
If the column type is int (integer), the number will be internally represented as an integer, meaning "first 0s" won't be stored, as they hold no meaning for integers.
Since what you are actually trying to store has meaning as a sequence of characters, and not as a quantity, it would make more sense to store it as a char(n), for n-digit sequences, or as a varchar for sequences whose size varies a lot.
Make your phone attribute as Varchar or Text to avoid this.
Phone numbers can at time also contain brackets and hyphens plus you can avoid your problem as well.
Change your data type. Int Data type will not store the starting 0's.
You can try as suggested above char or varchar
Integers : 06719823 = 6719823 = 0006719823
Save the phone as varchar if you would like to retain zeros in the begining

mysql int fields does not truncate

as all you know, when you describe varchar or integer fields you should set the length of them...
something like int(5) or varchar(5)...
but when you try add 123456 to both fields.. while varchar field truncates the value, integer field does not truncate it...
so what's the aim of describing int length?
int(5) does not do what you think it does: it specifies an integer field with a display width of 5 digits, i.e. numbers shorter than 5 digits will be padded with space characters.
In MySQL, int values are always 4 bytes wide and can go from -2,147,483,648 to 2,147,483,647.
See http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html.
The N in INT(N), indicating application display length; is was very misleading, due to the syntax similarity to VARCHAR(N), and understandably, often misunderstood. It's effectively meaningless for all applications I've seen.
This goes for all TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT.
It appears this "length" is used for display only: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)
The size of the INT type is neither bits nor bytes. It's just the display width that is used when the field has ZEROFILL specified.
See this blog article for an in depth explanation.
FRom 10.2. Numeric Types
MySQL supports an extension for
optionally specifying the display
width of integer data types in
parentheses following the base keyword
for the type. For example, INT(4)
specifies an INT with a display width
of four digits. This optional display
width may be used by applications to
display integer values having a width
less than the width specified for the
column by left-padding them with
spaces. (That is, this width is
present in the metadata returned with
result sets. Whether it is used or not
is up to the application.)
The display width does not constrain
the range of values that can be stored
in the column. Nor does it prevent
values wider than the column display
width from being displayed correctly.

MySQL Tri-state field

I need to create a good/neutral/bad field. which one would be the more understandable/correct way.
A binary field with null (1=good, null=neutral, 0=bad)
An int (1=good, 2=neutral, 3=bad)
An enum (good, neutral, bad)
Any other
It's only and informative field and I will not need to search by this.
NULL values should be reserved for either:
unknown values; or
not-applicable values;
neither of which is the case here.
I would simply store a CHAR value myself, one of the set {'G','N','B'}. That's probably the easiest solution and takes up little space while still providing mnemonic value (easily converting 'G' to 'Good' for example).
If you're less concerned about space, then you could even store them as varchar(7) or equivalent and store the actual values {'Good','Neutral','Bad'} so that no translation at all would be needed in your select statements (assuming those are the actual values you will be printing).
In Mysql you ought to be using an enum type. You can pick any names you like without worrying about space, because Mysql stores the data as a short integer. See 10.4.4. The ENUM Type in the documentation.