Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update mysql table with same values and still get a timestamp update

So I have this stamp timestamp DEFAULT NOW() ON UPDATE NOW() row on my table, and I need it to update even when the update I'm executing is basically same data on all fields.

Is there any way of doing this within the declaration of the table, like some other option other than on update, or do I have to force a stamp = now() every time I update (and remove the on update of course since it will be useless).

I've seen this thread, but it only answers what is happening and why, not how to get around it other than forcing it indirectly

like image 832
Bimp Avatar asked Dec 28 '11 14:12

Bimp


People also ask

What does a timestamp do on UPDATE Current_timestamp data type?

With an ON UPDATE CURRENT_TIMESTAMP clause and a constant DEFAULT clause, the column is automatically updated to the current timestamp and has the given constant default value.

How UPDATE same column with different values in MySQL?

Specific columns can be modified using the SET clause by supplying new values for that column. The WHERE clause can be used to specify the conditions those identify which rows to update. Without using WHERE clause, all rows are updated. The ORDER BY clause is used to update the order that is already specified.

How do you change a timestamp in a table?

The default constraint is helpful to set the current timestamp value. During table creation itself, the default value can be set, or else by using ALTER command, we can set the default constraint. The other way is by using the 'UPDATE' command and which is seen in the above example.


2 Answers

As @veeTrain said, it'd be easy if you added it to your update statement. This is just another way of doing it, you can also use unix_timestamp().

UPDATEtable_nameSETlast_logged_in= unix_timestamp() WHEREid= '$user_id'

I know my response is late, but I ran into a similar issue and figured I'd share my solution for those encountering this thread in the future.

like image 59
Winter Avatar answered Sep 19 '22 19:09

Winter


You'd have to use a trigger to force it each time.

DELIMITER GO

CREATE TRIGGER `mydb`.`mytable_U` BEFORE UPDATE ON `mydb`.`mytable`  
FOR EACH ROW 
BEGIN  
    SET NEW.stamp = CURRENT_TIMESTAMP;   
END
GO

DELIMITER ;
like image 26
gbn Avatar answered Sep 19 '22 19:09

gbn