{"id":231132,"date":"2021-12-14T08:31:33","date_gmt":"2021-12-14T00:31:33","guid":{"rendered":"https:\/\/gulass.cn\/?p=231132"},"modified":"2021-11-28T17:33:00","modified_gmt":"2021-11-28T09:33:00","slug":"postgresql-update-timestamp","status":"publish","type":"post","link":"https:\/\/gulass.cn\/postgresql-update-timestamp.html","title":{"rendered":"PostgreSQL\u81ea\u52a8\u66f4\u65b0\u65f6\u95f4\u6233"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6700\u8fd1\u6709\u8fd9\u4e48\u4e00\u4e2a\u5de5\u7a0b,\u9700\u8981\u4f7f\u7528postgresql\u6570\u636e\u5e93,\u5728\u6570\u636e\u5e93\u4e2d\u7684\u597d\u51e0\u4e2a\u8868\u4e2d\u90fd\u9700\u8981\u65f6\u95f4\u6233\u8fd9\u4e2a\u5b57\u6bb5,\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8ePostgreSQL\u81ea\u52a8\u66f4\u65b0\u65f6\u95f4\u6233\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4ec0\u4e48\u662fPostgreSQL\u65f6\u95f4\u6233\u6570\u636e\u7c7b\u578b\uff1f<\/strong><\/div>\n

\u5728PostgreSQL\u4e2d\uff0c\u4e0b\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u662f TIMESTAMP \uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8 TIME \u548c DATE \u503c\u3002\u4f46\u5b83\u4e0d\u652f\u6301\u4efb\u4f55 \u65f6\u533a\u6570\u636e\u3002\u8fd9\u610f\u5473\u7740\u5f53\u6211\u4eec\u8f6c\u6362\u6570\u636e\u5e93\u670d\u52a1\u5668\u7684 \u65f6\u533a\u65f6\uff0c \u65f6\u95f4\u6233\u503c\u5c06\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u4e14\u4e0d\u80fd\u91cd\u590d\u4fee\u6539\u3002<\/p>\n

\u5728PostgreSQL\u4e2d\uff0cTIMESTAMP\u6570\u636e\u7c7b\u578b\u5206\u4e3a \u4e24\u79cd\u65f6\u95f4\u6570\u636e\u7c7b\u578b\uff0c\u5982\u4e0b:<\/p>\n

timestamp<\/p>\n

timpstamptz<\/p>\n

Timestamp: : \u4f7f\u7528\u65f6\u95f4\u6233\u6570\u636e\u7c7b\u578b \uff0c\u4e0d\u5e26\u65f6\u533a\u3002<\/p>\n

Timestamptz: : timestamptz\u6570\u636e\u7c7b\u578b\u7528\u4e8e\u5e26\u6709\u65f6\u533a\u7684 \u3002<\/p>\n

\u6ce8\u610f:<\/p>\n

    \n
  • timestamptz\u6570\u636e\u7c7b\u578b\u662f\u4e0e\u65f6\u533a\u76f8\u5173\u7684\u65e5\u671f\u548c\u65f6\u95f4\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u4e14\u662f\u5177\u6709\u65f6\u533a\u7684\u65f6\u95f4\u6233\u3002<\/li>\n
  • \u65f6\u95f4\u6233\u548c\u65f6\u95f4\u6233\u5b58\u50a8\u65f6\u95f4\u6233\u503c\u7684 8\u5b57\u8282\u5b58\u50a8\uff0c\u5982\u4ee5\u4e0b\u547d\u4ee4\u6240\u793a:<\/li>\n<\/ul>\n
    \r\nSELECT typname, typlen\r\nFROM pg_type\r\nWHERE typname ~ '^timestamp';\r\n<\/pre>\n

    \u6700\u8fd1\u4e00\u6bb5\u65f6\u95f4\u516c\u53f8\u6709\u4e00\u4e9b\u5165\u804c\u7684\u5b9e\u4e60\u751f\u5728\u4f7f\u7528PostgreSQL\u521b\u5efa\u8868Schema\u65f6\uff0c\u5bf9\u521b\u5efa\u65f6\u95f4create_at\u548c\u66f4\u65b0\u65f6\u95f4update_at\u5b57\u6bb5\u81ea\u52a8\u66f4\u65b0\u65f6\u95f4\u6233\u4e0d\u662f\u5f88\u6e05\u695a\u3002\u8bb0\u5f55\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8ba9\u540e\u9762\u6765\u7684\u5b9e\u4e60\u751f\u548c\u65b0\u540c\u4e8b\u6709\u4e00\u4e2a\u53c2\u8003\u3002<\/p>\n

    \u4e00\u3001\u4e3a\u4ec0\u4e48<\/strong><\/div>\n

    \u5728\u8bb2\u5982\u4f55\u66f4\u65b0\u65f6\u95f4\u6233\u4e4b\u524d\uff0c\u6211\u4eec\u8fd8\u662f\u8981\u4e86\u89e3\u4e3a\u4f55\u6709\u5fc5\u8981\u81ea\u52a8\u66f4\u65b0\u65f6\u95f4\u6233\u3002\u5bf9\u4e8e\u5f88\u591a\u4e1a\u52a1\u8868\uff0c\u6211\u4eec\u5927\u591a\u6570\u9700\u8981\u8bb0\u5f55\u4ee5\u4e0b\u51e0\u4e2a\u5b57\u6bb5\uff1a<\/p>\n

      \n
    • create_at \u521b\u5efa\u65f6\u95f4<\/li>\n
    • update_at \u66f4\u65b0\u65f6\u95f4<\/li>\n
    • create_by \u521b\u5efa\u4eba<\/li>\n
    • update_by \u66f4\u65b0\u4eba<\/li>\n<\/ul>\n

      \u4e3a\u4e86\u7ed9\u8fd9\u4e9b\u5b57\u6bb5\u8d4b\u503c\uff0c\u6211\u4eec\u9700\u8981\u5728repository\u5c42\u4e3aentity\u8d4b\u503c\uff0c\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u5c31\u53d6\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4LocalDateTime,\u521b\u5efa\u4eba\u548c\u66f4\u65b0\u4eba\u9700\u8981\u7528\u7cfb\u7edf\u7528\u6237\u53bb\u8d4b\u503c\u3002\u5bf9\u4e8e\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\uff0c\u8fd9\u79cd\u4e0e\u5f53\u524d\u4e1a\u52a1\u65e0\u5173\u7684\u5b57\u6bb5\uff0c\u6709\u6ca1\u6709\u53ef\u80fd\u4e0d\u5728repository\u4e0a\u6bcf\u6b21\u53bb\u624b\u52a8\u8d4b\u503c\u3002<\/p>\n

      \u5f53\u7136\uff0c\u80af\u5b9a\u662f\u6709\u7684\uff0c\u521b\u5efa\u65f6\u95f4\u65e0\u975e\u5c31\u662f\u6570\u636e\u65b0\u63d2\u5165\u884c\u7684\u65f6\u95f4\uff0c\u66f4\u65b0\u65f6\u95f4\u5c31\u662f\u884c\u6570\u636e\u66f4\u65b0\u7684\u65f6\u95f4\uff0c\u7406\u89e3\u4e86\u8fd9\u4e00\u5c42\u7684\u542b\u4e49\uff0c\u90a3\u5c31\u6709\u89e3\u51b3\u529e\u6cd5\u4e86\u3002<\/p>\n

      \u5bf9\u4e8eMysql\u6765\u8bf4\uff0c\u5176\u5185\u90e8\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u4e8e\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u7684\u5b57\u6bb5\u7684\u81ea\u52a8\u66f4\u65b0\u662f\u76f8\u5f53\u5bb9\u6613\u7684\uff0c\u4f46\u5bf9\u4e8ePostgreSQL\u4e8b\u60c5\u4f1a\u7a0d\u7a0d\u590d\u6742\u4e00\u70b9\u3002<\/p>\n

      \u4e8c\u3001\u5982\u4f55\u505a<\/strong><\/div>\n

      \u8981\u5728\u63d2\u5165\u6570\u636e\u7684\u65f6\u5019\u81ea\u52a8\u586b\u5145 create_at\u5217\u7684\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528DEFAULT\u503c\uff0c\u5982\u4e0b\u9762\u6240\u793a\u3002<\/p>\n

      \r\nCREATE TABLE users (\r\n  ...\r\n  create_at timestamp(6) default current_timestamp\r\n)\r\n<\/pre>\n

      \u4e3acreate_at\u5b57\u6bb5\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503ccurrent_timestamp\u5f53\u524d\u65f6\u95f4\u6233\uff0c\u8fd9\u6837\u8fbe\u5230\u4e86\u901a\u8fc7\u5728 INSERT \u8bed\u53e5\u4e2d\u63d0\u4f9b\u503c\u6765\u663e\u5f0f\u5730\u8986\u76d6\u8be5\u5217\u7684\u503c\u3002<\/p>\n

      \u4f46\u4e0a\u9762\u7684\u8fd9\u79cd\u65b9\u5f0f\u53ea\u662f\u5bf9\u4e8einsert\u884c\u6570\u636e\u7684\u65f6\u5019\u7ba1\u7528\uff0c\u5982\u679c\u5bf9\u884c\u66f4\u65b0\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5230\u6570\u636e\u5e93\u7684\u89e6\u53d1\u5668trigger\u3002<\/p>\n

      \u9996\u5148\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u89e6\u53d1\u5668update_modified_column\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\uff0c\u542b\u4e49\u662f\u66f4\u65b0\u8868\u7684\u5b57\u6bb5update_at\u4e3a\u5f53\u524d\u65f6\u95f4\u6233\u3002<\/p>\n

      \r\nCREATE OR REPLACE FUNCTION update_modified_column()\r\nRETURNS TRIGGER AS $$\r\nBEGIN\r\n    NEW.update_at = now();\r\n    RETURN NEW;\r\nEND;\r\n$$ language 'plpgsql';\r\n<\/pre>\n

      \u7136\u540e\u6211\u4eec\u5e94\u7528\u8fd9\u4e2a\u89e6\u53d1\u5668\uff0c\u5982\u4f55\u5e94\u7528\u5462\uff1f\u5f53\u7136\u662f\u4e3a\u8fd9\u4e2a\u89e6\u53d1\u5668\u8bbe\u7f6e\u89e6\u53d1\u6761\u4ef6\u3002<\/p>\n

      \r\nCREATE TRIGGER update_table_name_update_at BEFORE UPDATE ON table_name FOR EACH ROW EXECUTE PROCEDURE  update_modified_column();\r\n<\/pre>\n

      \u5373\u4ee3\u8868\u7684\u542b\u4e49\u662f\u66f4\u65b0\u8868table_name\u884c\u6570\u636e\u7684\u65f6\u5019\uff0c\u6267\u884c\u8fd9\u4e2a\u89e6\u53d1\u5668\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u6bcf\u4e00\u4e2a\u8868\u8bbe\u7f6e\u5e94\u7528\u8fd9\u4e2a\u89e6\u53d1\u5668\uff01\u81f3\u6b64\uff0c\u8fbe\u5230\u76ee\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

      \u5728PostgreSQL\u4e2d\uff0c\u4e0b\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u662f TIMESTAMP \uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8 TIME \u548c DATE \u503c\u3002\u4f46\u5b83 […]<\/p>\n","protected":false},"author":668,"featured_media":7449,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-231132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/231132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=231132"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/231132\/revisions"}],"predecessor-version":[{"id":231134,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/231132\/revisions\/231134"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/7449"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=231132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=231132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=231132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}