{"id":7519,"date":"2024-11-14T09:20:42","date_gmt":"2024-11-14T01:20:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7519\/"},"modified":"2024-11-14T09:20:42","modified_gmt":"2024-11-14T01:20:42","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8postgresql%e4%b8%ad%e7%94%9f%e6%88%90%e8%87%aa%e5%ae%9a%e4%b9%89%e6%a0%bc%e5%bc%8f%e7%9a%84id%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7519\/","title":{"rendered":"\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173095452485610.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>postgresql\u751f\u6210\u81ea\u5b9a\u4e49id\u683c\u5f0f<\/strong><\/p>\n<p>\u5728postgresql\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u81ea\u589e\u4e3b\u952e\u6765\u751f\u6210\u552f\u4e00\u7684id\u3002\u7136\u800c\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u751f\u6210\u5177\u6709\u7279\u5b9a\u683c\u5f0f\u7684id\uff0c\u4f8b\u5982\u524d\u7f00\u5b57\u7b26\u4e32\u6216\u968f\u673a\u5b57\u7b26\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u5982\u4f55\u751f\u6210\u8fd9\u6837\u7684id\u5462\uff1f<\/p>\n<p><strong>\u524d\u7f00\u5b57\u7b26\u4e32+\u81ea\u589e\u957f\u6570\u5b57<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528serial\u6570\u636e\u7c7b\u578b\u548cconcat()\u51fd\u6570\u6765\u5b9e\u73b0\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<p>\u8be5\u5c06\u751f\u6210\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u683c\u5f0f\u7684id\uff1a<\/p>\n<pre>bg-0000000000000001\nbg-0000000000000002\n...<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u524d\u7f00\u5b57\u7b26\u4e32+\u968f\u673a\u5b57\u7b26<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528gensalt()\u51fd\u6570\u548csubstring()\u51fd\u6570\u6765\u751f\u6210\uff1a<\/p>\n<pre>create table your_table_name (\n    id text primary key default concat('bg-', substring(gensalt('md5'), 3, 10))\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5sql\u8bed\u53e5\u5c06\u751f\u6210\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u683c\u5f0f\u7684id\uff1a<\/p>\n<pre>bg-000857Y3EX\nbg-00085ERU8y\n...<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f\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>postgresql\u751f\u6210\u81ea\u5b9a\u4e49id\u683c\u5f0f \u5728postgresql\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u81ea\u589e\u4e3b\u952e\u6765\u751f\u6210\u552f\u4e00\u7684id\u3002\u7136\u800c\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u751f\u6210\u5177\u6709\u7279\u5b9a\u683c\u5f0f\u7684id\uff0c\u4f8b\u5982\u524d\u7f00\u5b57\u7b26\u4e32\u6216\u968f\u673a\u5b57\u7b26\u3002 \u90a3\u4e48\uff0c\u5982\u4f55\u751f\u6210\u8fd9\u6837\u7684id\u5462\uff1f \u524d\u7f00\u5b57\u7b26\u4e32+\u81ea\u589e\u957f\u6570\u5b57 \u53ef\u4ee5\u4f7f\u7528serial\u6570\u636e\u7c7b\u578b\u548cconcat()\u51fd\u6570\u6765\u5b9e\u73b0\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 \u8be5\u5c06\u751f\u6210\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u683c\u5f0f\u7684id\uff1a bg-0000000000000001 bg-0000000000000002 &#8230; \u767b\u5f55\u540e\u590d\u5236 \u524d\u7f00\u5b57\u7b26\u4e32+\u968f\u673a\u5b57\u7b26 \u53ef\u4ee5\u4f7f\u7528gensalt()\u51fd\u6570\u548csubstring()\u51fd\u6570\u6765\u751f\u6210\uff1a create table your_table_name ( id text primary key default concat(&#8216;bg-&#8216;, substring(gensalt(&#8216;md5&#8217;), 3, 10)) ); \u767b\u5f55\u540e\u590d\u5236 \u8be5sql\u8bed\u53e5\u5c06\u751f\u6210\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u683c\u5f0f\u7684id\uff1a bg-000857Y3EX bg-00085ERU8y &#8230; \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f\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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-7519","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7519","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=7519"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7519\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}