{"id":239294,"date":"2022-04-16T08:01:14","date_gmt":"2022-04-16T00:01:14","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=239294"},"modified":"2022-03-30T10:02:30","modified_gmt":"2022-03-30T02:02:30","slug":"mysql-current-timestamp","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-current-timestamp.html","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u4f7f\u7528MySQL\u4e2dCURRENT_TIMESTAMP\u65f6\u95f4\u6233"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8eMySQL\u4e2dCURRENT_TIMESTAMP\u65f6\u95f4\u6233\u7684\u76f8\u5173\u8d44\u6599,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6,\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u524d\u8a00<\/strong><\/div>\n

\u6700\u8fd1\u5728\u9879\u76ee\u4e2d\u53d1\u73b0\u4e00\u4e2a\u5c0f\u95ee\u9898\uff0c\u6570\u636e\u88ab\u66f4\u6539\u65f6\uff0c\u63d2\u5165\u8bb0\u5f55\u548c\u66f4\u65b0\u8bb0\u5f55\u7684\u65f6\u95f4\u4f1a\u88ab\u540c\u6b65\u66f4\u65b0\u3002\u8bbe\u7f6e\u7684\u4e24\u4e2a\u65f6\u95f4create_time\u3001update_time\uff0c\u6309\u7167\u9884\u671f\u6765\u8bb2\uff0c\u5e94\u8be5\u662f\u521b\u5efa\u8bb0\u5f55\u7684\u65f6\u5019\u4f1a\u540c\u6b65\u66f4\u65b0create_time\uff0cupdate_time\uff0c\u800c\u5728\u66f4\u65b0\u8bb0\u5f55\u7684\u65f6\u5019\uff0c\u53ea\u6709update_time\u4f1a\u88ab\u540c\u6b65\u66f4\u65b0\u3002\u4f46\u5b9e\u9645\u60c5\u51b5\u5374\u662fupdate\u8bb0\u5f55\u65f6\uff0c\u4e24\u4e2atime\u90fd\u4f1a\u88ab\u540c\u6b65\u66f4\u65b0\u3002<\/p>\n

\u5728\u4ee3\u7801\u4e2d\u5e76\u6ca1\u6709\u5bf9\u65f6\u95f4\u8fdb\u884c\u663e\u6027\u7684\u8bbe\u7f6e\uff0c\u5bf9\u65f6\u95f4\u7684\u7ef4\u62a4\u662fMySQL\u672c\u8eab\u8fdb\u884c\u7ba1\u7406\u7684\uff0c\u6240\u4ee5\u5c31\u67e5\u770b\u4e86\u4e00\u4e0b\u4e4b\u524d\u540c\u4e8b\u521b\u5efa\u8868\u65f6\u7684SQL\u3002<\/p>\n

\"\"<\/p>\n

\u901a\u8fc7SQL\u8bed\u53e5\u53ef\u4ee5\u770b\u51fa\uff0ccreate_time \u548c update_time \u8bbe\u7f6e\u7684\u90fd\u662f DEFAULT CURRENT_TIMESTAMP\uff0c\u4e0d\u7ba1\u662f\u65b0\u521b\u5efa\u7684\u8bb0\u5f55\uff0c\u8fd8\u662f\u66f4\u65b0\u539f\u6709\u7684\u8bb0\u5f55\uff0c\u53ea\u8981\u662f\u6709\u89e6\u53d1\u7684\u64cd\u4f5c\uff0c\u8fd9\u4e24\u4e2a\u65f6\u95f4\u5c31\u4f1a\u88ab\u540c\u6b65\u4fee\u6539\u3002\u6240\u4ee5\u8981\u8fbe\u5230\u9884\u671f\u7684\u6548\u679c\uff0c\u5c31\u9700\u8981\u4fee\u6539\u8fd9\u91cc\u4e86\u3002\u95ee\u9898\u6839\u6e90\u5c31\u662fSQL\u8bed\u53e5\u8fd9\u91cc\u7684\u8bbe\u7f6e\u3002<\/p>\n

