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.
Related
This question already has answers here:
Need a datetime column in SQL Server that automatically updates when the record is modified
(5 answers)
Closed 1 year ago.
What is the equivalent of the following in MS SQL server syntax?
CREATE TABLE `new_table` (
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
I have tried:
update myTable set updated = CURRENT_TIMESTAMP;
Any update on table with timestamp data type will automatically update it's row timestamp value. If anything is missing in your question, please add more data.
I'm not entirely sure how to explain what's happening but basically I'm trying to insert values into my table using a subquery and it's telling me that I'm getting an error because a field that is not involved in the query at all does not have a default value.
INSERT INTO customerPayment (customerOrderId)
SELECT ID FROM customerOrder
WHERE customerOrder.orderStateId = (
SELECT ID
FROM orderState
WHERE orderState.state = "Payment Recieved"
);
ERROR 1364 (HY000): Field 'total' doesn't have a default value
And then when I go into the table itself to try and set a default value for total, it then tells me I have an invalid default value for another unrelated field.
ALTER TABLE customerPayment ALTER total SET DEFAULT 0.0;
ERROR 1067 (42000): Invalid default value for 'paymentDate'
It may be relevant to note that 'paymentDate' currently has a default value of curdate().
From what I can see, the total column was created with NOT NULL condition but wasn't assigned with any default value. Something like this example:
total DECIMAL(4,4) NOT NULL,
And it seems CURDATE() or CURRENT_DATE() can't be assigned as default value from my testing. Instead, the column datatype should be TIMESTAMP or DATETIME then can only assign with default value of CURRENT_TIMESTAMP() or NOW(). So, maybe the first step is to change the paymentDate datatype and default value like:
ALTER TABLE customerPayment MODIFY COLUMN `paymentDate` TIMESTAMP NOT NULL DEFAULT NOW();
Then you can proceed with modifying the total column like:
ALTER TABLE customerPayment MODIFY COLUMN total DECIMAL(4,4) NOT NULL DEFAULT 0.0;
Then probably you can do your INSERT after that.
Here's a fiddle: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4af3d11f90916469d8fbe61011b3fcab
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.
This question already has an answer here:
Create table fail in mysql when using CURDATE() as default
(1 answer)
Closed 7 years ago.
Trying to insert new columns into my blog posts table as part of my sites update.
Here is the mysql code I am using:
ALTER TABLE blogposts
ADD (
postType varchar(20),
postDate datetime NOT NULL DEFAULT CURDATE(),
postTime datetime NOT NULL DEFAULT CURTIME()
);
It throws this error though when executed:
#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 'CURDATE(),
postTime datetime NOT NULL DEFAULT CURTIME()
)' at line 4
I assume that curdate() is the issue, but do not understand why since it is a valid mysql function.
This is not a duplicate of this as was proposed., the solution in that post did not fix my problem and instead threw this error:
#1067 - Invalid default value for 'postDate'
When using code:
ALTER TABLE blogposts
ADD (
postType varchar(20),
postDate datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
postTime datetime NOT NULL DEFAULT CURTIME()
);
So for reference, this is not a duplicate question it seems.
Default values for DATETIME columns are not available before MySQL Server 5.6.5.
In 5.6.5 and later, the value you specify as default must be CURRENT_TIMESTAMP or an equivalent expression returning a datetime value.
https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
Because I want them separated out by default for easier calling from the backend.
That seems like a little bit of a frivolous motivation, when you can SELECT TIME(c1) AS teh_time, DATE(c1) AS teh_date if needed, and almost any other operation you wanted to do, like datetime math, time zone conversions or DATE_FORMAT() would require them to be recombined.
This question already has answers here:
Insert date and time into Mysql
(6 answers)
Closed 7 years ago.
I have a mysql table that I created like this:
CREATE TABLE `Table` (
`Table_id` int(11) NOT NULL AUTO_INCREMENT,
`Table_date` datetime NOT NULL,
`Table_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`Played_id`))
ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8
Now I try to call the INSERT query:
INSERT INTO Table (`Table_date`) VALUES ('04/12/2015 16:50:35')
and it doesn't work. I get the confirmation that one row was inserted, but all I see is value 0000-00-00 00:00:00 as a Table_date. The timestamp in Table_modified is fine, it's current value, but only zeros in Table_date really bugs me. I know it's impossible to store two timestamps in database, that's why I've decided to switch and use the type Datetime as for Table_date, so far with no success. How can I fix it and fill that column with the time provided?
You have the format for the date part wrong. Try this:
INSERT INTO Table (`Table_date`) VALUES ('2015-04-12 16:50:35')