on insert current_timestamp in mysql - mysql

I need to alter a column with timestamp datatype. The thing is when record is inserted the current time stamp should be inserted in that column. I know there is ON UPDATE CURRENT_TIMESTAMP but for insertion i can't find a way.

What you're looking for is the DEFAULT keyword.
ALTER TABLE yourTable MODIFY COLUMN yourColumn timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

What Rock 'Em wanted was to be told to use CURRENT_TIMESTAMP as the default for the column. Not on update. I use this all of the time. That way when a record gets inserted you know exactly when it happened. It is good practice in many tables to do this for audit tracking, etc.

Simply use DEFAULT clause, ignoring ON UPDATE clause, that's it should work for you!
ALTER TABLE yourTable MODIFY COLUMN yourColumn timestamp DEFAULT CURRENT_TIMESTAMP

Related

Explaining mysql timestamps of specific row

I want to add at row "jailtime" timestamp. That timestamp would be the timestamp of when "pjailed" row was updated.
I tried to do the fallowing:
https://dba.stackexchange.com/questions/45470/get-the-time-of-last-update-of-a-column
But instead of creating table, I wanted to alter existing table, so I went for
ALTER TABLE `users`
CHANGE `jailtime` `jailtime` TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP NULL
DEFAULT 'updated_at pJailed';
But it gives me error "Invalid value for pJailed"
If the value for pJailed for a row is updated that whole row receives an update. So it would suffice to use ON UPDATE CURRENT_TIMESTAMP (and DEFAULT CURRENT_TIMESTAMP) on jailtime.
You can alter the column jailtime to reflect this using:
ALTER TABLE your_table
MODIFY COLUMN jailtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Edit: OP mentioned in their comment that the attribute jailtime should only be updated when the attribute pJailed is updated. To accomplish this one should use a trigger as described here.

Can I amend the properties of a column in a table after the fact

I have a Timestamp columnn in a MySQL table. The default value of this column is CURRENT_TIMESTAMP, I want to make the ON UPDATE property of this column also equal to CURRENT_TIMESTAMP without having to drop and re-add the column, as this would cause me to lose all of the data.
Is this possible?
You ought to be able to do this successfully with an ALTER statement:
ALTER TABLE yourtable
MODIFY the_timestamp_column TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
It works correctly in my quick testing.

How to add "ON update current timestamp" to existing table column

I have a column, say ts_activity, with datatype timestamp in MYSQL and default value to current timestamp. I want to add on update apply current time stamp to this column value.
I am not able to get alter query to do this. While create new table and its column I can add that, but not able to modify existing column by adding on update apply current timestamp.
Can someone tell me exact query to handle this? Can we alter this after creating table?
Devart's suggestion will work, but I prefer to use MODIFY COLUMN instead of CHANGE COLUMN if I'm not renaming the column.
I also assume that your ts_activity column is not nullable since you have a default, so I am setting it to NOT NULL, but that's up to you.
This is the statement I would use:
ALTER TABLE your_table
MODIFY COLUMN ts_activity TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Try this query -
ALTER TABLE table_name
CHANGE COLUMN column_name column_name TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Note, in the table only one field can be with 'ON UPDATE CURRENT_TIMESTAMP' option.

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?
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
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Very good documentation here for time-stamp.
You can use a default value for that field and not include it in the insert or update query.

How do I alter a mysql table column defaults?

I have a table with a column of type timestamp which defaults current_timestamp and updates to current_timestamp on every update.
I want to remove the "on update" feature on this column. How do I write the alter statement?
I tried the following:
ALTER TABLE mytable alter column time set DEFAULT now();
but this didn't work.
Pete was almost correct but used the wrong syntax for 'change':
ALTER TABLE mytable CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Notice that you must repeat the column name. Also, make sure you are using backticks instead of single quotes to escape the column name time, which prevents it from being interpreted as the mysql column type of time.
By specifying the DEFAULT of CURRENT_TIMESTAMP, MySQL will no longer automatically update the column. From the MySQL Manual:
With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
You can't AFAIK use functions such as NOW() as a default.
Try
ALTER TABLE `mytable` CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
(Edited to add escaping and second use of field name)