Application Scenarios:
1, in the data table, to record when each piece of data was created, do not need the application to deliberately record, and the data database to obtain the current time automatically record creation time;
2, in the database, to record when each piece of data is modified, do not need the application to deliberately record, and the data database to obtain the current time automatically recorded modification time;
Implementation method:
1. Set the field type to TIMESTAMP
2. Set the default value to current_timestamp
Examples of applications:
1. MySQL Script Implementation use case
-- add Createtime Set default time current_timestamp
ALTER TABLE ' table_name '
ADD COLUMN ' createtime ' datetime NULL DEFAULT current_timestamp COMMENT ' creation time ';
--Modify Createtime Set default time current_timestamp
ALTER TABLE ' table_name '
MODIFY COLUMN ' createtime ' datetime NULL DEFAULT current_timestamp COMMENT ' creation time ' ;
-- add UpdateTime Set default time current_timestamp set update time to on update current_timestamp
ALTER TABLE ' table_name '
ADD COLUMN ' updatetime ' timestamp NULL DEFAULT current_timestamp on UPDATE Current_timestamp COMMENT ' creation time ';
--Modify UpdateTime Set default time current_timestamp set update time is on update current_timestamp
ALTER TABLE ' table_name '
MODIFY COLUMN ' updatetime ' timestamp NULL DEFAULT current_timestamp on UPDATE Current_ TIMESTAMP COMMENT ' creation time ';
2. mysql Tool settings
Summarize:
1, MySQL automatic management, maintain and database time consistency;
2, simple and efficient, do not need application development support, MySQL automatic completion;
How Mysql sets the field to get the current time automatically