I know there is TIMESTAMP data type that automatically updates to timestamp value when a record is updated, and I already have such column.
Besides that I'd like to have a column that automatically populates to NOW() (or CURRENT_TIMESTAMP) and never changes, but MySQL DEFAULT doesn't appear to support function calls.
Please post only pure MySQL answers. I know how to do it at application level.
EDIT: If there开发者_Python百科's no such feature - I'd appreciate to hear that.
EDIT2: MySQL version is 5.0.32
Use a trigger to set the default.
DELIMITER | CREATE TRIGGER trigger_name BEFORE INSERT ON tbl_name FOR EACH ROW BEGIN SET NEW.colname = NOW(); END;|
Try this one, including the delimiter.
I'd like to have a column that automatically populates to NOW() (or CURRENT_TIMESTAMP) and never changes
By saying you'd like it to populate to NOW() it sounds like you're referring to the initial INSERT. If that's the case, can you just use that? Something like
INSERT INTO table (field1,field2,my_datetime) VALUES (1,'a',NOW())
加载中,请稍侯......
精彩评论