How does Mysql set the field to automatically obtain the current time?
Application scenarios:
1. In a data table, to record when each piece of data was created, the data database automatically records the creation time without the need for the application to specifically record it;
2. In the database, when is each data record modified? The data database obtains the current time to automatically record the modification time without the need for the application to specifically record it;
Implementation Method:
1. Set the field type to TIMESTAMP.
2. Set the default value to CURRENT_TIMESTAMP.
Example application:
1. MySQL script implementation case
-- Add CreateTime to set the default time CURRENT_TIMESTAMP
Alter table 'table _ name'
Add column 'createtime' datetime null default CURRENT_TIMESTAMP comment' creation time ';
-- Modify the default time of CreateTime settings CURRENT_TIMESTAMP
Alter table 'table _ name'
Modify column 'createtime' datetime null default CURRENT_TIMESTAMP COMMENT 'creation time ';
-- Add UpdateTime to set the default time CURRENT_TIMESTAMP to set the 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 to set the default time CURRENT_TIMESTAMP and set the UPDATE Time To 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
Summary:
1. MySQL is automatically managed to maintain time consistency with the database;
2. Simple and efficient. MySQL is automatically completed without application development support;
View comments