Mysql Warning Question? - mysql

I inserted some info into my mysql database and I got the following error listed below. What does this mean and how can I fix it?
1 row(s) inserted.
Inserted row id: 1
Warning: #1265 Data truncated for column 'summary' at row 1
Here is my Mysql tables structure below.
CREATE TABLE mem_articles (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
member_id INT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
summary VARCHAR(255) DEFAULT NULL,
content LONGTEXT NOT NULL,
date_created DATETIME NOT NULL,
date_updated DATETIME DEFAULT NULL,
PRIMARY KEY (id)
);

I think it means that the amount of characters you attempted to insert into the summary column exceeded 255, perhaps you should alter it to be TEXT instead of VARCHAR(255).
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

It means that the data were "truncated", which in MySQL terminology means either it was truncated, or it was changed into something totally different if it was incompatible with the type.
This behaviour sucks; if you don't want it, use
SET SQL_MODE='TRADITIONAL'
Then it will behave like a sensible database (unfortunately this will probably break your entire code base if it's an existing application)

I would suggest setting the type to "longtext" or something larger.

Related

MySQL: Expression of generated column contains disallowed function? CONCAT?

I have a table with a virtual generated column that concatenates five other columns (int and char) using CONCAT_WS(). This table contains 200-odd records and is never updated - it's just used as a lookup table. Recently, after months of untroubled processing, when I update records in a child table during which a SELECT is performed on this table, I sometimes see this error (ignore the "ITEM UPDATE FAILED" - that's me):
I am in development with a many changes every day, so it is impossible for me to determine if there is a correlating change. I have recently added "created" and "lastmodified" datetime fields to several tables with CURRENT_TIMESTAMP for DEFAULT or ON UPDATE, but not to this table.
Here's the table:
{EDIT} --- adding table definition:
CREATE TABLE `cpct_fixedfield` (
`id` int(11) UNSIGNED NOT NULL,
`name` varchar(256) NOT NULL,
`label` varchar(50) NOT NULL,
`field` int(11) NOT NULL,
`start` int(11) NOT NULL,
`rectype` int(11) NOT NULL ,
`mediatype` char(1) NOT NULL DEFAULT '' ,
`length` int(11) NOT NULL,
`userdefined` tinyint(1) NOT NULL,
`defaultval` varchar(5) NOT NULL,
`helpcode` varchar(10) NOT NULL,
`mandatory` varchar(2) NOT NULL ,
`idx` varchar(20) GENERATED ALWAYS AS (concat_ws('.',`field`,`rectype`,`mediatype`,`start`,`length`)) VIRTUAL NOT NULL)
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
The length of the data in field never exceeds 11chars. I can view the entire table in pma or Mysql Workbench and the virtual field materialises in all records without complaint, which suggests to me that there is nothing wrong with either the expression for the virtual column or the data in the columns that expression draws on.
The error occurs in several contexts when I am updating a child table. All the updates occur in Stored Procedures/Functions. One section of code that seems to trigger the error is this:
SET idxvar = CONCAT_WS(".", SUBSTRING(tmpfldkey,3,1), rectype, ptype, position, "%") COLLATE utf8mb4_general_ci;
SELECT id INTO ffid FROM cpct_fixedfield WHERE idx LIKE idxvar AND idx != "0.0..6.2";
All the variables involved are varchars or ints. utf8mb4_general_ci is used throughout the database.
I cannot find any reference in MYSQL documentation to CONCAT or CONCAT_WS being unsafe, and none of the columns referenced has a default using a non-deterministic function. All the other questions I can find in this forum and elsewhere about this error have arisen because of the use of non-deterministic functions like CURRENT_TIMESTAMP() in the virtual field, or a component of the field.
I replaced the SELECT on the table with a (large) CASE statement and all was well, and in fact, after I did this then reverted to the SELECT I had no errors for many hours. But it just happened again (so I'm back to the case statement).
I have run out of ideas - I'm hoping someone has some knowledge/experience that can help.
Thanks

phpMyAdmin - "Please enter a valid length"

I'm trying to create a table in phpMyAdmin, and I keep getting the same error no matter how I manipulate the SQL code. This is the preview SQL that phpMyAdmin generates
CREATE TABLE `puppies`.`animals` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`puppy_name` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`breed_id` INT(11) NOT NULL ,
`description` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`price` DECIMAL(10,2) NOT NULL ,
`picture_url` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`sold` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
I've tried it with multiple variations of brackets and commas.
I have also faced the same issue and what I did was clicked on Preview SQL and copy the sql query and paste it in the SQL Run
To those still experiencing this, and don't want to wait for it to randomly work again:
I just encountered this, and cannot find any explanation other than some bug.
I tried:
CREATE TABLE `database`.`measurement_types` (
`TypeID` INT(2) NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(32) NOT NULL ,
`Description` VARCHAR(256) NOT NULL ,
PRIMARY KEY (`TypeID`)) ENGINE = InnoDB;
Which produced the same "Please enter valid length" error
Tried a few times with different length values, but kept getting the same error.
--SOLUTION--
So I just created the table with a single column first, then altered it with the two other columns like so:
CREATE TABLE `database`.`measurement_types` (
`TypeID` INT(2) NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`TypeID`)) ENGINE = InnoDB;
And then:
ALTER TABLE `measurement_types`
ADD `Name` VARCHAR(32) NOT NULL AFTER `TypeID`,
ADD `Description` VARCHAR(256) NOT NULL AFTER `Name`;
And that worked.
I also tried to delete the table and create it with the first SQL again, and this time it worked. Seems pretty random
I've had the same issue, seems to be a bug with VARCHAR fields. My solution was to make those fields INT, create the table, and then change them back to VARCHAR
You can also solve it by restarting your mysql... It worked for me.
It is a bug with varchar. If you change collation to utf8mb4_general_ci it should fix the problem
There seems to be some issue with PhpMyAdmin.
It needs 'Collation' value if column type is of varchar. You will encounter the error “Please enter a valid length” if the Collation field is empty for varchar. So basically both fields 'Size' and 'Collation' are mandatory and cannot be empty.
Please set Collation field with some value like 'utf8mb4_general_ci' to resolve the issue.
I solved the issue of "Please enter a valid length" by adding one by one column separately and by giving length to all the columns as the database needs to know how much memory it will consume.
It is definitely a problem with varchar fields but does not always happen.
You can still create your table by copying out the SQL query and executing the raw query.
As stated in the official docs, I think it is mandatory to give a specific length in later versions of phpMyAdmin.
I solved my “Please enter a valid length” by typing the length values for the data types that weren't of a dynamic memory allocation type. So therefore they obviously needed to know how much memory they could use for storage. Or by all means a valid length
If you use Varchar you have to give a length. Otherwise, it will not save.
My solution was to set a value for every varchar type.

