I need to add current timestamp as a default value in a varchar column in MySQL 5.7
Here is table query I am trying
CREATE TABLE foo (
creation_time VARCHAR(100) DEFAULT CURRENT_TIMESTAMP, col1 VARCHAR(100)
)
I need to add current timestamp as a default value in a varchar column in MySQL 5.7
Here is table query I am trying
CREATE TABLE foo (
creation_time VARCHAR(100) DEFAULT CURRENT_TIMESTAMP, col1 VARCHAR(100)
)
Supposing the existing time has a varchar type like below:
If it's possible to change the date type, we can use:
Note: The
alter tablebackground process includes creating a new table, then populating it based on data from the original table, and finally destroying the original table. So it should be done at an off-business time.However, if the data type cannot be changed. Then we will have to use a trigger to do the timestamp initialization job.
Now the
inserttrigger will automatically set the value for your varchar column to be the current datetime every time a new row is inserted.