{"id":7515,"date":"2024-11-14T15:22:10","date_gmt":"2024-11-14T07:22:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7515\/"},"modified":"2024-11-14T15:22:10","modified_gmt":"2024-11-14T07:22:10","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8postgresql%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\/7515\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528PostgreSQL\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\/173095059820445.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528PostgreSQL\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528PostgreSQL\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>postgresql \u751f\u6210\u81ea\u5b9a\u4e49 id<\/strong><\/p>\n<p>\u5728 postgresql \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u589e\u957f\u7684\u6574\u6570 id\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u9700\u8981\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684 id\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u751f\u6210\u4e24\u79cd\u7c7b\u578b\u683c\u5f0f\u7684 id\u3002<\/p>\n<p><strong>\u81ea\u589e\u957f\u6570\u5b57\u524d\u7f00<\/strong><\/p>\n<p>\u8981\u751f\u6210\u4ee5\u56fa\u5b9a\u5b57\u7b26\u4e32\u4e3a\u524d\u7f00\uff0c\u81ea\u589e\u957f\u6570\u5b57\u4e3a\u540e\u7f00\u7684 id\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>create table company(\n   id text primary key default concat('bg-', lpad(serial, 16, '0'))\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cserial \u662f postgresql \u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u751f\u6210\u81ea\u589e\u957f\u7684\u6574\u6570\u5e8f\u5217\u3002lpad() \u51fd\u6570\u5c06\u6570\u5b57\u586b\u5145\u4e3a\u56fa\u5b9a\u957f\u5ea6\uff0c\u53c2\u6570 16 \u8868\u793a\u586b\u5145\u5230 16 \u4f4d\u6570\uff0c\u4e0d\u8db3\u8865 0\u3002concat() \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<p><strong>\u968f\u673a\u5b57\u6bcd\u6570\u5b57\u524d\u7f00<\/strong><\/p>\n<p>\u8981\u751f\u6210\u4ee5\u56fa\u5b9a\u5b57\u7b26\u4e32\u4e3a\u524d\u7f00\uff0c\u968f\u673a\u5b57\u6bcd\u6570\u5b57\u4e3a\u540e\u7f00\u7684 id\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>CREATE TABLE COMPANY(\n   ID TEXT PRIMARY KEY DEFAULT concat('bg-', random_string(16))\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0crandom_string() \u662f\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u7528\u6765\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528PostgreSQL\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\u4e49 id \u5728 postgresql \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u589e\u957f\u7684\u6574\u6570 id\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u9700\u8981\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684 id\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u751f\u6210\u4e24\u79cd\u7c7b\u578b\u683c\u5f0f\u7684 id\u3002 \u81ea\u589e\u957f\u6570\u5b57\u524d\u7f00 \u8981\u751f\u6210\u4ee5\u56fa\u5b9a\u5b57\u7b26\u4e32\u4e3a\u524d\u7f00\uff0c\u81ea\u589e\u957f\u6570\u5b57\u4e3a\u540e\u7f00\u7684 id\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a create table company( id text primary key default concat(&#8216;bg-&#8216;, lpad(serial, 16, &#8216;0&#8217;)) ); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cserial \u662f postgresql \u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u751f\u6210\u81ea\u589e\u957f\u7684\u6574\u6570\u5e8f\u5217\u3002lpad() \u51fd\u6570\u5c06\u6570\u5b57\u586b\u5145\u4e3a\u56fa\u5b9a\u957f\u5ea6\uff0c\u53c2\u6570 16 \u8868\u793a\u586b\u5145\u5230 16 \u4f4d\u6570\uff0c\u4e0d\u8db3\u8865 0\u3002concat() \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002 \u968f\u673a\u5b57\u6bcd\u6570\u5b57\u524d\u7f00 \u8981\u751f\u6210\u4ee5\u56fa\u5b9a\u5b57\u7b26\u4e32\u4e3a\u524d\u7f00\uff0c\u968f\u673a\u5b57\u6bcd\u6570\u5b57\u4e3a\u540e\u7f00\u7684 id\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a CREATE TABLE COMPANY( ID TEXT PRIMARY KEY DEFAULT concat(&#8216;bg-&#8216;, random_string(16)) ); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0crandom_string() \u662f\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u7528\u6765\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u4e32\u3002 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528PostgreSQL\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-7515","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7515","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=7515"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7515\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}