{"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":"
\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 |