Search code examples
mysqlunix-timestamp

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


I was thinking at something like this:

`post_modified` int(11) NOT NULL DEFAULT UNIX_TIMESTAMP (NOW ()) ON UPDATE UNIX_TIMESTAMP (NOW ()),

But this code it is not working.


Solution

  • 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());