{"id":2628,"date":"2025-03-11T23:39:29","date_gmt":"2025-03-11T15:39:29","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2628"},"modified":"2025-03-11T23:39:29","modified_gmt":"2025-03-11T15:39:29","slug":"postgresql-delete-%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/11\/postgresql-delete-%e8%af%ad%e5%8f%a5\/","title":{"rendered":"PostgreSQL DELETE \u8bed\u53e5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>DELETE<\/code> \u8bed\u53e5\u7528\u4e8e\u5220\u9664\u8868\u4e2d\u7684\u6570\u636e\u3002\u901a\u8fc7\u6307\u5b9a\u6761\u4ef6\uff0c\u53ef\u4ee5\u5220\u9664\u8868\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u5982\u679c\u4e0d\u52a0 <code>WHERE<\/code> \u5b50\u53e5\uff0c\u5c06\u5220\u9664\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\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>1. DELETE \u8bed\u6cd5<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM table_name\nWHERE condition;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>table_name<\/code>\uff1a\u4f60\u8981\u5220\u9664\u6570\u636e\u7684\u8868\u540d\u3002<\/li>\n\n\n\n<li><code>condition<\/code>\uff1a\u6761\u4ef6\uff0c\u6307\u5b9a\u8981\u5220\u9664\u7684\u8bb0\u5f55\u3002\u5982\u679c\u6ca1\u6709 <code>WHERE<\/code> \u6761\u4ef6\uff0c\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\u90fd\u5c06\u88ab\u5220\u9664\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. \u793a\u4f8b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e00\u4e2a <code>employees<\/code> \u8868\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(50),\n    age INT,\n    department VARCHAR(50),\n    salary DECIMAL(10,2)\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5411\u8868\u4e2d\u63d2\u5165\u4e00\u4e9b\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO employees (name, age, department, salary) VALUES\n('Alice', 30, 'HR', 6000),\n('Bob', 35, 'IT', 8000),\n('Charlie', 28, 'Finance', 5500),\n('David', 40, 'IT', 9500),\n('Eve', 25, 'HR', 5000);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u5220\u9664\u5355\u4e2a\u8bb0\u5f55<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2<\/strong>\uff1a\u5220\u9664 <code>Alice<\/code> \u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM employees\nWHERE name = 'Alice';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a\u8868\u4e2d <code>Alice<\/code> \u7684\u8bb0\u5f55\u88ab\u5220\u9664\uff0c\u5176\u4ed6\u5458\u5de5\u6570\u636e\u4e0d\u53d7\u5f71\u54cd\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u5220\u9664\u591a\u4e2a\u8bb0\u5f55<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2<\/strong>\uff1a\u5220\u9664\u6240\u6709 <code>HR<\/code> \u90e8\u95e8\u7684\u5458\u5de5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM employees\nWHERE department = 'HR';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a\u8868\u4e2d\u6240\u6709 <code>HR<\/code> \u90e8\u95e8\u7684\u5458\u5de5\u8bb0\u5f55\u5c06\u88ab\u5220\u9664\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u5220\u9664\u6240\u6709\u8bb0\u5f55<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2<\/strong>\uff1a\u5220\u9664\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff08\u4e0d\u52a0 <code>WHERE<\/code> \u5b50\u53e5\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a<code>employees<\/code> \u8868\u4e2d\u7684\u6240\u6709\u6570\u636e\u5c06\u88ab\u5220\u9664\uff0c\u4f46\u8868\u7ed3\u6784\u548c\u7ea6\u675f\u4ecd\u7136\u5b58\u5728\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.4 \u4f7f\u7528\u5b50\u67e5\u8be2\u5220\u9664\u8bb0\u5f55<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b50\u67e5\u8be2\u6765\u5220\u9664\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2<\/strong>\uff1a\u5220\u9664\u6240\u6709\u85aa\u6c34\u4f4e\u4e8e <code>7000<\/code> \u7684\u5458\u5de5\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM employees\nWHERE salary &lt; (SELECT AVG(salary) FROM employees);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a\u5220\u9664\u85aa\u6c34\u4f4e\u4e8e\u5458\u5de5\u85aa\u6c34\u5e73\u5747\u503c\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.5 \u5220\u9664\u8bb0\u5f55\u5e76\u8fd4\u56de\u5220\u9664\u7684\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c<code>RETURNING<\/code> \u5b50\u53e5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8fd4\u56de\u88ab\u5220\u9664\u7684\u8bb0\u5f55\uff0c\u65b9\u4fbf\u786e\u8ba4\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2<\/strong>\uff1a\u5220\u9664 <code>IT<\/code> \u90e8\u95e8\u7684\u5458\u5de5\uff0c\u5e76\u8fd4\u56de\u88ab\u5220\u9664\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM employees\nWHERE department = 'IT'\nRETURNING *;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id | name  | age | department | salary\n---+-------+-----+------------+--------\n2  | Bob   |  35 | IT         | 8000.00\n4  | David |  40 | IT         | 9500.00<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>RETURNING<\/code> \u5b50\u53e5\u8fd4\u56de\u7684\u662f\u88ab\u5220\u9664\u7684\u8bb0\u5f55\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>3. \u4f7f\u7528 DELETE \u8bed\u53e5\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u786e\u4fdd\u6761\u4ef6\u6b63\u786e<\/strong>\uff1a\u5220\u9664\u6570\u636e\u65f6\uff0c\u7279\u522b\u662f\u6ca1\u6709 <code>WHERE<\/code> \u5b50\u53e5\u65f6\uff0c\u9700\u8981\u975e\u5e38\u5c0f\u5fc3\u3002\u6ca1\u6709 <code>WHERE<\/code> \u7684 <code>DELETE<\/code> \u8bed\u53e5\u4f1a\u5220\u9664\u6240\u6709\u8bb0\u5f55\u3002\u8981\u5c0f\u5fc3\u64cd\u4f5c\uff0c\u786e\u4fdd\u6570\u636e\u4e0d\u4e22\u5931\u3002<\/li>\n\n\n\n<li><strong>\u5907\u4efd\u6570\u636e<\/strong>\uff1a\u5220\u9664\u64cd\u4f5c\u662f\u4e0d\u53ef\u6062\u590d\u7684\uff0c\u9664\u975e\u4f60\u5728\u6267\u884c\u5220\u9664\u524d\u521b\u5efa\u4e86\u5907\u4efd\u3002\u6267\u884c\u5220\u9664\u524d\u5efa\u8bae\u8fdb\u884c\u5907\u4efd\uff0c\u5c24\u5176\u662f\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u89e6\u53d1\u5668\u548c\u7ea6\u675f<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5220\u9664\u64cd\u4f5c\u53ef\u80fd\u4f1a\u89e6\u53d1\u5916\u952e\u7ea6\u675f\u6216\u89e6\u53d1\u5668\u3002\u5982\u679c\u8868\u4e4b\u95f4\u6709\u5916\u952e\u4f9d\u8d56\u5173\u7cfb\uff0c\u5220\u9664\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5931\u8d25\u6216\u5f71\u54cd\u5176\u4ed6\u8868\u7684\u6570\u636e\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>4. DELETE \u4e0e TRUNCATE<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>DELETE<\/code><\/strong>\uff1a\u5220\u9664\u8868\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u884c\uff0c\u4f46\u4e0d\u4f1a\u5f71\u54cd\u8868\u7ed3\u6784\u6216\u81ea\u589e\u5e8f\u5217\u3002<code>DELETE<\/code> \u64cd\u4f5c\u53ef\u4ee5\u5e26\u6709\u6761\u4ef6\uff0c\u5e76\u4e14\u80fd\u9010\u884c\u5220\u9664\u6570\u636e\u3002\u53ef\u4ee5\u914d\u5408 <code>RETURNING<\/code> \u5b50\u53e5\u67e5\u770b\u88ab\u5220\u9664\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><strong><code>TRUNCATE<\/code><\/strong>\uff1a\u5220\u9664\u8868\u4e2d\u6240\u6709\u6570\u636e\uff0c\u901f\u5ea6\u6bd4 <code>DELETE<\/code> \u5feb\u3002<code>TRUNCATE<\/code> \u4e0d\u4f1a\u9010\u884c\u5220\u9664\u6570\u636e\uff0c\u800c\u662f\u5c06\u8868\u6570\u636e\u76f4\u63a5\u6e05\u7a7a\u3002\u5b83\u4e0d\u80fd\u5e26\u6709 <code>WHERE<\/code> \u5b50\u53e5\uff0c\u4e5f\u4e0d\u4f1a\u89e6\u53d1\u5916\u952e\u7ea6\u675f\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>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>DELETE<\/code> \u8bed\u53e5\u7528\u4e8e\u5220\u9664\u8868\u4e2d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u5e26\u6709 <code>WHERE<\/code> \u5b50\u53e5\u6307\u5b9a\u5220\u9664\u7684\u6761\u4ef6\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6ca1\u6709 <code>WHERE<\/code> \u5b50\u53e5\uff0c<code>DELETE<\/code> \u4f1a\u5220\u9664\u6240\u6709\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>RETURNING<\/code> \u5b50\u53e5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u67e5\u770b\u5220\u9664\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>DELETE<\/code> \u65f6\u8981\u5c0f\u5fc3\uff0c\u786e\u4fdd\u6761\u4ef6\u6b63\u786e\uff0c\u4ee5\u907f\u514d\u8bef\u5220\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>DELETE<\/code> \u4e0d\u5f71\u54cd\u8868\u7684\u7ed3\u6784\u548c\u7ea6\u675f\uff0c\u53ef\u4ee5\u5728\u5220\u9664\u6570\u636e\u540e\u7ee7\u7eed\u4f7f\u7528\u8868\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5408\u7406\u4f7f\u7528 <code>DELETE<\/code> \u8bed\u53e5\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u5220\u9664\u64cd\u4f5c\u3002\u66f4\u591a\u8be6\u7ec6\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DELETE \u8bed\u53e5\u7528\u4e8e\u5220\u9664\u8868\u4e2d\u7684\u6570\u636e\u3002\u901a\u8fc7\u6307\u5b9a\u6761\u4ef6\uff0c\u53ef\u4ee5\u5220\u9664\u8868\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u5982\u679c\u4e0d\u52a0 WHERE \u5b50\u53e5\uff0c\u5c06 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2629,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2628","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\/2628","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=2628"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2628\/revisions"}],"predecessor-version":[{"id":2630,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2628\/revisions\/2630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2629"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}