{"id":2691,"date":"2025-03-12T23:50:03","date_gmt":"2025-03-12T15:50:03","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2691"},"modified":"2025-03-12T23:50:03","modified_gmt":"2025-03-12T15:50:03","slug":"postgresql-auto-increment%ef%bc%88%e8%87%aa%e5%8a%a8%e5%a2%9e%e9%95%bf%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/12\/postgresql-auto-increment%ef%bc%88%e8%87%aa%e5%8a%a8%e5%a2%9e%e9%95%bf%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"PostgreSQL AUTO INCREMENT\uff08\u81ea\u52a8\u589e\u957f\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728 PostgreSQL \u4e2d\uff0c<code>AUTO INCREMENT<\/code> \u662f\u6307\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u6570\u5b57\uff0c\u7528\u4e8e\u4e3a\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u751f\u6210\u552f\u4e00\u7684\u6807\u8bc6\u7b26\u3002PostgreSQL \u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u65b9\u5f0f\u4e0e\u5176\u4ed6\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff08\u5982 MySQL\uff09\u4e0d\u540c\uff0c\u5b83\u4f7f\u7528 <strong>\u5e8f\u5217\uff08Sequence\uff09<\/strong> \u6765\u5b9e\u73b0\u81ea\u52a8\u9012\u589e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 <code>SERIAL<\/code> \u7c7b\u578b\u5b9e\u73b0\u81ea\u52a8\u589e\u957f<\/strong><\/h3>\n\n\n\n<p>PostgreSQL \u4f7f\u7528 <code>SERIAL<\/code> \u6570\u636e\u7c7b\u578b\u6765\u5b9e\u73b0\u7c7b\u4f3c\u4e8e <code>AUTO INCREMENT<\/code> \u7684\u529f\u80fd\u3002<code>SERIAL<\/code> \u662f\u4e00\u4e2a\u4f2a\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u5b9e\u9645\u4e0a\u662f\u4e3a\u5b57\u6bb5\u521b\u5efa\u4e00\u4e2a\u81ea\u52a8\u9012\u589e\u7684\u5e8f\u5217\uff08sequence\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.1 <code>SERIAL<\/code> \u6570\u636e\u7c7b\u578b<\/strong><\/h4>\n\n\n\n<p><code>SERIAL<\/code> \u6570\u636e\u7c7b\u578b\u4e3a\u5217\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7684\u5e8f\u5217\uff0c\u5e76\u4f7f\u8be5\u5217\u5728\u6bcf\u6b21\u63d2\u5165\u65f6\u81ea\u52a8\u589e\u52a0\u3002<\/p>\n\n\n\n<p>PostgreSQL \u63d0\u4f9b\u4e86\u4e09\u79cd <code>SERIAL<\/code> \u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SERIAL<\/code>: \u5bf9\u5e94 4 \u5b57\u8282\u6574\u6570\uff08\u8303\u56f4\uff1a-2,147,483,648 \u5230 2,147,483,647\uff09\u3002<\/li>\n\n\n\n<li><code>BIGSERIAL<\/code>: \u5bf9\u5e94 8 \u5b57\u8282\u6574\u6570\uff08\u8303\u56f4\uff1a-9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807\uff09\u3002<\/li>\n\n\n\n<li><code>SMALLSERIAL<\/code>: \u5bf9\u5e94 2 \u5b57\u8282\u6574\u6570\uff08\u8303\u56f4\uff1a-32,768 \u5230 32,767\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u521b\u5efa\u5e26\u6709 <code>SERIAL<\/code> \u5217\u7684\u8868<\/strong><\/h2>\n\n\n\n<p>\u5728 PostgreSQL \u4e2d\u521b\u5efa\u5e26\u6709\u81ea\u52a8\u9012\u589e\u5217\u7684\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a <code>SERIAL<\/code> \u7c7b\u578b\u6765\u5b9e\u73b0\u81ea\u52a8\u9012\u589e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u521b\u5efa\u5e26\u6709 <code>SERIAL<\/code> \u7684\u8868<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    id SERIAL PRIMARY KEY,   -- \u81ea\u52a8\u9012\u589e\u7684\u4e3b\u952e\n    name VARCHAR(100),\n    salary NUMERIC\n);<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>id SERIAL PRIMARY KEY<\/code>\uff1a\u4e3a <code>id<\/code> \u5217\u521b\u5efa\u4e86\u4e00\u4e2a\u81ea\u52a8\u9012\u589e\u7684\u6574\u6570\u503c\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u4e3b\u952e\u3002<\/li>\n\n\n\n<li>\u6bcf\u6b21\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\uff0c<code>id<\/code> \u5217\u5c06\u81ea\u52a8\u751f\u6210\u552f\u4e00\u503c\uff0c\u65e0\u9700\u624b\u52a8\u6307\u5b9a\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>BIGSERIAL<\/code><\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u66f4\u5927\u7684\u6570\u5b57\u8303\u56f4\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>BIGSERIAL<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE orders (\n    order_id BIGSERIAL PRIMARY KEY,   -- \u4f7f\u7528\u66f4\u5927\u7684\u9012\u589e\u8303\u56f4\n    order_date DATE,\n    customer_id INT\n);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u624b\u52a8\u63a7\u5236\u5e8f\u5217\u7684\u503c<\/strong><\/h2>\n\n\n\n<p>\u867d\u7136 PostgreSQL \u4f1a\u81ea\u52a8\u7ba1\u7406\u5e8f\u5217\u7684\u9012\u589e\uff0c\u4f46\u4f60\u4ecd\u7136\u53ef\u4ee5\u624b\u52a8\u63a7\u5236\u5e8f\u5217\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u67e5\u770b\u5e8f\u5217\u7684\u5f53\u524d\u503c<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2 <code>pg_sequences<\/code> \u6216\u4f7f\u7528 <code>currval<\/code> \u6765\u67e5\u770b\u5e8f\u5217\u7684\u5f53\u524d\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT last_value FROM orders_order_id_seq;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c<code>orders_order_id_seq<\/code> \u662f\u7531 <code>BIGSERIAL<\/code> \u521b\u5efa\u7684\u5e8f\u5217\u540d\u79f0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u8bbe\u7f6e\u5e8f\u5217\u7684\u8d77\u59cb\u503c<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>setval<\/code> \u6765\u8bbe\u7f6e\u5e8f\u5217\u7684\u503c\uff0c\u4ece\u800c\u63a7\u5236\u5e8f\u5217\u7684\u4e0b\u4e00\u4e2a\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT setval('orders_order_id_seq', 1000, false);<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u91ca<\/strong>\uff1a\u8fd9\u4f1a\u5c06\u5e8f\u5217 <code>orders_order_id_seq<\/code> \u7684\u4e0b\u4e00\u4e2a\u503c\u8bbe\u7f6e\u4e3a 1000\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a <code>false<\/code>\uff0c\u8868\u793a\u4e0b\u4e00\u4e2a\u8fd4\u56de\u503c\u4e3a 1000\uff08\u5982\u679c\u662f <code>true<\/code>\uff0c\u5e8f\u5217\u5c06\u8fd4\u56de 1001\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u4f7f\u7528 <code>nextval<\/code> \u83b7\u53d6\u4e0b\u4e00\u4e2a\u5e8f\u5217\u503c<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>nextval<\/code> \u83b7\u53d6\u5e8f\u5217\u7684\u4e0b\u4e00\u4e2a\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT nextval('orders_order_id_seq');<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u63d2\u5165\u6570\u636e\u65f6\u5ffd\u7565\u81ea\u52a8\u9012\u589e\u5217<\/strong><\/h2>\n\n\n\n<p>\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u5982\u679c\u5217\u5df2\u7ecf\u8bbe\u7f6e\u4e3a\u81ea\u52a8\u9012\u589e\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5ffd\u7565\u8be5\u5217\u5e76\u8ba9\u7cfb\u7edf\u81ea\u52a8\u751f\u6210\u503c\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u63d2\u5165\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO employees (name, salary)\nVALUES ('John Doe', 50000);<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c<code>id<\/code> \u5217\u5c06\u81ea\u52a8\u9012\u589e\uff0c\u800c\u65e0\u9700\u660e\u786e\u6307\u5b9a\u5176\u503c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u5220\u9664\u81ea\u52a8\u9012\u589e\u5217<\/strong><\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u5220\u9664\u81ea\u52a8\u9012\u589e\u5217\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ALTER TABLE<\/code> \u6765\u5220\u9664\u5217\u7684\u9ed8\u8ba4\u503c\u548c\u5e8f\u5217\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5220\u9664\u5e8f\u5217<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees\n  ALTER COLUMN id SET DEFAULT nextval('employees_id_seq'::regclass);<\/code><\/pre>\n\n\n\n<p>\u4f46\u8981\u5220\u9664 <code>AUTO INCREMENT<\/code> \u529f\u80fd\uff0c\u4f60\u9700\u8981\u624b\u52a8\u5220\u9664\u76f8\u5173\u5e8f\u5217\u6216\u91cd\u7f6e\u5217\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u4f7f\u7528 <code>UUID<\/code> \u66ff\u4ee3\u81ea\u52a8\u9012\u589e<\/strong><\/h2>\n\n\n\n<p>\u6709\u65f6\uff0c\u4f7f\u7528 <code>UUID<\/code> \uff08\u901a\u7528\u552f\u4e00\u6807\u8bc6\u7b26\uff09\u4f5c\u4e3a\u4e3b\u952e\u6bd4 <code>AUTO INCREMENT<\/code> \u66f4\u5408\u9002\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u9700\u8981\u5206\u5e03\u5f0f\u6570\u636e\u5e93\u65f6\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 UUID<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE users (\n    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),\n    name VARCHAR(100)\n);<\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u65e0\u9700\u4f9d\u8d56\u6570\u5b57\u9012\u589e\uff0c\u800c\u662f\u4f7f\u7528\u968f\u673a\u751f\u6210\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>PostgreSQL<\/strong> \u4f7f\u7528 <code>SERIAL<\/code> \u548c <code>BIGSERIAL<\/code> \u6570\u636e\u7c7b\u578b\u6765\u6a21\u62df <code>AUTO INCREMENT<\/code> \u7684\u529f\u80fd\u3002<\/li>\n\n\n\n<li><code>SERIAL<\/code> \u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u5e8f\u5217\u5e76\u5c06\u5176\u4e0e\u8868\u4e2d\u7684\u5217\u5173\u8054\uff0c\u5728\u63d2\u5165\u6570\u636e\u65f6\u81ea\u52a8\u751f\u6210\u552f\u4e00\u503c\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u624b\u52a8\u63a7\u5236\u5e8f\u5217\u7684\u503c\uff0c\u6216\u8005\u4f7f\u7528 <code>UUID<\/code> \u6765\u66ff\u4ee3\u81ea\u52a8\u9012\u589e\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u4e2d\uff0cAUTO INCREMENT \u662f\u6307\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u6570\u5b57\uff0c\u7528\u4e8e\u4e3a\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u751f\u6210 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2692,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2691"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2691\/revisions"}],"predecessor-version":[{"id":2693,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2691\/revisions\/2693"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2692"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}