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.
Related
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to add 'updated_at' and 'created_at' columns to some already existing table in MySQL database. I've added those colums using MySQL Workbench, but what query should I use to make them work properly? Thanks in advance ;)
According to the reference manual* you can use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in column definitions:
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP,
the column has the current timestamp for its default value and is
automatically updated to the current timestamp.
whereas:
With a DEFAULT clause but no ON UPDATE CURRENT_TIMESTAMP clause, the
column has the given default value and is not automatically updated to
the current timestamp.
So, for example, you could use:
CREATE TABLE t1 (
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
To add the columns to an already existing table you can use:
ALTER TABLE t1
ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Note: Link provided refers to MySQL 8.0. The syntax is the same for previous versions as well. There is some difference though for versions prior to 5.6.5. Just quoting from the manual again:
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table.
Try to use a Trigger. As example, when creating a new entry, the trigger will be activated and will execute, like an event in c# or an Action listener in Java!
With them, you can update that new entry with a creation date, or an edited date when you manipulated an entry. See this Documentation on how to use MySql Triggers on w3resource.
This question already has answers here:
Invalid default value for 'dateAdded'
(8 answers)
Closed 6 years ago.
I am having problem while altering a table. I need a column with data type DATETIME to take default value as current date/time and on update also it should automatically update it's value to current date/time. I am writing the following SQL
ALTER TABLE `groups`
CHANGE COLUMN `modified` `modified` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
I am getting the following error message.
Error Code: 1067. Invalid default value for 'modified'
The MySQL version I'm using is 5.5.49 on a Ubuntu 14.04.1 system.
Please let me know how this can be fixed.
Prior to MySQL 5.6.5, you can only use the CURRENT_TIMESTAMP default value for columns of type TIMESTAMP.
See https://stackoverflow.com/a/9005872/1293303
Most probably this is because you already have another column with CURRENT_TIMESTAMP as default.
In MySQL versions prior to 5.6 this is a problem:
Why there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT clause?
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')