MYSQL issue to change some date values in a column - mysql

I'm facing a problem with a shitty database in production done by someone before me.
I have a table called "projets" with some column with a date. Instead of having a column with NULL value accepted, it was created with no NULL value authorized. So instead of a NULL, all entry are 0000-00-00.
So i've change a bit the column to insert NULL value inside like this
ALTER TABLE `projets`
ALTER `date_avant_projet` DROP DEFAULT;
ALTER TABLE `projets`
CHANGE COLUMN `date_avant_projet` `date_avant_projet` DATE NULL AFTER `procede`;
So now the column can normally accept NULL value. So i try to do this to change all 0000-00-00 to NULL
update projets set date_avant_projet = NULL where date_avant_projet = '0000-00-00'
But like that i've got an SQL error 1292 : Incorrect date value: '0000-00-00' for column 'date_avant_projet' at row 1.
It's very annoying for me because i have to also create a new column with a foreign key but when i try this
ALTER TABLE `projets`
ADD COLUMN `id_dernier_changement_etat` INT NOT NULL AFTER `etat`,
ADD CONSTRAINT `FK_projets_id_changement_etat` FOREIGN KEY (`id_dernier_changement_etat`) REFERENCES `changement_etat_projet` (`id`);
I've got EXACTLY the same error message : SQL error 1292 : Incorrect date value: '0000-00-00' for column 'date_avant_projet' at row 1
If someone understand better than me, it will be very helpful!
Thanks in advance for your future answer.

Related

How do I fix this MYSQL//Phpmyadmin error? [duplicate]

This question already has answers here:
#1292 - Incorrect date value: '0000-00-00' [duplicate]
(3 answers)
Closed 1 year ago.
So I tried to do a ALTER TABLE command to my table to add a date column that stores what time a post was made. Whenever I enter the SQL code, it pops up this error in PhpMyAdmin. I'm a beginner and I would really like if someone could help me.
Original code:
ALTER TABLE posts
ADD date datetime not null;
Error that pops up:
#1292 - Incorrect date value: '0000-00-00' for column 'website' . 'posts' . 'date' at row 1
Give a default value
ALTER TABLE posts ADD `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
Or if default value is not desired, add the column allowing NULL, update with appropriate values, and change the column to NOT NULL
ALTER TABLE posts ADD `date` datetime
;
UPDATE posts
SET `date` = NOW() -- or any suitable values
;
ALTER TABLE posts CHANGE `date` `date` datetime NOT NULL
;
You are adding a column, that can't be null. So what value do the existing rows get?
You need to either specify a default value, or allow null until its populated somehow.

How to properly alter table default value

I can't figure out why my alter table query throws an error. Currently, this is a (DATETIME) column with default value NULL.
My wish is to alter it so datetime value gets automatically populated when I update a row. I'm trying to write an alter statement, but I can't figure out why mine is throwing the error.
My alter statement
ALTER TABLE `mydb`.`orders` CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}';
And this is the error that I'm getting
16:28:34 ALTER TABLE `mydb`.`orders` CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}' Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}'' at line 1 0.00041 sec
I'm using MySQL version 5.7
The CHANGE COLUMN alteration is used when you might want to change the name of the column, and requires you to provide the new name after the old name. If you're not renaming the column, you have to provide the name twice. Your command tries to rename the date_u column to DATETIME, and it's missing the datatype before the NULL keyword.
Use MODIFY COLUMN instead. It's the same, but doesn't allow renaming, so doesn't require you to give the column name twice.
ALTER TABLE `mydb`.`orders` MODIFY COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
I'm also not sure what you intended with '{}' at the end, but I don't think it's valid syntax, either, so I've removed it.
You're missing to name the new column.
change this:
ALTER TABLE `mydb`.`orders`
CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}';
into this:
ALTER TABLE `mydb`.`orders`
CHANGE COLUMN `date_u` `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
Notice the column name is typed twic, because you want the column name to stay the same, formerly here's the change column syntax:
ALTER TABLE `table_name`
CHANGE COLUMN `column_name` `column_new_name` (...);
Or, you can just you modify column syntax:
ALTER TABLE `mydb`.`orders` MODIFY COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
Ps: I don't get what you mean by '{}' so I removed it because I think it's not a valid syntax.
Hope I pushed you further.

Failed to set default null value for column

