Home > Database > Mysql Tutorial > Detailed example of how mysql automatically obtains the time and date

Detailed example of how mysql automatically obtains the time and date

黄舟
Release: 2017-09-18 10:41:39
Original
1791 people have browsed it

Implementation method:

1. Set the field type to TIMESTAMP

2. Set the default value to CURRENT_TIMESTAMP

##Example application:

1. MySQL script implementation use case

--Add CreateTime to set the default time CURRENT_TIMESTAMP

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


--Modify CreateTime to set the default time CURRENT_TIMESTAMP
##

ALTER TABLE `table_name`MODIFY COLUMN  `CreateTime` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' ;
Copy after login


--Add UpdateTime to set the default time CURRENT_TIMESTAMP 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 '创建时间' ;
Copy after login


##--Modify UpdateTime to set the default time

CURRENT_TIMESTAMP 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 '创建时间' ;
Copy after login


##2. MySQL tool settings

Summarize:

1. MySQL automatically manages and maintains time consistency with the database;

2. Simple and efficient, no application development support is required. MySQL autocomplete;

The above is the detailed content of Detailed example of how mysql automatically obtains the time and date. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template