{"id":200976,"date":"2025-05-06T08:05:46","date_gmt":"2025-05-06T00:05:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200976\/"},"modified":"2025-05-06T08:05:46","modified_gmt":"2025-05-06T00:05:46","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:\/\/server.hk\/cnblog\/200976\/","title":{"rendered":"\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u4e86\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e3b\u673a\u5b9d\u8d1d\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/17323661296741cf312b46c.jpg\" class=\"aligncenter\"><\/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>\u8be5sql\u8bed\u53e5\u5c06\u751f\u6210\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u683c\u5f0f\u7684id\uff1a<\/p>\n<pre>bg-0000000000000001\nbg-0000000000000002\n...<\/pre>\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>\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>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684ID\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6570\u636e\u5e93\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728PostgreSQL\u4e2d\u751f\u6210&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200976","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200976"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200976\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}