Nir
Nir

Reputation: 25369

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?

Upvotes: 41

Views: 53104

Answers (3)

Paolo Bergantino
Paolo Bergantino

Reputation: 488394

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

Upvotes: 79

user99974
user99974

Reputation: 203

ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Very good documentation here for time-stamp.

Upvotes: 1

cherouvim
cherouvim

Reputation: 31903

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

Upvotes: 0

Related Questions