{"id":160645,"date":"2024-11-03T23:49:22","date_gmt":"2024-11-03T15:49:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160645\/"},"modified":"2024-11-03T23:49:23","modified_gmt":"2024-11-03T15:49:23","slug":"postgresql%e6%95%99%e7%a8%8bpostgresql-insert-into-%e8%aa%9e%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160645\/","title":{"rendered":"POSTGRESQL\u6559\u7a0bPostgreSQL INSERT INTO \u8a9e\u53e5"},"content":{"rendered":"<h1 id=\"postgresql-%e6%95%99%e7%a8%8b%ef%bc%9apostgresql-insert-into-%e8%aa%9e%e5%8f%a5-aqJKLpkGOH\">PostgreSQL \u6559\u7a0b\uff1aPostgreSQL INSERT INTO \u8a9e\u53e5<\/h1>\n<p>PostgreSQL \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e PostgreSQL \u4e2d\u7684 INSERT INTO \u8a9e\u53e5\uff0c\u9019\u662f\u7528\u65bc\u5411\u6578\u64da\u8868\u4e2d\u63d2\u5165\u6578\u64da\u7684\u57fa\u672c SQL \u8a9e\u53e5\u4e4b\u4e00\u3002<\/p>\n<h2 id=\"insert-into-%e8%aa%9e%e5%8f%a5%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-aqJKLpkGOH\">INSERT INTO \u8a9e\u53e5\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>\u5728 PostgreSQL \u4e2d\uff0cINSERT INTO \u8a9e\u53e5\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>INSERT INTO \u8868\u540d (\u6b04\u4f4d1, \u6b04\u4f4d2, \u6b04\u4f4d3, ...)\nVALUES (\u503c1, \u503c2, \u503c3, ...);<\/code><\/pre>\n<p>\u9019\u88e1\uff0c<strong>\u8868\u540d<\/strong> \u662f\u4f60\u8981\u63d2\u5165\u6578\u64da\u7684\u76ee\u6a19\u8868\uff0c<strong>\u6b04\u4f4d<\/strong> \u662f\u4f60\u5e0c\u671b\u586b\u5145\u7684\u5217\uff0c\u800c<strong>\u503c<\/strong> \u5247\u662f\u5c0d\u61c9\u6b04\u4f4d\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%af%84%e4%be%8b%ef%bc%9a%e5%9f%ba%e6%9c%ac%e7%9a%84-insert-into-%e8%aa%9e%e5%8f%a5-aqJKLpkGOH\">\u7bc4\u4f8b\uff1a\u57fa\u672c\u7684 INSERT INTO \u8a9e\u53e5<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <strong>employees<\/strong> \u7684\u8868\uff0c\u7d50\u69cb\u5982\u4e0b\uff1a<\/p>\n<pre><code>CREATE TABLE employees (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100),\n    position VARCHAR(100),\n    salary NUMERIC\n);<\/code><\/pre>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u53e5\u5411 <strong>employees<\/strong> \u8868\u4e2d\u63d2\u5165\u4e00\u689d\u65b0\u8a18\u9304\uff1a<\/p>\n<pre><code>INSERT INTO employees (name, position, salary)\nVALUES ('\u5f35\u4e09', '\u5de5\u7a0b\u5e2b', 60000);<\/code><\/pre>\n<p>\u9019\u689d\u8a9e\u53e5\u5c07\u5728 <strong>employees<\/strong> \u8868\u4e2d\u63d2\u5165\u4e00\u540d\u540d\u70ba\u300c\u5f35\u4e09\u300d\u7684\u5de5\u7a0b\u5e2b\uff0c\u85aa\u6c34\u70ba 60000\u3002<\/p>\n<h2 id=\"%e6%8f%92%e5%85%a5%e5%a4%9a%e6%a2%9d%e8%a8%98%e9%8c%84-aqJKLpkGOH\">\u63d2\u5165\u591a\u689d\u8a18\u9304<\/h2>\n<p>PostgreSQL \u5141\u8a31\u5728\u4e00\u6b21 INSERT \u8a9e\u53e5\u4e2d\u63d2\u5165\u591a\u689d\u8a18\u9304\u3002\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>INSERT INTO \u8868\u540d (\u6b04\u4f4d1, \u6b04\u4f4d2, \u6b04\u4f4d3, ...)\nVALUES (\u503c1, \u503c2, \u503c3, ...),\n       (\u503c4, \u503c5, \u503c6, ...),\n       (\u503c7, \u503c8, \u503c9, ...);<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u4e00\u6b21\u6027\u63d2\u5165\u591a\u540d\u54e1\u5de5\u7684\u8a18\u9304\uff1a<\/p>\n<pre><code>INSERT INTO employees (name, position, salary)\nVALUES ('\u674e\u56db', '\u8a2d\u8a08\u5e2b', 55000),\n       ('\u738b\u4e94', '\u7522\u54c1\u7d93\u7406', 70000);<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8-default-%e5%80%bc-aqJKLpkGOH\">\u4f7f\u7528 DEFAULT \u503c<\/h2>\n<p>\u5982\u679c\u67d0\u500b\u6b04\u4f4d\u5728\u8868\u4e2d\u8a2d\u7f6e\u4e86 DEFAULT \u503c\uff0c\u6211\u5011\u53ef\u4ee5\u5728 INSERT \u8a9e\u53e5\u4e2d\u7701\u7565\u8a72\u6b04\u4f4d\u3002\u5047\u8a2d <strong>salary<\/strong> \u6b04\u4f4d\u7684\u9ed8\u8a8d\u503c\u70ba 50000\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u63d2\u5165\u6578\u64da\uff1a<\/p>\n<pre><code>INSERT INTO employees (name, position)\nVALUES ('\u8d99\u516d', '\u6e2c\u8a66\u5de5\u7a0b\u5e2b');<\/code><\/pre>\n<p>\u9019\u6a23\uff0c<strong>salary<\/strong> \u6b04\u4f4d\u5c07\u81ea\u52d5\u586b\u5145\u70ba 50000\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-returning-%e5%ad%90%e5%8f%a5-aqJKLpkGOH\">\u4f7f\u7528 RETURNING \u5b50\u53e5<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u53ef\u80fd\u5e0c\u671b\u5728\u63d2\u5165\u6578\u64da\u5f8c\u7acb\u5373\u7372\u53d6\u65b0\u63d2\u5165\u8a18\u9304\u7684 ID\u3002\u9019\u53ef\u4ee5\u901a\u904e\u4f7f\u7528 RETURNING \u5b50\u53e5\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>INSERT INTO employees (name, position, salary)\nVALUES ('\u9673\u4e03', '\u7cfb\u7d71\u5206\u6790\u5e2b', 65000)\nRETURNING id;<\/code><\/pre>\n<p>\u9019\u689d\u8a9e\u53e5\u5c07\u8fd4\u56de\u65b0\u63d2\u5165\u8a18\u9304\u7684 ID\uff0c\u65b9\u4fbf\u5f8c\u7e8c\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-aqJKLpkGOH\">\u7d50\u8ad6<\/h2>\n<p>PostgreSQL \u7684 INSERT INTO \u8a9e\u53e5\u662f\u4e00\u500b\u5f37\u5927\u4e14\u9748\u6d3b\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u8f15\u9b06\u5730\u5411\u6578\u64da\u8868\u4e2d\u63d2\u5165\u6578\u64da\u3002\u7121\u8ad6\u662f\u55ae\u689d\u8a18\u9304\u9084\u662f\u591a\u689d\u8a18\u9304\u7684\u63d2\u5165\uff0cPostgreSQL \u90fd\u63d0\u4f9b\u4e86\u7c21\u55ae\u660e\u77ad\u7684\u8a9e\u6cd5\u4f86\u6eff\u8db3\u9700\u6c42\u3002\u638c\u63e1\u9019\u4e9b\u57fa\u672c\u64cd\u4f5c\u5f8c\uff0c\u60a8\u5c07\u80fd\u5920\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u60a8\u7684\u6578\u64da\u5eab\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528PostgreSQL\u7684INSERT INTO\u8a9e\u53e5\uff0c\u8f15\u9b06\u5c07\u6578\u64da\u63d2\u5165\u8cc7\u6599\u8868\uff0c\u63d0\u5347\u6578\u64da\u5eab\u64cd\u4f5c\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-160645","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160645","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=160645"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160645\/revisions"}],"predecessor-version":[{"id":160646,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160645\/revisions\/160646"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}