9

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.

2

2 Answers 2

6

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

6

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 !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.