Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL timestamp only on create

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?

like image 928
Nir Avatar asked May 03 '09 16:05

Nir


People also ask

What creates TIMESTAMP?

A timestamp is a sequence of characters or encoded information identifying when a certain event occurred, usually giving date and time of day, sometimes accurate to a small fraction of a second. Timestamps do not have to be based on some absolute notion of time, however.

How do I add a TIMESTAMP to a MySQL table?

You can use the timestamp column as other posters mentioned. Here is the SQL you can use to add the column in: ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ; This adds a column called 'lastUpdated' with a default value of the current date/time.

How is TIMESTAMP stored in MySQL?

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME .) By default, the current time zone for each connection is the server's time.


1 Answers

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 
like image 78
Paolo Bergantino Avatar answered Sep 30 '22 05:09

Paolo Bergantino