When you create a database table, how to set the default value of time in mysql

Scenario: 

1, in the data table to record each piece of data is created when, does not require the application to specifically record, acquired by the data in the database is created automatically records the current time period;

2, in the database, each of the data to be recorded is modified when the application does not require specially to recording, and the current time acquired by the data automatically records database modification time; 

Method to realize: 

1, the field type is set to TIMESTAMP 

2, the default value is set CURRENT_TIMESTAMP 

For example application: 

1, MySQL script to achieve the use case

- Add CreateTime set the default time CURRENT_TIMESTAMP 

ALTER TABLE `table_name`ADD COLUMN  `CreateTime` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' ;

 

- Modify the default settings CreateTime time the ALTER TABLE `table_name`MODIFY the COLUMN CURRENT_TIMESTAMP` CreateTime` datetime  NULL  the DEFAULT  CURRENT_TIMESTAMP the COMMENT 'Creation Time';

 

- Add UpdateTime set the default time settings update CURRENT_TIMESTAMP CURRENT_TIMESTAMP time of the ALTER TABLE `UPDATE ON table_name`ADD the COLUMN` UpdateTime` timestamp  NULL  the DEFAULT  CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP the COMMENT 'Creation Time'; 

- Modify UpdateTime set the default time settings update CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP time 

ALTER TABLE `table_name`MODIFY COLUMN `UpdateTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间' ;

2, MySQL tool settings

 

 

to sum up: 

1, MySQL automatically manage and maintain databases and time consistency;

2, simple and efficient, does not require application development support, MySQL automatically;

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11617365.html