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
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.
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.
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.
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().
UPDATE
table_nameSET
last_logged_in= unix_timestamp() WHERE
id= '$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.
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 ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With