I already have a table, simplified example:
CREATE TABLE t (c INT NOT NULL);
And I need to change column default value to NULL, so I tried:
ALTER TABLE t ALTER COLUMN c SET DEFAULT NULL;
but I got the error "Error Code: 1067. Invalid default value for 'c'".
It looks really strange, because query conforms with official docs.
I even tried to:
ALTER TABLE t ALTER COLUMN c DROP DEFAULT;
and after it to make a 'SET DEFAULT NULL' query, but the same error occurred.
It's interesting, that query like:
ALTER TABLE t ALTER COLUMN c SET DEFAULT 1;
executed without errors.
I know, that it is possible to change column default value to NULL in my case using:
ALTER TABLE t MODIFY COLUMN c INT NULL;
but this query is really slow on big tables (it is much slower, than queries like 'SET DEFAULT 1')
So, how to just change default value to NULL?
I mean, without any overhead caused by 'MODIFY COLUMN' command.
Details: MySQL x64 version 5.7.10, Win8. Tested using MySQL Workbench.
By creating column as NOT NULL you have created a CONSTRAINT - declaring that values entered into that column may never be NULL.
A default value of NULL (set to null is value not present during INSERT) would create invalid data.
As sadly nullability constraint is part of the datatype in mysql the only way to make the column nullable will be
ALTER TABLE t MODIFY COLUMN c INT NULL;

Add the NOT NULL constraint to a column

I'm using PHPMyAdmin and I try to add the NOT NULL constraint to a column of my table.
PHPMyAdmin accepts my following query :
ALTER TABLE `wall` MODIFY `token_message` varchar(40) NOT NULL;
But I can still insert empty strings (=NULL), I don't understand why.
PS : If you're going to give me some other queries to add this constraint, note I've have tried these 3 which don't work in my PHPMyAdmin (kind of error : #1064 - You have an error in your SQL syntax; check the manual) :
ALTER TABLE `wall` ALTER COLUMN `token_message` SET NOT NULL;
ALTER TABLE `wall` ALTER COLUMN `token_message` varchar(40) NOT NULL;
ALTER TABLE `wall` MODIFY `token_message` CONSTRAINTS token_message_not_null NOT NULL;
You wrote, "I can still insert empty strings (=NULL)," which sounds like a misunderstanding. In SQL, an empty string does not evaluate to NULL, or vice versa. Try inserting an empty string and doing SELECT from wall where token_message is NULL. You should get zero rows back. Then try doing an insert where you specify NULL (unquoted) as the value for your column, and you should get the expected error message.
If those tests work as expected, then everything is fine, and your problem is actually that you want to prevent blank strings from being inserted. Check out this question for suggestions, or just check for blank strings during validation, before the query.
MySQL's column alter syntax requires you to completely re-specify the column. You can't just change one attribute of a column, you have to re-define it completely:
ALTER TABLE wall MODIFY token_message varchar(40) NOT NULL default ''
The only 'SET' version allowed is to change the default value.
ref: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
I think this is a matter of scrubbing your inputs. As octern mentioned, an empty string ('') is not a NULL value in sql. The best way to handle this is to only allow updates through a store procedure which strips out empty strings, even space characters:
CREATE PROC InsertIntoMyDb (#MyVarChar VARCHAR(2000)) AS
SET #MyVarChar = NULLIF(RTRIM(LTRIM(#MyVarChar)), '')
INSERT INTO [TBL] (MyVarChar)
VALUES #MyVarChar
This will truncate any number of spaces to an empty string, turn an empty string into a NULL, and then it will not allow the NULL value to be inserted based on the constraint you already have in place.
Try to use this query
Alter table table_name
change column_name column_name datatype(length) definition
ie,
Alter table wall
change tocken_message tocken_message varchar(40) NOT NULL DEFAULT

mysql - "column cannot be null"

as you see in the title, even if i removed "not null" feature from the related field, it still doesn't let me to insert null value for that field although the field is nullable!
Any help would be appreciated.
EDITED
Create:
CREATE TABLE `review` (
..
`RATING` int(11) DEFAULT NULL,
..
(`CATALOG_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=31625 DEFAULT CHARSET=latin5 ROW_FORMAT=DYNAMIC
Query:
INSERT INTO review (RATING,..) VALUES (null,..);
Error message:
Error: Column 'RATING' cannot be null
SQLState: 23000
ErrorCode: 1048
I also try to insert without RATING in the insert query, even if it is default null and nullable field, it gives the same error message and never inserts the field.
Bohemian, thanks for your attention. You are right, I figured out that there is a trigger for insert action which effects the related field. I disabled the trigger and the error is fixed. Thanks.
First of all look your "datetime / created_at / updated_at" field, do not assign the default value, select current time. after that you can can update the fields name