{"id":231128,"date":"2021-12-13T09:31:32","date_gmt":"2021-12-13T01:31:32","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=231128"},"modified":"2021-11-28T17:32:55","modified_gmt":"2021-11-28T09:32:55","slug":"mysql-current_timestamp","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-current_timestamp.html","title":{"rendered":"MySQL\u4e2d\u7684CURRENT_TIMESTAMP"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86MySQL\u4e2dCURRENT_TIMESTAMP\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u5177\u6709\u5f88\u597d\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002\u5982\u6709\u9519\u8bef\u6216\u672a\u8003\u8651\u5b8c\u5168\u7684\u5730\u65b9\uff0c\u671b\u4e0d\u541d\u8d50\u6559<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
CURRENT_TIMESTAMP\u7684\u4f7f\u7528<\/strong><\/div>\n

\u4f17\u6240\u5468\u77e5\uff0cMySQL\u7684\u65e5\u671f\u7c7b\u578b\u53ef\u4ee5\u4f7f\u7528CURRENT_TIMESTAMP\u6765\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u4f46\u662f\u8fd9\u4e2a\u8ddfMySQL\u7684\u7248\u672c\u53ca\u65e5\u671f\u7684\u5177\u4f53\u7c7b\u578b\u6709\u5173\uff0c\u53ea\u67095.6\u4e4b\u540e\u7684\u7248\u672c\u624d\u80fd\u4f7f\u7528CURRENT_TIMESTAMP\u4f5c\u4e3aDATETIME\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n

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

\r\nALTER TABLE t_user ADD update_time DATETIME DEFAULT CURRENT_TIMESTAMP\r\n<\/pre>\n

\u57285.6\u4e4b\u524d\u7684\u7248\u672c\uff0c\u4f7f\u7528CURRENT_TIMESTAMP\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e0b\u9762\u7684\u9519\u8bef<\/p>\n

\r\n[Err] 1067 - Invalid default value for 'update_time'\r\n<\/pre>\n

\u5728MySQL 5.6.5\u7248\u672c\u4e4b\u524d\uff0cDEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\u53ea\u9002\u7528\u4e8eTIMESTAMP\uff0c\u800c\u4e14\u4e00\u5f20\u8868\u4e2d\uff0c\u6700\u591a\u5141\u8bb8\u4e00\u4e2aTIMESTAMP\u5b57\u6bb5\u91c7\u7528\u8be5\u7279\u6027\u3002 \u4eceMySQL 5.6.5\u5f00\u59cb\uff0c DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\u540c\u65f6\u9002\u7528\u4e8eTIMESTAMP\u548cDATETIME\uff0c\u4e14\u4e0d\u9650\u5236\u6570\u91cf\u3002<\/p>\n

timestamp\u4f7f\u7528CURRENT_TIMESTAMP\u62a5\u9519<\/strong><\/div>\n

\u9879\u76ee\u51fa\u73b0\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n

\r\nError updating database.\r\nCause:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'createTime' cannot be null\r\n<\/pre>\n

\u6570\u636e\u6a21\u578b\u5982\u4e0b\uff1a<\/p>\n

\r\n\/* \u521b\u5efa\u65f6\u95f4\u4e0d\u53ef\u4e3a\u7a7a*\/\r\ncreateTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '\u521b\u5efa\u65f6\u95f4' ,\r\n\/* \u66f4\u65b0\u65f6\u95f4\u4e0d\u53ef\u4e3a\u7a7a*\/\r\nupdateTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '\u66f4\u65b0\u65f6\u95f4' ,\r\n<\/pre>\n

\u7ecf\u8fc7\u4e00\u7cfb\u5217\u7684\u95ee\u9898\u6392\u67e5\uff0c\u5b9a\u4f4d\u5230\u662f\u56e0\u4e3a\u4e0d\u540c\u7248\u672c\u7684MySQL\u6570\u636e\u5e93\u5168\u5c40\u53d8\u91cf\u201cexplicit_defaults_for_timestamp\u201d\u7684\u95ee\u9898\u3002<\/p>\n

\r\n-- \u67e5\u770bexplicit_defaults_for_timestamp\u9ed8\u8ba4\u503c\r\nSHOW GLOBAL VARIABLES LIKE \"explicit_defaults_for_timestamp\"; \r\n \r\n-- \u4fee\u6539explicit_defaults_for_timestamp\u9ed8\u8ba4\u503c\r\nSET @@global.explicit_defaults_for_timestamp=OFF;\r\n<\/pre>\n

\u53c2\u6570\u503c\u4e3a\"ON\"\u7684\u60c5\u51b5\uff1a<\/p>\n

\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4f17\u6240\u5468\u77e5\uff0cMySQL\u7684\u65e5\u671f\u7c7b\u578b\u53ef\u4ee5\u4f7f\u7528CURRENT_TIMESTAMP\u6765\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u4f46\u662f\u8fd9\u4e2a\u8ddfMySQL\u7684\u7248 […]<\/p>\n","protected":false},"author":668,"featured_media":15758,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-231128","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\/231128","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\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=231128"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231128\/revisions"}],"predecessor-version":[{"id":231131,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231128\/revisions\/231131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/15758"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=231128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=231128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=231128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}