MySQL ERROR 1265: Data truncated for column - mysql

I can't figure out why I'm getting this message. I'm using MySQL Workbench and am editing the values in an ENUM field that connects to a dropdown choice in my app.
Everything seems to be fine. I've searched on this error and all I find refers to datatype mismatches but, in this instance, that's not possible with ENUM when feeding it an array of string values.
Here's the SQL
Executing:
ALTER TABLE `mydbase`.`average_monthly_expenses`
CHANGE COLUMN `expense_category` `expense_category` ENUM('Home', 'Healthcare', 'Child care', 'Groceries and supplies', 'Eating out', 'Utilities', 'Telecomms', 'Laundry and cleaning', 'Clothes', 'Education', 'Entertainment gifts vacation', 'Auto and transportation', 'Insurance', 'Savings and investments', 'Charitable contributions', 'Itemized monthly payments') NULL DEFAULT NULL ;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1265: Data truncated for column 'expense_category' at row 1
SQL Statement:
ALTER TABLE `mydbase`.`average_monthly_expenses`
CHANGE COLUMN `expense_category` `expense_category` ENUM('Home', 'Healthcare', 'Child care', 'Groceries and supplies', 'Eating out', 'Utilities', 'Telecomms', 'Laundry and cleaning', 'Clothes', 'Education', 'Entertainment gifts vacation', 'Auto and transportation', 'Insurance', 'Savings and investments', 'Charitable contributions', 'Itemized monthly payments') NULL DEFAULT NULL
Any suggestions are very welcome

The query itself is correct.
modelling fiddle
Execute
SELECT DISTINCT expense_category, HEX(expense_category)
FROM mydbase.average_monthly_expenses
and check the output for the values which are not listed in the column definition.
There may be typos, leading/trailing spaces or another non-printed symbols, double spaces in the middle of the value, or there may be some collation problems.
UPDATE
My current field definition I'm trying to change to the above is ENUM('Home', 'Living', 'Telecommunications', 'Transportation', 'Other'). When I run your suggested SQL I just get Housing and Other listed.
These values are absent in new column definition - so server cannot convert them and truncates the values.
Recommendations: alter column definition, add new values to ENUM values list but do not remove old ones; update table and replace old values with new ones; alter column definition and remove old values from the list.

#Akina
So I figured out why I was blocked from editing the values. It would not let me make any edits that changed either value "Housing" or "Other". I did as you suggested, adding my new values to the existing ones, no problem, that worked fine. I couldn't however delete "Housing" or "Other", but the other prior values deleted fine. For the moment, I kept both, using "Housing" instead of "Home" and leaving "Other" at the end.
But I wanted to know however why those two values were protected, and then it dawned on me, there were existing records using those values. I manually changed all instances using "Other" to "Telecomms" and then I could remove "Other" from the ENUM values. All good now.

Related

"#1265 - Data truncated" error changing column (set) values

This error occurs several times, while I attent to change values in a "set" column, in structure mode of Phpmyadmin.
I get an "#1265 - Data truncated" error, even if the new length of a value is exactly the same than the old one (for example if I change "fra" with "frn", or "gre" with "grc").
I.g. I have a "lang" column (in my table quotations), whose values are: 'it','lat','fr','en','deu','esp','gr'.
I cannot change not only 'fr' with 'frn' or 'gr' with 'grc', but neither 'lat' with 'ltn'. I don't understand why.
I have PHP version: 7.3.7, MariaDB: 10.3.16-MariaDB, and Phpmyadmin: 4.9.4
Thanks
I found the solution (quite trivial, indeed): the problem was that some records were already with the value I would change.
So, instead than replace 'gr' with 'grc', I added 'grc' in table structure. Then I searched all my items with 'gr' value and changed them with 'grc' value.
And finally I deleted 'gr' from table structure.

Unable to change the value of enum field in MySQL when manually typing a query

I have an enum column in a MySQL table called site_status:
From my understanding, updating the value of such a field is exactly the same process as changing value of a varchar or int or any other field.
The only "limitation" is that as a value I obviously have to set one from predefined values - as this is the nature of enum types. Right?
So I want to change site_status from LIVE to FREE:
I type:
update db_name.db_table set site_status = 'FREE' where site_id = 501;
Pretty easy query. Unfortunately does nothing. It also doesn't produce any error. Simply:
0 rows affected.
What am I doing wrong? When I manually click via GUI in Sequel Pro, the value is changed to FREE with no issues. I see a dropdown from which I pick another option.

SQL NULL insertion not working

