How to set default value of a mysql column as unix_timestamp? How to set default value of a mysql column as unix_timestamp? mysql mysql

How to set default value of a mysql column as unix_timestamp?


Note : I have no idea about the performance of MYSQL TRIGGER

Please go through these links

  1. Identify some of the drawback of implementing sql server triggers

  2. Using Triggers

You can create triggers for this.

for insertion

CREATE TRIGGER {trigger_name} BEFORE INSERT ON {table_name} FOR EACH ROW SET new.{field_name} = UNIX_TIMESTAMP(NOW());

for update

CREATE TRIGGER {trigger_name} BEFORE UPDATE ON {table_name} FOR EACH ROW SET new.{field_name} = UNIX_TIMESTAMP(NOW());


Nope you cant use it as you showed.

From Data Type doc on MySql:

"The default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column."

Look at below answer for workaround:

Is it possible to create a column with a UNIX_TIMESTAMP default in MySQL?

Hope this will help !