\u89e3\u51b3\uff1a<\/strong><\/div>\n

\u628aupdate_time\u7684 DEFAULT CURRENT_TIMESTAMP\u540e\u9762\u518d\u52a0\u4e0a\u6761\u4ef6\u9650\u5236 ON UPDATE CURRENT_TIMESTAMP\u3002\u8fd9\u6837\u5728\u66f4\u65b0\u8bb0\u5f55\u65f6\uff0c\u53ea\u6709\u66f4\u65b0\u65f6\u95f4\u88ab\u4fee\u6539\uff0c\u521b\u5efa\u65f6\u95f4\u5c31\u662f\u6700\u521d\u521b\u5efa\u8bb0\u5f55\u7684\u65f6\u95f4\u3002<\/p>\n

MySQL\u4e2d\u7684CURRENT_TIMESTAMP\uff1a<\/p>\n

\u5728\u521b\u5efa\u65f6\u95f4\u5b57\u6bb5\u7684\u65f6\u5019-----<\/p>\n

\u2460 DEFAULT CURRENT_TIMESTAMP<\/p>\n

\u8868\u793a\u5f53\u63d2\u5165\u6570\u636e\u7684\u65f6\u5019\uff0c\u8be5\u5b57\u6bb5\u9ed8\u8ba4\u503c\u4e3a\u5f53\u524d\u65f6\u95f4<\/p>\n

\u2461 ON UPDATE CURRENT_TIMESTAMP<\/p>\n

\u8868\u793a\u6bcf\u6b21\u66f4\u65b0\u8fd9\u6761\u6570\u636e\u7684\u65f6\u5019\uff0c\u8be5\u5b57\u6bb5\u90fd\u4f1a\u66f4\u65b0\u6210\u5f53\u524d\u65f6\u95f4<\/p>\n

\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u662fmysql\u6570\u636e\u5e93\u672c\u8eab\u5728\u7ef4\u62a4\uff0c\u6240\u4ee5\u53ef\u4ee5\u6839\u636e\u8fd9\u4e2a\u7279\u6027\u6765\u751f\u6210\u3010\u521b\u5efa\u65f6\u95f4\u3011\u548c\u3010\u66f4\u65b0\u65f6\u95f4\u3011\u4e24\u4e2a\u5b57\u6bb5\uff0c\u4e14\u4e0d\u9700\u8981\u4ee3\u7801\u6765\u7ef4\u62a4\u3002<\/p>\n

\u5982\u4e0b\uff1a<\/p>\n