I'm having some odd SQL problems when inserting new rows into a table. I have set some columns to NULL, as I have with another table in my database. Obviously when no data is passed through on insertion it should enter NULL into the record, however currently it is not.
I have checked all settings in comparison with my other table (which is inserting records as NULL correctly) but can't find the issue. The columns appear as below, in both tables.
`statement_1` varchar(255) DEFAULT NULL,
No data is being pasted through (so not a blank space issue). Can anyone suggest why one table is doing as expected but the other is not?
Using below as the insert statement
$statement_a = "INSERT INTO statements (ucsid, statement_1, statement_2, statement_3, statement_4, statement_5, statement_6, statement_7, statement_8, statement_9, statement_10) VALUES (:ucsid, :statement_1, :statement_2, :statement_3, :statement_4, :statement_5, :statement_6, :statement_7, :statement_8, :statement_9, :statement_10)";
$q_a = $this->db_connection->prepare($statement_a);
$q_a->execute(array(':ucsid'=>$ucsid,
':statement_1'=>$statement_1,
':statement_2'=>$statement_2,
':statement_3'=>$statement_3,
':statement_4'=>$statement_4,
':statement_5'=>$statement_5,
':statement_6'=>$statement_6,
':statement_7'=>$statement_7,
':statement_8'=>$statement_8,
':statement_9'=>$statement_9,
':statement_10'=>$statement_10));
I can not add comments as I am new:
Try a simple INSERT statement using NOT phpmyadmin. Try
http://www.heidisql.com/ OR https://www.mysql.com/products/workbench/
INSERT INTO statements (ucsid) VALUES (123)
INSERT INTO statements (ucsid, statement_1) VALUES (123, NULL)
In both cases the statement_1 should be NULL. Which in your case most likely is not. However that would tell the problem lies in the database table and NOT with php or the php execute method you are using.
Also is the statement_1 field defined as NOT NULL and the default set as NULL? which can not happen.
Try recreating a new database and a new table with no records and than try inserting NULL as values as a test.
Also can you post the SQL of your database and table with Character Set and Collation
I've fixed the issue by ensuring that NULL is passed through the functions if nothing has been inserted by using the following code
if($_POST['statement_1'] == '') { $statement_1 = NULL; } else { $statement_1 = $_POST['statement_1']; }
Here the value passed by the varriable $statement_1 will be ""
Try this query SELECT * FROM my_table WHERE statement_1 ="".You will get rows.
Which means you are assigning some values to $statement_1 else it should be null.
Check your code. Hope this helps

what does #1136 - Column count doesn't match value count at row 1 mean

I am brand new to MySQL and to all database langues as a whole so excuse me if this is just a dumb error. i am writing my first database and started by making the coulombs of CrayFish, Hooks, LiveBait, Spinners, and Worms. in it i put this command.
INSERT INTO gear (CrayFish, Hooks, LiveBait, Spinners, Worms) values ('big', 'small'), ('size5', 'size4', 'size3', 'size2', 'size1'), ('nightCrawlers', 'frog', 'liveMinos', 'bloodWorms'),('perch', 'mino', 'sunfish'), ('pink', 'orange', 'green');
I am getting the error: #1136 - Column count doesn't match value count at row 1. I have looked online but none of the answers I saw fix my problem. Thank you for your answer!
It means exactly what it says. Here are your columns:
(CrayFish, Hooks, LiveBait, Spinners, Worms)
And here are your values:
('big', 'small')
5 is not equal to 2. The counts need to match. If you want to leave the rest null, explicitly define that:
('big', 'small', NULL, NULL, NULL)
I suspect the rest of that query is wrong, too. You're probably going to have an error on that next comma.
Upon further inspection, it looks like you're trying to insert all of the rows for a given column one column at a time? Tables don't work like that. Though from the values and the column names it's difficult to discern specifically what you're trying to do.
Either way, an INSERT statement inserts a single row of values at one time. Define that row of values, insert it, repeat for other rows.
the syntax an insert statement is:
INSERT INTO table_name (column_name_1, column_name_2, ..., column_name_n)VALUES (value_1,value_2,...,value_n);
when you insert a row into a table you need to specify the value that will be inserted into each column for the row. You can also only insert one row at a time.
You need to try something like
INSERT INTO gear (CrayFish, Hooks, LiveBait, Spinners, Worms) values ('big', 'size5','nightCrawlers', 'perch','pink');
to insert one row into the gear table

Update MySQL without specifying column names

I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.
If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.
You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html
No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.
Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html
I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.