{"id":26393,"date":"2024-11-24T14:55:23","date_gmt":"2024-11-24T06:55:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26393\/"},"modified":"2024-11-24T14:55:23","modified_gmt":"2024-11-24T06:55:23","slug":"postgresql-%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e5%b8%a6%e6%9c%89%e8%87%aa%e5%ae%9a%e4%b9%89%e6%a0%bc%e5%bc%8f%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93id%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26393\/","title":{"rendered":"PostgreSQL \u4e2d\u5982\u4f55\u751f\u6210\u5e26\u6709\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6570\u636e\u5e93ID\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173090317793974.jpg\" class=\"aligncenter\" title=\"PostgreSQL \u4e2d\u5982\u4f55\u751f\u6210\u5e26\u6709\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6570\u636e\u5e93ID\uff1f\u63d2\u56fe\" alt=\"PostgreSQL \u4e2d\u5982\u4f55\u751f\u6210\u5e26\u6709\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6570\u636e\u5e93ID\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u751f\u6210\u5e26\u6709\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6570\u636e\u5e93id\uff1f<\/strong><\/p>\n<p>\u5728 postgresql \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u683c\u5f0f\u7684 id\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u56fa\u5b9a\u5b57\u7b26\u4e32\u52a0\u4e0a\u6307\u5b9a\u957f\u5ea6\u7684\u81ea\u589e\u957f\u6570\u5b57\uff08\u4f8b\uff1a&#8217;bg-0000000000000001&#8217;\uff09<\/li>\n<li>\u56fa\u5b9a\u5b57\u7b26\u4e32\u52a0\u4e0a\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u6bcd\u6570\u5b57\uff08\u4f8b\uff1a&#8217;bg-000857y3ex&#8217;\uff09<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u751f\u6210\u8fd9\u79cd\u683c\u5f0f\u7684 id\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 postgresql \u7684\u5185\u7f6e\u6570\u636e\u7c7b\u578b serial \u548c\u5b57\u7b26\u4e32\u8fde\u63a5\u51fd\u6570 concat()\u3002<\/p>\n<p><strong>\u81ea\u589e\u957f\u6570\u5b57\u683c\u5f0f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5e26\u6709\u81ea\u589e\u957f\u6570\u5b57\u7684 id\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>create table your_table_name (\n    id text primary key default concat('bg-', lpad(serial, 16, '0'))\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>serial \u6570\u636e\u7c7b\u578b\u7528\u4e8e\u751f\u6210\u81ea\u589e\u957f\u7684\u5e8f\u5217\u3002<\/li>\n<li>lpad() \u51fd\u6570\u7528\u4e8e\u5728\u6570\u5b57\u524d\u9762\u586b\u5145\u524d\u5bfc\u96f6\uff0c\u4ee5\u786e\u4fdd\u6240\u9700\u7684\u957f\u5ea6\u3002<\/li>\n<li>concat() \u51fd\u6570\u8fde\u63a5\u56fa\u5b9a\u5b57\u7b26\u4e32\u548c\u683c\u5f0f\u5316\u7684\u6570\u5b57\u4ee5\u521b\u5efa\u6700\u7ec8\u7684 id\u3002<\/li>\n<\/ul>\n<p><strong>\u968f\u673a\u5b57\u6bcd\u6570\u5b57\u683c\u5f0f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5e26\u6709\u968f\u673a\u5b57\u6bcd\u6570\u5b57\u7684 id\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>CREATE TABLE your_table_name (\n    id TEXT PRIMARY KEY DEFAULT concat('bg-', MD5(RANDOM()))\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>random() \u51fd\u6570\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\u3002<\/li>\n<li>md5() \u51fd\u6570\u5c06\u968f\u673a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u5341\u516d\u8fdb\u5236\u503c\u3002<\/li>\n<li>concat() \u51fd\u6570\u8fde\u63a5\u56fa\u5b9a\u5b57\u7b26\u4e32\u548c\u5341\u516d\u8fdb\u5236\u503c\u4ee5\u521b\u5efa\u6700\u7ec8\u7684 id\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fPostgreSQL \u4e2d\u5982\u4f55\u751f\u6210\u5e26\u6709\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6570\u636e\u5e93ID\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u751f\u6210\u5e26\u6709\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6570\u636e\u5e93id\uff1f \u5728 postgresql \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u683c\u5f0f\u7684 id\uff0c\u4f8b\u5982\uff1a \u56fa\u5b9a\u5b57\u7b26\u4e32\u52a0\u4e0a\u6307\u5b9a\u957f\u5ea6\u7684\u81ea\u589e\u957f\u6570\u5b57\uff08\u4f8b\uff1a&#8217;bg-0000000000000001&#8217;\uff09 \u56fa\u5b9a\u5b57\u7b26\u4e32\u52a0\u4e0a\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u6bcd\u6570\u5b57\uff08\u4f8b\uff1a&#8217;bg-000857y3ex&#8217;\uff09 \u89e3\u51b3\u65b9\u6848 \u8981\u751f\u6210\u8fd9\u79cd\u683c\u5f0f\u7684 id\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 postgresql \u7684\u5185\u7f6e\u6570\u636e\u7c7b\u578b serial \u548c\u5b57\u7b26\u4e32\u8fde\u63a5\u51fd\u6570 concat()\u3002 \u81ea\u589e\u957f\u6570\u5b57\u683c\u5f0f \u5bf9\u4e8e\u5e26\u6709\u81ea\u589e\u957f\u6570\u5b57\u7684 id\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a create table your_table_name ( id text primary key default concat(&#8216;bg-&#8216;, lpad(serial, 16, &#8216;0&#8217;)) ); \u767b\u5f55\u540e\u590d\u5236 serial \u6570\u636e\u7c7b\u578b\u7528\u4e8e\u751f\u6210\u81ea\u589e\u957f\u7684\u5e8f\u5217\u3002 lpad() \u51fd\u6570\u7528\u4e8e\u5728\u6570\u5b57\u524d\u9762\u586b\u5145\u524d\u5bfc\u96f6\uff0c\u4ee5\u786e\u4fdd\u6240\u9700\u7684\u957f\u5ea6\u3002 concat() \u51fd\u6570\u8fde\u63a5\u56fa\u5b9a\u5b57\u7b26\u4e32\u548c\u683c\u5f0f\u5316\u7684\u6570\u5b57\u4ee5\u521b\u5efa\u6700\u7ec8\u7684 id\u3002 \u968f\u673a\u5b57\u6bcd\u6570\u5b57\u683c\u5f0f \u5bf9\u4e8e\u5e26\u6709\u968f\u673a\u5b57\u6bcd\u6570\u5b57\u7684 id\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a CREATE TABLE your_table_name ( id TEXT PRIMARY KEY DEFAULT concat(&#8216;bg-&#8216;, MD5(RANDOM())) ); [&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-26393","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26393","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=26393"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26393\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}