See the below screenshot. Note that the insert statement lists the trade_id as 4404689907. The subsequent select lists the trade_id as 2147483647. Anyone have any idea what's going on here?
Your column in a signed INT which holds integers up to 2147483647. Your value is clearly larger than that. Even an unsigned INT only holds a value up to 4294967295. You will need to use BIGINT for that data.
See Integer Types (Exact Value)
Related
I have a field in my database table that is currently VARCHAR datatype but has numeric values in it so I want to change the data type. When I try to change it to MEDIUMINT, I get an error saying "#1265 - Data truncated for column 'fees' at row 1"
Any ideas how to change the data type? I'm using phpmyadmin to do this.
There may be beyond the range or may have string values in the column.
MEDIUMINT -8388608 to 8388607
https://dev.mysql.com/doc/refman/5.7/en/integer-types.html
I want to convert a CSV database into a MySQL one, I know I will never add any new row in the database tables. I know the max ID of each table, for example : 9898548.
What should be the proper way to compute the int size ? Does a CEIL(LOG2(last_id)) could be sufficient for this ? With my example, it would be LOG2(9898548) = 23.2387 so int(24) ? is this correct ?
When you're defining your table and you know your max values you can refer to the max table sizes. See http://dev.mysql.com/doc/refman/5.7/en/integer-types.html for a table of numeric sizes.
IDs are usually positive so you can use the unsigned numbers. In your case 9898548 is less than 16777215 (the unsigned MEDIUMINT max value) so that would be the most space efficient storage option. So your calculation is correct. You need 24 bits or 3 bytes, or a UNSIGNED MEDIUMINT.
CREATE TABLE your_table (id UNSIGNED MEDIUMINT PRIMARY KEY);
The brackets with numbers inside are to help MySQL display the number correctly, they don't do anything to the storage size. So INT(11) and INT (24), can both sure the same range of numbers. But the one defined INT (11) will only display a number with a column width of equivalent to 11 digits even if the number is smaller. See
http://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html
"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"
Yes, in this case, you need an integer type with a least 24 bits (equals 3 bytes). The smallest in MySQL satisfying this is UNSIGNED MEDIUMINT, according to the documentation.
Edit: Added the UNSIGNED.
I've got the following table in MySQL (MySQL Server 5.7):
CREATE TABLE IF NOT EXISTS SIMCards (
SIMCardID INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT,
ICCID VARCHAR(50) UNIQUE NOT NULL,
MSISDN BIGINT UNSIGNED UNIQUE);
INSERT INTO SIMCards (ICCID, MSISDN) VALUES
(89441000154687982548, 905511528749),
(89441000154687982549, 905511528744),
(89441000154687982547, 905511528745);
I then run the following query:
SELECT SIMCardID FROM SIMCards WHERE ICCID = 89441000154687982549;
However, rather than returning just the relevant row, it returns all of them. If I surround the ICCID in quotes, it works fine, e.g.:
SELECT SIMCardID FROM SIMCards WHERE ICCID = '89441000154687982549';
Why does the first SELECT query not work as I expected?
An integer in MySQL has a maximum value (unsigned) of 4294967295. Your IDs are substantially larger than that number. As a result, if you select * from your database by integer, your behavior is going to be undefined because the number you are selecting by cannot be represented by an integer.
I'm not sure exactly why you are getting the results that you are getting, but I do know that trying to select by an integer when your data can't be represented by an integer will definitely not work.
Edit to add detail I forgot: even a bigint in MySQL is not large enough to represent your IDs. So you need to make sure and just always use strings.
This must be a simple one but got no idea why is this happening.
Under this query:
INSERT INTO assist_reg (ar_id,ar_subid,ar_date) VALUES ('','2431052014','2014-05-31');
Field ar_subid on DDBB always records this value you can see on this screenshot:
ar_subid is a INT field with maximum of 20 characters, non-null with no predeterminate valur. this table is under UTF8-generalci.
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html - you are exceeding the int data type's max value. You need a bigint
I just can't understand why is my database (mysql) behaving like this! My console shows that the record is created properly (please, notice the "remote_id" value):
Tweet Create (0.3ms)
INSERT INTO `tweets` (`remote_id`, `text`, `user_id`, `twitter_account_id`)
VALUES (12325438258, 'jamaica', 1, 1)
But when I check the record, it shows that the remote_id is 2147483647 intead of the provided value (12325438258 in the example above)...
This table has many entries, but this field is always written with 2147483647... It was supposed to fill this space with an unique id (which I guarantee you is being generated properly).
That's because you're using the INT numeric type which has a limit of '2147483647', use BIGINT instead.
Source: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
My guess is that the value you are trying to insert is too large for the column. That number is suspicious in that it is the max value of a 32 bit signed integer.
Is the column type INT? To store that value you should make it a BIGINT. See Numeric Types from the MySQL manual.
As it is obvious you used a value with less size, so you need to use a larger type like BigInt ( and in application use long or int64