MYSQL (Percona) Failed to read auto-increment value from storage engine

I am getting a "Failed to read auto-increment value from storage engine" error from MySQL (Percona), after trying to:
LOAD DATA LOCAL INFILE
I have been trying to find an answer on stackoverflow and google, but the answers did not solve the problem/error for my case.
The following shows Auto_increment as 1:
show table status like 'my_table_name';
Setting the auto increment value to the next value or to 1 did not solve it:
ALTER TABLE `my_table_name` AUTO_INCREMENT = 1;
Dropping the auto incrementing id column and recreating it did not solve it.
Dropping the table and recreating it did not solve it.
Dropping the database and recreating it did not solve it.
Destroying all data and rebuilding my docker development environment did not solve it.
Check table extended gave "OK":
CHECK TABLE distributor_users EXTENDED;
And it is the only auto incrementing value in the table.
This is what I use to create the table:
CREATE TABLE `my_table_name` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`x0` varchar(191) DEFAULT NULL,
`x1` varchar(4) DEFAULT NULL,
`x2` varchar(191) DEFAULT NULL,
`x3` varchar(191) DEFAULT NULL,
`x4` varchar(191) DEFAULT NULL,
`x5` varchar(191) DEFAULT NULL,
`x6` DATE DEFAULT NULL,
`x7` int(10) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `x0` (`x0`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
What could it be? Any lead to start looking at is highly welcomed!
I solved it myself by adding to my query:
SET id = NULL
The new query now looks like this:
LOAD DATA LOCAL INFILE '$csv_file_location' INTO TABLE $destination_table
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
IGNORE 1 LINES
(x0,x1,x2,x3,x4,x5,x6,x7)
SET id = NULL;
Although my query worked previously without it.
MySQL was trying to place the first column in the csv file into the first column of the database. Setting the column that has the auto_increment to NULL solved it.

MYSQL allowing me to insert nulls in PK and FK

I have been testing a database i am doing right now and i am noticing that it is letting me insert null values into fields that are part of a primary key, despite stating in the script that the value of the field should be NOT NULL. I am using MAC's MySQL Workbench, and I have been googling around and can't figure out why this is happening. (Maybe I am too brain-fried right now... I am even starting to doubt myself)
Part of the script of the database creation (these are the tables I have tested..):
DROP DATABASE IF EXISTS solytierra ;
CREATE DATABASE IF NOT EXISTS solytierra DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE solytierra ;
DROP TABLE IF EXISTS solytierra.Cliente ;
CREATE TABLE IF NOT EXISTS solytierra.Cliente (
CIF VARCHAR(25) NOT NULL,
Nombre VARCHAR(100) NULL,
EmailGeneral VARCHAR(45) NULL,
Web VARCHAR(45) NULL,
Notas VARCHAR(150) NULL,
insertado Timestamp,
CONSTRAINT pk_Cliente PRIMARY KEY (CIF)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS solytierra.PersonaContacto ;
CREATE TABLE IF NOT EXISTS solytierra.PersonaContacto (
Cliente_CIF VARCHAR(25) NOT NULL,
Nombre VARCHAR(50) NOT NULL,
Apellidos VARCHAR(100) NOT NULL,
Notas VARCHAR(150) NULL,
CONSTRAINT pk_PersonaContacto PRIMARY KEY (Cliente_CIF , Nombre , Apellidos),
CONSTRAINT fk_PersonaContacto_Cliente FOREIGN KEY (Cliente_CIF)
REFERENCES solytierra.Cliente (CIF)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
...
It will let me create Clients without CIF, "PersonaContacto" without Cliente_CIF or without "Nombre"....
I have also tested other databases that i already had that used to work and it is happening the same in an all them.
Got it!!
I don't know what sql mode i was running on by default, but with this:
SET sql_mode = TRADITIONAL;
It is now running perfectly! I didn't know that there were different sql modes! Thanks a lot to everyone for your time and efforts! It really helped me to see that the problem was in my workbench, not the code and look for the answer accordingly! I hope this thread will be useful for future beginners like me!
If the value being stored in the column CIF is actually a NULL, then the expression LENGTH(CIF) should also return NULL. (If it's a zero length string, then LENGTH(CIF) will return 0.
To verify:
SELECT c.CIF, LENGTH(c.CIF) FROM solytierra.Cliente c ;
SELECT c.CIF FROM solytierra.Cliente c WHERE c.CIF IS NULL;
If you are running an INSERT statement, I can't explain the behavior you are observing, either MySQL allowing a NULL value to be stored or MySQL providing an implicit default value.)
If it's a zero length string being stored, that's the behavior we would expect if the columns were not explicitly declared to be NOT NULL but were later declared to part of the primary key. It's also the behavior we'd expect if the column were defined NOT NULL DEFAULT ''.
When the NOT NULL is omitted from the column declaration and the column is later declared to be part of the PRIMARY KEY, MySQL will use an an implicit default value based on the datatype of the column (zero length string for VARCHAR, zero for an integer, etc.)
But I'm not able to reproduce the problem you report, with the table definitions you've posted.
I recommend you check the table definition by getting the output from:
SHOW CREATE TABLE solytierra.Cliente;

Duplicate entry '2147483647' for key 1

Strange problem I can't seem to get my head around. I have a table in a MySQL database with the following structure...
CREATE TABLE IF NOT EXISTS `tblbaseprices` (
`base_id` bigint(11) NOT NULL auto_increment,
`base_size` int(10) NOT NULL default '0',
`base_label` varchar(250) default NULL,
`base_price_1a` float default NULL,
`base_price_2a` float default NULL,
`base_price_3a` float default NULL,
`base_price_1b` float default NULL,
`base_price_2b` float default NULL,
`base_price_3b` float default NULL,
`site_id` int(11) default NULL,
PRIMARY KEY (`base_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=134 ;
The last base_id I have in there is 132. I assume a couple of records have been deleted to auto_increment is set to 134, as you can see about. I am trying to run the following SQL statement, and when I do, I get the error "Duplicate entry '2147483647' for key 1".
INSERT INTO tblbaseprices (site_id, base_size, base_price_1a, base_price_2a, base_price_3a, base_price_4a) VALUES ('', '', '', '', '', '')
Does anybody have any ideas?
Many thanks!
2^31 − 1 = 2,147,483,647
The number 2,147,483,647 is ... the maximum value for a 32-bit signed integer in computing
2147483647 is the largest int value for mysql. Just change the type from int to bigint.
With you code I got this error - Unknown column 'base_price_4a' in 'field list'.
It means that you are trying to insert into another table (maybe in another schema), and that table has primary key INT and AUTO_INCREMENT=2147483647.
you've hit the 32-bit integer limit, thus preventing the auto increment from incrementing. switching your pk to bigint with a higher column length should fix the issue.
Also, if your PK is never going to be negative, switching to an unsigned int should give you more space.
Try changing the auto_increment column to bigint instead of int, then the max value would be '9223372036854775807' or even '18446744073709551615' if you make it unsigned (no values below 0).
Change your Auto_Increment to the last id in the column so it is continued where it left off.
Be sure you do not delete auto_increment, otherwise it will continue to produce the error.
You're inserting empty strings into numerical columns. As far as I can see, you're also inserting into a column that does not exist in the schema. My guess is this has something to do with your error.
signed and unsigned issue
alter table tblbaseprices
modify column site_id int(10) unsigned NOT NULL;
reference - http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
make sure unsigned for foreign key (in this case could be the site_id)
it could be caused by trigger,
there is no int(11), the max it can go is int(10)
there is no need to allow negative value for ID
to be consistently using same data type for primary key
Today I got the error duplicate key 2147483647
I think it came out when I tried to insert a record into database from PhpMyAdmin, while typing, I also tried to enter the key value and it was eider lower than the current Next autoindex or I tried to type something like 99999999999999 as the key field, and that caused it to set Next autoindex to maximum
Anyway, the erorr was caused because Next autoindex was 2147483647 for that table.
My table was empty so I fixed it by this query:
ALTER TABLE table_name AUTO_INCREMENT = 0
if your table contains data, then replace 0 with your maximum key plus 1
it's a database issue. check your phpmyadmin > your DB > structure, your primary key should be setted in "bigint", not just "int"
CREATE TABLE IF NOT EXISTS `tblbaseprices` (
`base_id` bigint(11) NOT NULL auto_increment,
`base_size` int(10) NOT NULL default '0',
`base_label` varchar(250) default NULL,
`base_price_1a` float default NULL,
`base_price_2a` float default NULL,
`base_price_3a` float default NULL,
`base_price_1b` float default NULL,
`base_price_2b` float default NULL,
`base_price_3b` float default NULL,
`site_id` int(11) default NULL,
PRIMARY KEY (`base_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=134 ;
A good explanation of that is here: http://realtechtalk.com/Duplicate_entry_2147483647_for_key_PRIMARY_MySQL_Error_Solution-2015-articles
Essentially you are trying to insert a value larger than the maximum size an INT supports which is literally the number being given to you in the error.
If you are importing data than one of the fields contains a larger value than the INT size. You could also modify your table to be a BIGINT which would take care of the issue as well (of course at the cost of extra disk space).
A common reason is that you are using some script generating large/random numbers. You should add some check to make sure the size is the same or lower than that maximum INT size of 2147483647 and you'll be good to go.
Duplicate entry '57147-2147483647' for key 'app_user' [ INSERT INTO user_lookup (user_id, app_id, app_user_id, special_offers, ip_address) VALUES ('2426569', '57147', '4009116545', 1, 1854489853) ]