CREATE TABLE `mytest` (\r\n    `id` bigint NOT NULL AUTO_INCREMENT,\r\n    `comments` varchar(255) DEFAULT '' COMMENT '\u5185\u5bb9',\r\n    `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '\u521b\u5efa\u65f6\u95f4',\r\n    `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '\u66f4\u65b0\u65f6\u95f4'\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n \r\n\/\/\u5982\u679c\u60f3\u8bbe\u7f6e\u4e00\u4e2a\u5177\u4f53\u7684\u9ed8\u8ba4\u65f6\u95f4\u53ef\u4ee5\u8fd9\u6837\uff1a\r\nCREATE TABLE `mytest2` (\r\n  `id` bigint NOT NULL AUTO_INCREMENT,\r\n  `comments` varchar(255) DEFAULT '' COMMENT '\u5185\u5bb9',\r\n  `create_time` timestamp DEFAULT '2020-12-12 12:12:12' COMMENT '\u521b\u5efa\u65f6\u95f4',\r\n  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '\u66f4\u65b0\u65f6\u95f4',\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;<\/pre>\n
\u8865\u5145\uff1amysql\u81ea\u5e26\u7684\u4e00\u4e9b\u51fd\u6570\u529f\u80fd\u4e5f\u662f\u5f88\u5f3a\u5927\u7684\uff0c\u8fd9\u91cc\u6574\u7406\u4e86\u4e00\u4e0b\u65f6\u95f4\u65b9\u9762\u7684<\/strong><\/span><\/div>\n

\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u683c\u5f0f\u4e32\uff1a<\/p>\n

#\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\r\ncurrent_timestamp() yyyy-mm-dd hh:ii:ss \r\nnow() yyyy-mm-dd hh:ii:ss\r\ncurdate() yyyy-mm-dd\r\ncurrent_date()\r\ncurtime() hh:ii:ss\r\ncurrent_time()<\/pre>\n

\u63d0\u53d6date\u5404\u4e2a\u5b57\u6bb5<\/p>\n

#\u63d0\u53d6date\u5404\u4e2a\u5b57\u6bb5\r\ndate('yyyy-mm-dd hh:ii:ss') yyyy-mm-dd\r\nyear('yyyy-mm-dd hh:ii:ss') yyyy\r\nmonth('yyyy-mm-dd hh:ii:ss') mm\r\nday('yyyy-mm-dd hh:ii:ss') dd<\/pre>\n

\u63d0\u53d6time\u5404\u4e2a\u5b57\u6bb5<\/p>\n

#\u63d0\u53d6time\u5404\u4e2a\u5b57\u6bb5\r\ntime('yyyy-mm-dd hh:ii:ss') hh:ii:ss\r\nhour('yyyy-mm-dd hh:ii:ss') hh\r\nminute('yyyy-mm-dd hh:ii:ss') ii\r\nsecond('yyyy-mm-dd hh:ii:ss') ss<\/pre>\n

\u83b7\u53d6\u5f53\u524d\u6216\u8005\u67d0\u4e00\u65f6\u95f4\u7684unix\u65f6\u95f4\u6233<\/p>\n

#unix\u65f6\u95f4\u62331970-01-01\u4ee5\u6765\u7684\u79d2\u6570\r\nunix_timestamp()\r\n#\u540c\u65f6\u8fd8\u53ef\u4ee5\u5c06\u67d0\u4e00\u65f6\u95f4\u683c\u5f0f\u4e32\u7684\u79d2\u6570\u8f6c\u5316\u51fa\u6765\r\nunix_timestamp('yyyy-mm-dd hh:ii:ss')<\/pre>\n

\u683c\u5f0f\u5316\u65f6\u95f4\u4e32\u548c\u683c\u5f0f\u5316\u65f6\u95f4\u6233<\/p>\n

#\u683c\u5f0f\u5316\u65f6\u95f4\u4e32 date_format \u4e0e time_format \u4e3a\u540c\u4e00\u51fd\u6570 \u8f93\u5165\u4e3a \r\ndate_format(now(), '%Y-%m-%d %T');\r\ntime_format(now(), '%H:%i:%s');\r\n#\u683c\u5f0f\u5316\u65f6\u95f4\u6233 \u5c06\u65f6\u95f4\u6233\u8f6c\u5316\u4e3a\u65f6\u95f4\u683c\u5f0f\u4e32\r\nfrom_unixtime(unix_timestamp(), \"%Y-%m-%d %T\")<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eMySQL\u4e2dCURRENT_TIMESTAMP\u65f6\u95f4\u6233\u4f7f\u7528\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6700\u8fd1\u5728\u9879\u76ee\u4e2d\u53d1\u73b0\u4e00\u4e2a\u5c0f\u95ee\u9898\uff0c\u6570\u636e\u88ab\u66f4\u6539\u65f6\uff0c\u63d2\u5165\u8bb0\u5f55\u548c\u66f4\u65b0\u8bb0\u5f55\u7684\u65f6\u95f4\u4f1a\u88ab\u540c\u6b65\u66f4\u65b0\u3002\u8bbe\u7f6e\u7684\u4e24\u4e2a\u65f6\u95f4create_t […]<\/p>\n","protected":false},"author":1479,"featured_media":11367,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-239294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=239294"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239294\/revisions"}],"predecessor-version":[{"id":239300,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239294\/revisions\/239300"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/11367"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=239294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=239294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=239294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}