{"id":5649,"date":"2024-11-14T10:30:33","date_gmt":"2024-11-14T02:30:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5649\/"},"modified":"2024-11-14T10:30:33","modified_gmt":"2024-11-14T02:30:33","slug":"sql%e6%80%8e%e4%b9%88%e6%8f%92%e5%85%a5%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5649\/","title":{"rendered":"sql\u600e\u4e48\u63d2\u5165\u65f6\u95f4"},"content":{"rendered":"<blockquote><p>\n  \u6700\u4f73\u5b9e\u8df5\u63a8\u8350\u4f7f\u7528 current_timestamp \u51fd\u6570\u63d2\u5165\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\uff0c\u4ee5\u786e\u4fdd\u65f6\u95f4\u51c6\u786e\u3002\u6b64\u5916\uff0c\u53ef\u8003\u8651\u65f6\u533a\uff0c\u4f7f\u7528\u6807\u51c6\u65f6\u95f4\u683c\u5f0f\uff0c\u907f\u514d\u4f7f\u7528 null \u503c\uff0c\u5e76\u4f7f\u7528 default \u7ea6\u675f\u6216\u89e6\u53d1\u5668\u6765\u63d2\u5165\u9ed8\u8ba4\u65f6\u95f4\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019484165189.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u63d2\u5165\u65f6\u95f4\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u63d2\u5165\u65f6\u95f4\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d\u63d2\u5165\u65f6\u95f4\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 SQL \u63d2\u5165\u65f6\u95f4\uff1f<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\u63d2\u5165\u65f6\u95f4\u503c\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li> <strong>\u4f7f\u7528 CURRENT_TIMESTAMP\uff1a<\/strong>\u63d2\u5165\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\u3002<\/li>\n<li> <strong>\u4f7f\u7528\uff1a<\/strong>\u63d2\u5165\u7279\u5b9a\u65e5\u671f\u548c\u65f6\u95f4\u4f5c\u4e3a\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528 CURRENT_TIMESTAMP<\/strong><\/p>\n<p>\u4f7f\u7528 CURRENT_TIMESTAMP \u51fd\u6570\u63d2\u5165\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\uff0c\u8fd9\u662f\u6700\u53ef\u9760\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5b83\u786e\u4fdd\u63d2\u5165\u7684\u65f6\u95f4\u59cb\u7ec8\u51c6\u786e\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>INSERT INTO table_name (time_column) VALUES (CURRENT_TIMESTAMP);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u5b57\u7b26\u4e32\u5e38\u91cf<\/strong><\/p>\n<p>\u4f7f\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u63d2\u5165\u7279\u5b9a\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u901a\u5e38\u7528\u4e8e\u63d2\u5165\u5386\u53f2\u6570\u636e\u6216\u6a21\u62df\u4e0d\u540c\u65f6\u95f4\u70b9\u3002\u4f46\u662f\uff0c\u5b83\u4f9d\u8d56\u4e8e\u624b\u52a8\u8f93\u5165\u65f6\u95f4\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>INSERT INTO table_name (time_column) VALUES ('2023-03-08 10:30:00');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u8003\u8651\u65f6\u533a<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 CURRENT_TIMESTAMP \u65f6\uff0c\u9700\u8981\u8003\u8651\u65f6\u533a\u3002\u5982\u679c\u6570\u636e\u5e93\u6216\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u4e0d\u540c\u7684\u65f6\u533a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65f6\u95f4\u503c\u4e0d\u6b63\u786e\u3002\u53ef\u4ee5\u6307\u5b9a\u65f6\u533a\u6216\u5c06\u65f6\u95f4\u503c\u5b58\u50a8\u4e3a UTC\uff08\u534f\u8c03\u4e16\u754c\u65f6\uff09\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>-- \u6307\u5b9a\u65f6\u533a\nSET TIMEZONE='America\/New_York';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>-- \u5b58\u50a8\u4e3a UTC\nINSERT INTO table_name (time_column) VALUES (CURRENT_TIMESTAMP AT TIME ZONE 'UTC');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6807\u51c6\u65f6\u95f4\u683c\u5f0f<\/strong><\/p>\n<p>\u59cb\u7ec8\u4f7f\u7528\u4e00\u81f4\u7684\u65f6\u95f4\u683c\u5f0f\uff0c\u4f8b\u5982 YYYY-MM-DD HH:MM:SS\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u65f6\u95f4\u503c\u5728\u4e0d\u540c\u7684\u7cfb\u7edf\u4e4b\u95f4\u517c\u5bb9\u3002<\/p>\n<p><strong>5. \u907f\u514d\u4f7f\u7528 NULL \u503c<\/strong><\/p>\n<p>\u5982\u679c\u65f6\u95f4\u5b57\u6bb5\u4e0d\u80fd\u4e3a null\uff0c\u8bf7\u4f7f\u7528 DEFAULT \u7ea6\u675f\u6216\u89e6\u53d1\u5668\u6765\u63d2\u5165\u9ed8\u8ba4\u65f6\u95f4\u503c\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u63d2\u5165\u65f6\u95f4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u4f73\u5b9e\u8df5\u63a8\u8350\u4f7f\u7528 current_timestamp \u51fd\u6570\u63d2\u5165\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\uff0c\u4ee5\u786e\u4fdd\u65f6\u95f4\u51c6\u786e\u3002\u6b64\u5916\uff0c\u53ef\u8003\u8651\u65f6\u533a\uff0c\u4f7f\u7528\u6807\u51c6\u65f6\u95f4\u683c\u5f0f\uff0c\u907f\u514d\u4f7f\u7528 null \u503c\uff0c\u5e76\u4f7f\u7528 default \u7ea6\u675f\u6216\u89e6\u53d1\u5668\u6765\u63d2\u5165\u9ed8\u8ba4\u65f6\u95f4\u503c\u3002 SQL \u4e2d\u63d2\u5165\u65f6\u95f4\u7684\u6700\u4f73\u5b9e\u8df5 \u5982\u4f55\u4f7f\u7528 SQL \u63d2\u5165\u65f6\u95f4\uff1f \u5728 SQL \u4e2d\u63d2\u5165\u65f6\u95f4\u503c\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a \u4f7f\u7528 CURRENT_TIMESTAMP\uff1a\u63d2\u5165\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\u3002 \u4f7f\u7528\uff1a\u63d2\u5165\u7279\u5b9a\u65e5\u671f\u548c\u65f6\u95f4\u4f5c\u4e3a\u5b57\u7b26\u4e32\u3002 \u6700\u4f73\u5b9e\u8df5\uff1a 1. \u4f7f\u7528 CURRENT_TIMESTAMP \u4f7f\u7528 CURRENT_TIMESTAMP \u51fd\u6570\u63d2\u5165\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\uff0c\u8fd9\u662f\u6700\u53ef\u9760\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5b83\u786e\u4fdd\u63d2\u5165\u7684\u65f6\u95f4\u59cb\u7ec8\u51c6\u786e\u3002 \u793a\u4f8b\uff1a INSERT INTO table_name (time_column) VALUES (CURRENT_TIMESTAMP); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u5b57\u7b26\u4e32\u5e38\u91cf \u4f7f\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u63d2\u5165\u7279\u5b9a\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u901a\u5e38\u7528\u4e8e\u63d2\u5165\u5386\u53f2\u6570\u636e\u6216\u6a21\u62df\u4e0d\u540c\u65f6\u95f4\u70b9\u3002\u4f46\u662f\uff0c\u5b83\u4f9d\u8d56\u4e8e\u624b\u52a8\u8f93\u5165\u65f6\u95f4\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002 \u793a\u4f8b\uff1a INSERT INTO table_name (time_column) VALUES (&#8216;2023-03-08 10:30:00&#8217;); \u767b\u5f55\u540e\u590d\u5236 3. \u8003\u8651\u65f6\u533a \u5728\u4f7f\u7528 CURRENT_TIMESTAMP \u65f6\uff0c\u9700\u8981\u8003\u8651\u65f6\u533a\u3002\u5982\u679c\u6570\u636e\u5e93\u6216\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u4e0d\u540c\u7684\u65f6\u533a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65f6\u95f4\u503c\u4e0d\u6b63\u786e\u3002\u53ef\u4ee5\u6307\u5b9a\u65f6\u533a\u6216\u5c06\u65f6\u95f4\u503c\u5b58\u50a8\u4e3a UTC\uff08\u534f\u8c03\u4e16\u754c\u65f6\uff09\u3002 \u793a\u4f8b\uff1a &#8212; \u6307\u5b9a\u65f6\u533a SET [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5649","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5649"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5649\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}