MySQL: is there a way to automatically set the datetime field to write the creation timestamp?

I know that there is a TIMESTAMP data type that automatically updates the timestamp value when the record is updated, and I already have such a column.

Also, I would like to have a column that automatically populates before NOW () (or CURRENT_TIMESTAMP) and never changes, but MySQL DEFAULT does not seem to support function calls.

Please post only MySQL answers. I know how to do this at the application level.

EDIT: If this is not possible, I would appreciate it.

EDIT2: MySQL Version 5.0.32

+4
source share
2 answers

Use the trigger to set the default value.

DELIMITER | CREATE TRIGGER trigger_name BEFORE INSERT ON tbl_name FOR EACH ROW BEGIN SET NEW.colname = NOW(); END; 

|

Try this including a delimiter.

+9
source

I would like to have a column that automatically fills NOW () (or CURRENT_TIMESTAMP) and never changes

Saying you need to populate NOW (), it looks like you mean the initial INSERT . If so, can you just use it? Sort of

 INSERT INTO table (field1,field2,my_datetime) VALUES (1,'a',NOW()) 
+3
source

All Articles