Last modified Timestamp in MySQL - mysql

The default value of the column should be the one at the time of insertion and later when updation or deletion is done it should change in my sql

You probably want to set these properties on the field:
DEFAULT CURRENT_TIMESTAMP
ON UPDATE 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.

How to create 2 auto-set timestamp columns in a MySQL table?

I want a MySQL table of mine to contain 2 timestamp columns, both set automatically without the client side help: one to be initialized once on insert:
`added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
and another initialized the same on insert and updated on every update:
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
But this doesn't work this way:
[Err] 1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Is there a known workaround?
There is not just a workaround, there is a solution: Upgrade to MySQL 5.6.5 or higher and this is supported.
See: MySQL 5.6.6 TIMESTAMP columns and DEFAULT values
{edit} Since upgrading is not an option, you can make the first column a normal timestamp column and create a trigger that sets one timestamp when you insert the record. Then you can create the other colum with the DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, so it gets a timestamp on insertion and on update.
CREATE TRIGGER task_creation_timestamp BEFORE INSERT ON tasks
FOR EACH ROW
SET NEW.created = NOW();
I've stolen this trigger from this answer.
There is no "solution" as the error suggest, you can ONLY HAVE ONE TIMESTAMP per table (On previous versions of 5.6.6 as GolezTrol Suggested)
To workaround this i suggest you make of the "timestamps" a datetime and set the default to NOW() or CURRENT_TIMESTAMP() or any other synonym for NOW()

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 set default date time as system date time in mysql

I am trying to set my columns default date time to system datetime.
It shows me an error
Invalid default value for
'InsertionDate'
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
datetime DEFAULT 'Now()' NULL after `PickPointLatLong`
The default value for a column in mysql cannot be the result of a function.
The one exception is the current_timestamp as astander points out.
Your statement should be
alter table `vts`.`tblpickpoint`
add column `InsertionDate` TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
Have a look at CURRENT_TIMESTAMP
If you want to init and update the value on every change, use this:
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
after `PickPointLatLong`
If you only want the creation time, use this:
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
TIMESTAMP DEFAULT CURRENT_TIMESTAMP
after `PickPointLatLong`

mysql timestamp column

Is it possible to define a timestamp column in a MySQL table that will automatically be updated every time a field in the same row is modified? Ideally this column should initially be set to the time a row was inserted.
Cheers,
Don
You can use the timestamp column as other posters mentioned. Here is the SQL you can use to add the column in:
ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
This adds a column called 'lastUpdated' with a default value of the current date/time. When that record is updated (lets say 5 minutes later) that timestamp will automatically update to the current time.
That is the default functionality of the timestamp column type. However, note that the format of this type is yyyymmddhhmmss (all digits, no colons or other separation).
EDIT: The above comment about the format is only true for versions of MySQL < 4.1... Later versions format it like a DateTime
This is what I have observed (MySql 5.7.11) -
The first TIMESTAMP column in the table gets current timestamp as the default value. So, if you do an INSERT or UPDATE without supplying a value, the column will get the current timestamp.
Any subsequent TIMESTAMP columns should have a default value explicitly defined. If you have two TIMESTAMP columns and if you don't specify a default value for the second column, you will get this error while trying to create the table -
ERROR 1067 (42000): Invalid default value for 'COLUMN_NAME'
A MySQL timestamp is set with creation or update time only if their default value is set as it. ALTER TABLE some_table ADD when TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
Otherwise it works just like a DateTime field, only that it's relative to 1970/01/01 UTC, so it's an absolute point in time not depending on a specific timezone as is DateTime.