I use timestamp on MySQL 5.x (with PHP) to remember event times. During development I had to update the table with a query that changes something in all columns. The timestamp was then reset to current time.
How can I make timestamp change only on inserts and not on updates or replace?
Here's all you need to know. In short, though, I think this should do it:
ALTER TABLE `mytable`
CHANGE `mydatefield` `mydatefield`
TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Very good documentation here for time-stamp.
You can use a default value for that field and not include it in the insert or update query.
Related
In my Database i have a table with column
`LastUpdated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
i would like to Touch a record in that table so that LastUpdated column will be automatically updated but i don't want to change any value in that row.
Is that possible?
Thank you.
AFAIK, You don't have options to use touch to update mysql table records like you touch file in unix systems. You have to issue an update query to update the timestamp in the LastUpdated column .
UPDATE mytable SET LastUpdated=NOW() WHERE ...
I need to alter a column with timestamp datatype. The thing is when record is inserted the current time stamp should be inserted in that column. I know there is ON UPDATE CURRENT_TIMESTAMP but for insertion i can't find a way.
What you're looking for is the DEFAULT keyword.
ALTER TABLE yourTable MODIFY COLUMN yourColumn timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
What Rock 'Em wanted was to be told to use CURRENT_TIMESTAMP as the default for the column. Not on update. I use this all of the time. That way when a record gets inserted you know exactly when it happened. It is good practice in many tables to do this for audit tracking, etc.
Simply use DEFAULT clause, ignoring ON UPDATE clause, that's it should work for you!
ALTER TABLE yourTable MODIFY COLUMN yourColumn timestamp DEFAULT CURRENT_TIMESTAMP
Is ON UPDATE specific for TIMESTAMP cols?
When I use it on cols with other types it causes a syntax error.
Whilst I'm no Mysql expert, yes it looks like that particular syntax is TIMESTAMP specific.
From the documentation:
To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and
ON UPDATE CURRENT_TIMESTAMP clauses.
Then a little later:
Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is
specific to TIMESTAMP.
Ok, an old hack is no longer working. Currently using MySQL 5.5.11
In my table I have the below in the same order.
created TIMESTAMP NOT NULL DEFAULT 0000-00-00 00:00:00
updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
On INSERT everything works correctly and NOW() is inserted on both created and updated
Now with MySQL 5.5.11 when I UPDATE, updated works correctly but I loose created (reverts back to 0000-00-00 00:00:00)
How can I overcome this limitation with MySQL's lack of multiple TIMESTAMP support?
Yes, it would work on earlier version, but not newer version.
I believe you can solve this as follows:
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
And then create an trigger BEFORE UPDATE on [updated] column.
SET new.updated = now();
Or vice-versa.
As far as I understand it, TIMESTAMP columns get automatically updated on every UPDATE operation to a particular row. It looks like you need column created to be DATETIME instead of timestamp.
I have a table with a column of type timestamp which defaults current_timestamp and updates to current_timestamp on every update.
I want to remove the "on update" feature on this column. How do I write the alter statement?
I tried the following:
ALTER TABLE mytable alter column time set DEFAULT now();
but this didn't work.
Pete was almost correct but used the wrong syntax for 'change':
ALTER TABLE mytable CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Notice that you must repeat the column name. Also, make sure you are using backticks instead of single quotes to escape the column name time, which prevents it from being interpreted as the mysql column type of time.
By specifying the DEFAULT of CURRENT_TIMESTAMP, MySQL will no longer automatically update the column. From the MySQL Manual:
With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
You can't AFAIK use functions such as NOW() as a default.
Try
ALTER TABLE `mytable` CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
(Edited to add escaping and second use of field name)