views:

823

answers:

4

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 ?

A: 

You can use a default value for that field and not include it in the insert or update query.

cherouvim
+8  A: 

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
Paolo Bergantino
I may be missing something but my table is (and was) defined this way but still the dates changed.From mysql explain:'pur_time', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', ''
Nir
Did you run the query on the table?
Paolo Bergantino
The "on update" clause doesn't show up on EXPLAIN tblName. Use e.g. SHOW CREATE TABLE tblName instead
VolkerK
VolkerK Thank you !. I run the SHOW CREATE TABLE and got `pur_time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,This is what I needed. Thanks
Nir
A: 

ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Very good documentation here http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

I may be missing something but my table is (and was) defined this way but still the dates changed. From mysql explain: 'pur_time', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', ''
Nir
A: 

I suggest using DATETIME for that column. It won't change when the record is updated.

For me, I use TIMESTAMP for tracking when the record was last touched, although in general I avoid using it.

Klathzazt