{"id":1625,"date":"2024-05-08T15:53:03","date_gmt":"2024-05-08T07:53:03","guid":{"rendered":"http:\/\/oneai.eu.org\/?p=1625"},"modified":"2024-05-08T15:53:03","modified_gmt":"2024-05-08T07:53:03","slug":"postgres-db%e5%b8%b8%e8%a7%84%e6%93%8d%e4%bd%9csql","status":"publish","type":"post","link":"https:\/\/oneai.eu.org\/?p=1625","title":{"rendered":"postgres db\u5e38\u89c4\u64cd\u4f5cSQL"},"content":{"rendered":"<pre><code class=\"language-sql\">\u4e00\u3001\u8868\u7684\u5b9a\u4e49\uff1a\n\n    \u5bf9\u4e8e\u4efb\u4f55\u4e00\u79cd\u5173\u7cfb\u578b\u6570\u636e\u5e93\u800c\u8a00\uff0c\u8868\u90fd\u662f\u6570\u636e\u5b58\u50a8\u7684\u6700\u6838\u5fc3\u3001\u6700\u57fa\u7840\u7684\u5bf9\u8c61\u5355\u5143\u3002\u73b0\u5728\u5c31\u8ba9\u6211\u4eec\u4ece\u8fd9\u91cc\u8d77\u6b65\u5427\u3002\n    1. \u521b\u5efa\u8868\uff1a\n    CREATE TABLE products (\n        product_no integer,\n        name text,\n        price numeric\n    );\n\n    2. \u5220\u9664\u8868\uff1a\n    DROP TABLE products;\n\n    3. \u521b\u5efa\u5e26\u6709\u7f3a\u7701\u503c\u7684\u8868\uff1a\n    CREATE TABLE products (\n        product_no integer,\n        name text,\n        price numeric DEFAULT 9.99 --DEFAULT\u662f\u5173\u952e\u5b57\uff0c\u5176\u540e\u7684\u6570\u503c9.99\u662f\u5b57\u6bb5price\u7684\u9ed8\u8ba4\u503c\u3002\n    );\n\n    CREATE TABLE products (\n        product_no SERIAL,            --SERIAL\u7c7b\u578b\u7684\u5b57\u6bb5\u8868\u793a\u8be5\u5b57\u6bb5\u4e3a\u81ea\u589e\u5b57\u6bb5\uff0c\u5b8c\u5168\u7b49\u540c\u4e8eOracle\u4e2d\u7684Sequence\u3002\n        name text,\n        price numeric DEFAULT 9.99\n    );\n    \u8f93\u51fa\u4e3a\uff1a\n    NOTICE:  CREATE TABLE will create implicit sequence &quot;products_product_no_seq&quot; for serial column &quot;products.product_no&quot;\n\n    4. \u7ea6\u675f\uff1a\n    \u68c0\u67e5\u7ea6\u675f\u662f\u8868\u4e2d\u6700\u4e3a\u5e38\u89c1\u7684\u7ea6\u675f\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u4f60\u58f0\u660e\u5728\u67d0\u4e2a\u5b57\u6bb5\u91cc\u7684\u6570\u503c\u5fc5\u987b\u6ee1\u8db3\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u58f0\u660e\u8868\u7ea7\u522b\u7684\u68c0\u67e5\u7ea6\u675f\u3002\n    CREATE TABLE products (\n        product_no integer,\n        name text,\n        --price\u5b57\u6bb5\u7684\u503c\u5fc5\u987b\u5927\u4e8e0\uff0c\u5426\u5219\u5728\u63d2\u5165\u6216\u4fee\u6539\u8be5\u5b57\u6bb5\u503c\u662f\uff0c\u5c06\u5f15\u53d1\u8fdd\u89c4\u9519\u8bef\u3002\u8fd8\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u8be5\u68c0\u67e5\u7ea6\u675f\n        --\u662f\u533f\u540d\u7ea6\u675f\uff0c\u5373\u5728\u8868\u5b9a\u4e49\u65f6\u6ca1\u6709\u663e\u793a\u547d\u540d\u8be5\u7ea6\u675f\uff0c\u8fd9\u6837PostgreSQL\u5c06\u4f1a\u6839\u636e\u5f53\u524d\u7684\u8868\u540d\u3001\u5b57\u6bb5\u540d\u548c\u7ea6\u675f\u7c7b\u578b\uff0c\n        --\u4e3a\u8be5\u7ea6\u675f\u81ea\u52a8\u547d\u540d\uff0c\u5982\uff1aproducts_price_check\u3002\n        price numeric CHECK (price &gt; 0) \n    );\n\n    CREATE TABLE products (\n        product_no integer,\n        name text,\n        --\u8be5\u5b57\u6bb5\u7684\u68c0\u67e5\u7ea6\u675f\u88ab\u663e\u793a\u547d\u540d\u4e3apositive_price\u3002\u8fd9\u6837\u505a\u597d\u5904\u5728\u4e8e\u4eca\u540e\u7ef4\u62a4\u8be5\u7ea6\u675f\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u8be5\u540d\u8fdb\u884c\u76f4\u63a5\u64cd\u4f5c\u3002\n        price numeric CONSTRAINT positive_price CHECK (price &gt; 0) \n    );\n    \u4e0b\u9762\u7684\u7ea6\u675f\u662f\u975e\u7a7a\u7ea6\u675f\uff0c\u5373\u7ea6\u675f\u7684\u5b57\u6bb5\u4e0d\u80fd\u63d2\u5165\u7a7a\u503c\uff0c\u6216\u8005\u662f\u5c06\u5df2\u6709\u6570\u636e\u66f4\u65b0\u4e3a\u7a7a\u503c\u3002\n    CREATE TABLE products (\n        product_no integer NOT NULL,\n        name text NOT NULL,\n        price numeric\n    );\n    \u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u4e2d\u5b58\u5728\u591a\u4e2a\u7ea6\u675f\uff0c\u5728\u5b9a\u4e49\u65f6\u53ef\u4ee5\u4e0d\u7528\u8003\u8651\u7ea6\u675f\u7684\u58f0\u660e\u987a\u5e8f\u3002\n    CREATE TABLE products (\n        product_no integer NOT NULL,\n        name text NOT NULL,\n        price numeric NOT NULL CHECK (price &gt; 0)\n    );\n    \u552f\u4e00\u6027\u7ea6\u675f\uff0c\u5373\u6307\u5b9a\u7684\u5b57\u6bb5\u4e0d\u80fd\u63d2\u5165\u91cd\u590d\u503c\uff0c\u6216\u8005\u662f\u5c06\u67d0\u4e00\u8bb0\u5f55\u7684\u503c\u66f4\u65b0\u4e3a\u5f53\u524d\u8868\u4e2d\u7684\u5df2\u6709\u503c\u3002\n    CREATE TABLE products (\n        product_no integer UNIQUE,\n        name text,\n        price numeric\n    );\n\n    CREATE TABLE products (\n        product_no integer,\n        name text,\n        price numeric,\n        UNIQUE (product_no)\n    );\n    \u4e3a\u8868\u4e2d\u7684\u591a\u4e2a\u5b57\u6bb5\u5b9a\u4e49\u8054\u5408\u552f\u4e00\u6027\u3002\n    CREATE TABLE example (\n        a integer,\n        b integer,\n        c integer,\n        UNIQUE (a, c)\n    );\n    \u4e3a\u552f\u4e00\u6027\u7ea6\u675f\u547d\u540d\u3002\n    CREATE TABLE products (\n        product_no integer CONSTRAINT must_be_different UNIQUE,\n        name text,\n        price numeric\n    );\n    \u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u7a7a\u503c(NULL)\u4e4b\u95f4\u88ab\u89c6\u4e3a\u4e0d\u76f8\u7b49\u7684\u6570\u636e\uff0c\u56e0\u6b64\u5bf9\u4e8e\u67d0\u4e00\u552f\u4e00\u6027\u5b57\u6bb5\uff0c\u53ef\u4ee5\u591a\u6b21\u63d2\u5165\u7a7a\u503c\u3002\u7136\u800c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e00\u89c4\u5219\u5e76\u4e0d\u662f\u88ab\u6240\u6709\u6570\u636e\u5e93\u90fd\u9075\u5b88\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u6570\u636e\u5e93\u79fb\u690d\u65f6\u53ef\u80fd\u4f1a\u9020\u6210\u4e00\u5b9a\u7684\u9ebb\u70e6\u3002\n\n    5. \u4e3b\u952e\u548c\u5916\u952e\uff1a\n    \u4ece\u6280\u672f\u4e0a\u6765\u8bb2\uff0c\u4e3b\u952e\u7ea6\u675f\u53ea\u662f\u552f\u4e00\u7ea6\u675f\u548c\u975e\u7a7a\u7ea6\u675f\u7684\u7ec4\u5408\u3002\n    CREATE TABLE products (\n        product_no integer PRIMARY KEY,  --\u5b57\u6bb5product_no\u88ab\u5b9a\u4e49\u4e3a\u8be5\u8868\u7684\u552f\u4e00\u4e3b\u952e\u3002\n        name text,\n        price numeric\n    );\n    \u548c\u552f\u4e00\u6027\u7ea6\u675f\u4e00\u6837\uff0c\u4e3b\u952e\u53ef\u4ee5\u540c\u65f6\u4f5c\u7528\u4e8e\u591a\u4e2a\u5b57\u6bb5\uff0c\u5f62\u6210\u8054\u5408\u4e3b\u952e\uff1a\n    CREATE TABLE example (\n        a integer,\n        b integer,\n        c integer,\n        PRIMARY KEY (b, c)\n    );\n    \u5916\u952e\u7ea6\u675f\u58f0\u660e\u4e00\u4e2a\u5b57\u6bb5\uff08\u6216\u8005\u4e00\u7ec4\u5b57\u6bb5\uff09\u7684\u6570\u503c\u5fc5\u987b\u5339\u914d\u53e6\u5916\u4e00\u4e2a\u8868\u4e2d\u67d0\u4e9b\u884c\u51fa\u73b0\u7684\u6570\u503c\u3002 \u6211\u4eec\u628a\u8fd9\u4e2a\u884c\u4e3a\u79f0\u505a\u4e24\u4e2a\u76f8\u5173\u8868\u4e4b\u95f4\u7684\u53c2\u8003\u5b8c\u6574\u6027\u3002\n    CREATE TABLE orders (\n        order_id integer PRIMARY KEY, --\u8be5\u8868\u4e5f\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u4e3b\u952e\u3002\n        --\u8be5\u8868\u7684product_no\u5b57\u6bb5\u4e3a\u4e0a\u9762products\u8868\u4e3b\u952e(product_no)\u7684\u5916\u952e\u3002\n        product_no integer REFERENCES products(product_no), \n        quantity integer\n    );\n\n    CREATE TABLE t1 (\n        a integer PRIMARY KEY,\n        b integer,\n        c integer,\n        --\u8be5\u5916\u952e\u7684\u5b57\u6bb5\u6570\u91cf\u548c\u88ab\u5f15\u7528\u8868\u4e2d\u4e3b\u952e\u7684\u6570\u91cf\u5fc5\u987b\u4fdd\u6301\u4e00\u81f4\u3002\n        FOREIGN KEY (b, c) REFERENCES example (b, c)\n    );   \n    \u5f53\u591a\u4e2a\u8868\u4e4b\u95f4\u5b58\u5728\u4e86\u4e3b\u5916\u952e\u7684\u53c2\u8003\u6027\u7ea6\u675f\u5173\u7cfb\u65f6\uff0c\u5982\u679c\u60f3\u5220\u9664\u88ab\u5e94\u7528\u8868(\u4e3b\u952e\u8868)\u4e2d\u7684\u67d0\u884c\u8bb0\u5f55\uff0c\u7531\u4e8e\u8be5\u884c\u8bb0\u5f55\u7684\u4e3b\u952e\u5b57\u6bb5\u503c\u53ef\u80fd\u6b63\u5728\u88ab\u5176\u5f15\u7528\u8868(\u5916\u952e\u8868)\u4e2d\u67d0\u6761\u8bb0\u5f55\u6240\u5173\u8054\uff0c\u6240\u4ee5\u5220\u9664\u64cd\u4f5c\u5c06\u4f1a\u5931\u8d25\u3002\u5982\u679c\u60f3\u5b8c\u6210\u6b64\u64cd\u4f5c\uff0c\u4e00\u4e2a\u663e\u800c\u6613\u89c1\u7684\u65b9\u6cd5\u662f\u5148\u5220\u9664\u5f15\u7528\u8868\u4e2d\u548c\u8be5\u8bb0\u5f55\u5173\u8054\u7684\u884c\uff0c\u4e4b\u540e\u518d\u5220\u9664\u88ab\u5f15\u7528\u8868\u4e2d\u7684\u8be5\u884c\u8bb0\u5f55\u3002\u7136\u800c\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0cPostgreSQL\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u66f4\u4e3a\u65b9\u4fbf\u7684\u65b9\u5f0f\u5b8c\u6210\u6b64\u7c7b\u64cd\u4f5c\u3002\n    CREATE TABLE products (\n        product_no integer PRIMARY KEY,\n        name text,\n        price numeric\n    );\n\n    CREATE TABLE orders (\n        order_id integer PRIMARY KEY,\n        shipping_address text\n    );\n\n    CREATE TABLE order_items (\n        product_no integer REFERENCES products ON DELETE RESTRICT, --\u9650\u5236\u9009\u9879\n        order_id integer REFERENCES orders ON DELETE CASCADE, --\u7ea7\u8054\u5220\u9664\u9009\u9879\n        quantity integer,\n        PRIMARY KEY (product_no, order_id)\n    );\n    \u9650\u5236\u548c\u7ea7\u8054\u5220\u9664\u662f\u4e24\u79cd\u6700\u5e38\u89c1\u7684\u9009\u9879\u3002RESTRICT \u7981\u6b62\u5220\u9664\u88ab\u5f15\u7528\u7684\u884c\u3002 NO ACTION \u7684\u610f\u601d\u662f\u5982\u679c\u5728\u68c0\u67e5\u7ea6\u675f\u7684\u65f6\u5019\uff0c\u5982\u679c\u8fd8\u5b58\u5728\u4efb\u4f55\u5f15\u7528\u884c\uff0c\u5219\u629b\u51fa\u9519\u8bef\uff1b \u5982\u679c\u4f60\u4e0d\u58f0\u660e\u4efb\u4f55\u4e1c\u897f\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u7f3a\u7701\u7684\u884c\u4e3a\u3002(\u8fd9\u4e24\u4e2a\u9009\u62e9\u7684\u5b9e\u9645\u533a\u522b\u662f\uff0cNO ACTION \u5141\u8bb8\u7ea6\u675f\u68c0\u67e5\u63a8\u8fdf\u5230\u4e8b\u52a1\u7684\u665a\u4e9b\u65f6\u5019\uff0c\u800c RESTRICT \u4e0d\u884c\u3002) CASCADE\u58f0\u660e\u5728\u5220\u9664\u4e00\u4e2a\u88ab\u5f15\u7528\u7684\u884c\u7684\u65f6\u5019\uff0c\u5f15\u7528\u5b83\u7684\u884c\u4e5f\u4f1a\u88ab\u81ea\u52a8\u5220\u9664\u6389\u3002 \u5728\u5916\u952e\u5b57\u6bb5\u4e0a\u7684\u52a8\u4f5c\u8fd8\u6709\u4e24\u4e2a\u9009\u9879\uff1a SET NULL \u548c SET DEFAULT\u3002 \u8fd9\u6837\u4f1a\u5bfc\u81f4\u5728\u88ab\u5f15\u7528\u884c\u5220\u9664\u7684\u65f6\u5019\uff0c\u5f15\u7528\u5b83\u4eec\u7684\u5b57\u6bb5\u5206\u522b\u8bbe\u7f6e\u4e3a\u7a7a\u6216\u8005\u7f3a\u7701\u503c\u3002 \u8bf7\u6ce8\u610f\u8fd9\u4e9b\u9009\u9879\u5e76\u4e0d\u80fd\u8ba9\u4f60\u9003\u8131\u88ab\u89c2\u5bdf\u548c\u7ea6\u675f\u7684\u5883\u5730\u3002\u6bd4\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u52a8\u4f5c\u58f0\u660e SET DEFAULT\uff0c\u4f46\u662f\u7f3a\u7701\u503c\u5e76\u4e0d\u80fd\u6ee1\u8db3\u5916\u952e\uff0c\u90a3\u4e48\u52a8\u4f5c\u5c31\u4f1a\u5931\u8d25\u3002\u7c7b\u4f3cON DELETE\uff0c\u8fd8\u6709ON UPDATE \u9009\u9879\uff0c\u5b83\u662f\u5728\u88ab\u5f15\u7528\u5b57\u6bb5\u4fee\u6539(\u66f4\u65b0)\u7684\u65f6\u5019\u8c03\u7528\u7684\u3002\u53ef\u7528\u7684\u52a8\u4f5c\u662f\u4e00\u6837\u7684\u3002\n\n\u4e8c\u3001\u7cfb\u7edf\u5b57\u6bb5\uff1a\n\n    PostgreSQL\u7684\u6bcf\u4e2a\u6570\u636e\u8868\u4e2d\u90fd\u5305\u542b\u51e0\u4e2a\u9690\u542b\u5b9a\u4e49\u7684\u7cfb\u7edf\u5b57\u6bb5\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u540d\u5b57\u4e0d\u80fd\u7528\u4e8e\u7528\u6237\u5b9a\u4e49\u7684\u5b57\u6bb5\u540d\u3002\u8fd9\u4e9b\u7cfb\u7edf\u5b57\u6bb5\u7684\u529f\u80fd\u6709\u4e9b\u7c7b\u4f3c\u4e8eOracle\u4e2d\u7684rownum\u548crowid\u7b49\u3002\n    oid: \u884c\u7684\u5bf9\u8c61\u6807\u8bc6\u7b26(\u5bf9\u8c61ID)\u3002\u8fd9\u4e2a\u5b57\u6bb5\u53ea\u6709\u5728\u521b\u5efa\u8868\u7684\u65f6\u5019\u4f7f\u7528\u4e86WITH OIDS\uff0c\u6216\u8005\u662f\u8bbe\u7f6e\u4e86\u914d\u7f6e\u53c2\u6570default_with_oids\u65f6\u51fa\u73b0\u3002\u8fd9\u4e2a\u5b57\u6bb5\u7684\u7c7b\u578b\u662foid(\u548c\u5b57\u6bb5\u540c\u540d)\u3002 \n    tableoid: \u5305\u542b\u672c\u884c\u7684\u8868\u7684OID\u3002\u8fd9\u4e2a\u5b57\u6bb5\u5bf9\u90a3\u4e9b\u4ece\u7ee7\u627f\u5c42\u6b21\u4e2d\u9009\u53d6\u7684\u67e5\u8be2\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5982\u679c\u6ca1\u6709\u5b83\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u5f88\u96be\u8bf4\u660e\u4e00\u884c\u6765\u81ea\u54ea\u4e2a\u72ec\u7acb\u7684\u8868\u3002tableoid\u53ef\u4ee5\u548cpg_class\u7684oid\u5b57\u6bb5\u8fde\u63a5\u8d77\u6765\u83b7\u53d6\u8868\u540d\u5b57\u3002 \n    xmin: \u63d2\u5165\u8be5\u884c\u7248\u672c\u7684\u4e8b\u52a1\u7684\u6807\u8bc6(\u4e8b\u52a1ID)\u3002\n    cmin: \u5728\u63d2\u5165\u4e8b\u52a1\u5185\u90e8\u7684\u547d\u4ee4\u6807\u8bc6(\u4ece\u96f6\u5f00\u59cb)\u3002 \n    xmax: \u5220\u9664\u4e8b\u52a1\u7684\u6807\u8bc6(\u4e8b\u52a1ID)\uff0c\u5982\u679c\u4e0d\u662f\u88ab\u5220\u9664\u7684\u884c\u7248\u672c\uff0c\u90a3\u4e48\u662f\u96f6\u3002\n    cmax: \u5728\u5220\u9664\u4e8b\u52a1\u5185\u90e8\u7684\u547d\u4ee4\u6807\u8bc6\u7b26\uff0c\u6216\u8005\u662f\u96f6\u3002 \n    ctid: \u4e00\u4e2a\u884c\u7248\u672c\u5728\u5b83\u6240\u5904\u7684\u8868\u5185\u7684\u7269\u7406\u4f4d\u7f6e\u3002\u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1ctid\u53ef\u4ee5\u7528\u4e8e\u975e\u5e38\u5feb\u901f\u5730\u5b9a\u4f4d\u884c\u7248\u672c\uff0c\u4f46\u6bcf\u6b21VACUUM FULL\u4e4b\u540e\uff0c\u4e00\u4e2a\u884c\u7684ctid\u90fd\u4f1a\u88ab\u66f4\u65b0\u6216\u8005\u79fb\u52a8\u3002\u56e0\u6b64ctid\u662f\u4e0d\u80fd\u4f5c\u4e3a\u957f\u671f\u7684\u884c\u6807\u8bc6\u7b26\u7684\u3002    \n    OID\u662f32\u4f4d\u7684\u91cf\uff0c\u662f\u5728\u540c\u4e00\u4e2a\u96c6\u7fa4\u5185\u901a\u7528\u7684\u8ba1\u6570\u5668\u4e0a\u8d4b\u503c\u7684\u3002\u5bf9\u4e8e\u4e00\u4e2a\u5927\u578b\u6216\u8005\u957f\u65f6\u95f4\u4f7f\u7528\u7684\u6570\u636e\u5e93\uff0c\u8fd9\u4e2a\u8ba1\u6570\u5668\u662f\u6709\u53ef\u80fd\u91cd\u53e0\u7684\u3002\u56e0\u6b64\uff0c\u5047\u8bbeOID\u662f\u552f\u4e00\u7684\u662f\u975e\u5e38\u9519\u8bef\u7684\uff0c\u9664\u975e\u4f60\u81ea\u5df1\u91c7\u53d6\u4e86\u63aa\u65bd\u6765\u4fdd\u8bc1\u5b83\u4eec\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u4f60\u9700\u8981\u6807\u8bc6\u8868\u4e2d\u7684\u884c\uff0c\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528\u5e8f\u5217\u53f7\u751f\u6210\u5668\u3002     \n\n\u4e09\u3001\u8868\u7684\u4fee\u6539\uff1a\n\n    1. \u589e\u52a0\u5b57\u6bb5\uff1a\n    ALTER TABLE products ADD COLUMN description text;\n    \u65b0\u589e\u7684\u5b57\u6bb5\u5bf9\u4e8e\u8868\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u884c\u800c\u8a00\u6700\u521d\u5c06\u5148\u586b\u5145\u6240\u7ed9\u51fa\u7684\u7f3a\u7701\u503c(\u5982\u679c\u4f60\u6ca1\u6709\u58f0\u660eDEFAULT\u5b50\u53e5\uff0c\u90a3\u4e48\u7f3a\u7701\u662f\u7a7a\u503c)\u3002\n    \u5728\u65b0\u589e\u5b57\u6bb5\u65f6\uff0c\u53ef\u4ee5\u540c\u65f6\u7ed9\u8be5\u5b57\u6bb5\u6307\u5b9a\u7ea6\u675f\u3002\n    ALTER TABLE products ADD COLUMN description text CHECK(description &lt;&gt; &#039;&#039;);\n\n    2. \u5220\u9664\u5b57\u6bb5\uff1a\n    ALTER TABLE products DROP COLUMN description;\n    \u5982\u679c\u8be5\u8868\u4e3a\u88ab\u5f15\u7528\u8868\uff0c\u8be5\u5b57\u6bb5\u4e3a\u88ab\u5f15\u7528\u5b57\u6bb5\uff0c\u90a3\u4e48\u4e0a\u9762\u7684\u5220\u9664\u64cd\u4f5c\u5c06\u4f1a\u5931\u8d25\u3002\u5982\u679c\u8981\u60f3\u5728\u5220\u9664\u88ab\u5f15\u7528\u5b57\u6bb5\u7684\u540c\u65f6\u7ea7\u8054\u7684\u5220\u9664\u5176\u6240\u6709\u5f15\u7528\u5b57\u6bb5\uff0c\u53ef\u4ee5\u91c7\u7528\u4e0b\u9762\u7684\u8bed\u6cd5\u5f62\u5f0f\u3002\n    ALTER TABLE products DROP COLUMN description CASCADE;\n\n    3. \u589e\u52a0\u7ea6\u675f\uff1a\n    ALTER TABLE products ADD CHECK(name &lt;&gt; &#039;&#039;);  --\u589e\u52a0\u4e00\u4e2a\u8868\u7ea7\u7ea6\u675f\n    ALTER TABLE products ADD CONSTRAINT some_name UNIQUE(product_no);--\u589e\u52a0\u547d\u540d\u7684\u552f\u4e00\u6027\u7ea6\u675f\u3002\n    ALTER TABLE products ADD FOREIGN KEY(pdt_grp_id) REFERENCES pdt_grps; --\u589e\u52a0\u5916\u952e\u7ea6\u675f\u3002\n    ALTER TABLE products ALTER COLUMN product_no SET NOT NULL; --\u589e\u52a0\u4e00\u4e2a\u975e\u7a7a\u7ea6\u675f\u3002\n\n    4. \u5220\u9664\u7ea6\u675f\uff1a\n    ALTER TABLE products DROP CONSTRAINT some_name;\n    \u5bf9\u4e8e\u663e\u793a\u547d\u540d\u7684\u7ea6\u675f\uff0c\u53ef\u4ee5\u6839\u636e\u5176\u540d\u79f0\u76f4\u63a5\u5220\u9664\uff0c\u5bf9\u4e8e\u9690\u5f0f\u81ea\u52a8\u547d\u540d\u7684\u7ea6\u675f\uff0c\u53ef\u4ee5\u901a\u8fc7psql\u7684\\d tablename\u6765\u83b7\u53d6\u8be5\u7ea6\u675f\u7684\u540d\u5b57\u3002\u548c\u5220\u9664\u5b57\u6bb5\u4e00\u6837\uff0c\u5982\u679c\u4f60\u60f3\u5220\u9664\u6709\u7740\u88ab\u4f9d\u8d56\u5173\u7cfb\u5730\u7ea6\u675f\uff0c\u4f60\u9700\u8981\u7528CASCADE\u3002\u4e00\u4e2a\u4f8b\u5b50\u662f\u67d0\u4e2a\u5916\u952e\u7ea6\u675f\u4f9d\u8d56\u88ab\u5f15\u7528\u5b57\u6bb5\u4e0a\u7684\u552f\u4e00\u7ea6\u675f\u6216\u8005\u4e3b\u952e\u7ea6\u675f\u3002\u5982\uff1a\n    MyTest=# \\d products\n         Table &quot;public.products&quot;\n       Column     |  Type   | Modifiers\n     ------------+---------+-----------\n     product_no | integer |\n     name          | text    |\n     price           | numeric |\n     Check constraints:\n        &quot;positive_price&quot; CHECK (price &gt; 0::numeric)\n    \u548c\u5176\u4ed6\u7ea6\u675f\u4e0d\u540c\u7684\u662f\uff0c\u975e\u7a7a\u7ea6\u675f\u6ca1\u6709\u540d\u5b57\uff0c\u56e0\u6b64\u53ea\u80fd\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u5220\u9664\uff1a\n    ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;\n\n    5. \u6539\u53d8\u5b57\u6bb5\u7684\u7f3a\u7701\u503c:\n    \u5728\u4e3a\u5df2\u6709\u5b57\u6bb5\u6dfb\u52a0\u7f3a\u7701\u503c\u65f6\uff0c\u4e0d\u4f1a\u5f71\u54cd\u4efb\u4f55\u8868\u4e2d\u73b0\u6709\u7684\u6570\u636e\u884c\uff0c \u5b83\u53ea\u662f\u4e3a\u5c06\u6765INSERT\u547d\u4ee4\u6539\u53d8\u7f3a\u7701\u503c\u3002\n    ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;\n    \u4e0b\u9762\u4e3a\u5220\u9664\u7f3a\u7701\u503c\uff1a\n    ALTER TABLE products ALTER COLUMN price DROP DEFAULT\n\n    6. \u4fee\u6539\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\uff1a\n    \u53ea\u6709\u5728\u5b57\u6bb5\u91cc\u73b0\u6709\u7684\u6bcf\u4e2a\u9879\u90fd\u53ef\u4ee5\u7528\u4e00\u4e2a\u9690\u542b\u7684\u7c7b\u578b\u8f6c\u6362\u8f6c\u6362\u6210\u65b0\u7684\u7c7b\u578b\u65f6\u624d\u53ef\u80fd\u6210\u529f\u3002\u6bd4\u5982\u5f53\u524d\u7684\u6570\u636e\u90fd\u662f\u6574\u578b\uff0c\u800c\u8f6c\u6362\u7684\u76ee\u6807\u7c7b\u578b\u4e3anumeric\u6216varchar\uff0c\u8fd9\u6837\u7684\u8f6c\u6362\u4e00\u822c\u90fd\u53ef\u4ee5\u6210\u529f\u3002\u4e0e\u6b64\u540c\u65f6\uff0cPostgreSQL\u8fd8\u5c06\u8bd5\u56fe\u628a\u5b57\u6bb5\u7684\u7f3a\u7701\u503c\uff08\u5982\u679c\u5b58\u5728\uff09\u8f6c\u6362\u6210\u65b0\u7684\u7c7b\u578b\uff0c \u8fd8\u6709\u6d89\u53ca\u8be5\u5b57\u6bb5\u7684\u4efb\u4f55\u7ea6\u675f\u3002\u4f46\u662f\u8fd9\u4e9b\u8f6c\u6362\u53ef\u80fd\u5931\u8d25\uff0c\u6216\u8005\u53ef\u80fd\u751f\u6210\u5947\u602a\u7684\u7ed3\u679c\u3002 \u5728\u4fee\u6539\u67d0\u5b57\u6bb5\u7c7b\u578b\u4e4b\u524d\uff0c\u4f60\u6700\u597d\u5220\u9664\u90a3\u4e9b\u7ea6\u675f\uff0c\u7136\u540e\u518d\u628a\u81ea\u5df1\u624b\u5de5\u4fee\u6539\u8fc7\u7684\u6dfb\u52a0\u4e0a\u53bb\u3002 \n    ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);\n\n    7. \u4fee\u6539\u5b57\u6bb5\u540d\uff1a\n    ALTER TABLE products RENAME COLUMN product_no TO product_number;\n\n    8. \u4fee\u6539\u8868\u540d\uff1a\n    ALTER TABLE products RENAME TO items;\n\n\u56db\u3001\u6743\u9650\uff1a\n\n    \u53ea\u6709\u8868\u7684\u6240\u6709\u8005\u624d\u80fd\u4fee\u6539\u6216\u8005\u5220\u9664\u8868\u7684\u6743\u9650\u3002\u8981\u8d4b\u4e88\u4e00\u4e2a\u6743\u9650\uff0c\u6211\u4eec\u4f7f\u7528GRANT\u547d\u4ee4\uff0c\u8981\u64a4\u9500\u4e00\u4e2a\u6743\u9650\uff0c\u4f7f\u7528REVOKE\u547d\u4ee4\u3002\n    \u9700\u8981\u6307\u51fa\u7684\u662f\uff0cPUBLIC\u662f\u7279\u6b8a&quot;\u7528\u6237&quot;\u53ef\u4ee5\u7528\u4e8e\u5c06\u6743\u9650\u8d4b\u4e88\u7cfb\u7edf\u4e2d\u7684\u6bcf\u4e00\u4e2a\u7528\u6237\u3002\u5728\u58f0\u660e\u6743\u9650\u7684\u4f4d\u7f6e\u5199ALL\u5219\u5c06\u6240\u6709\u7684\u4e0e\u8be5\u5bf9\u8c61\u7c7b\u578b\u76f8\u5173\u7684\u6743\u9650\u90fd\u8d4b\u4e88\u51fa\u53bb\u3002\n    GRANT UPDATE ON table_name TO user;  --\u5c06\u8868\u7684\u66f4\u65b0\u6743\u9650\u8d4b\u4e88\u6307\u5b9a\u7684user\u3002\n    GRANT SELECT ON table_name TO GROUP group; --\u5c06\u8868\u7684select\u6743\u9650\u8d4b\u4e88\u6307\u5b9a\u7684\u7ec4\u3002\n    REVOKE ALL ON table_name FROM PUBLIC; --\u5c06\u8868\u7684\u6240\u6709\u6743\u9650\u4ecePublic\u64a4\u9500\u3002\n    \u6700\u521d\uff0c\u53ea\u6709\u5bf9\u8c61\u6240\u6709\u8005(\u6216\u8005\u8d85\u7ea7\u7528\u6237)\u53ef\u4ee5\u8d4b\u4e88\u6216\u8005\u64a4\u9500\u5bf9\u8c61\u7684\u6743\u9650\u3002\u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u8d4b\u4e88\u4e00\u4e2a&quot;with grant option&quot;\u6743\u9650\uff0c\u8fd9\u6837\u5c31\u7ed9\u63a5\u53d7\u6743\u9650\u7684\u4eba\u4ee5\u6388\u4e88\u8be5\u6743\u9650\u7ed9\u5176\u5b83\u4eba\u7684\u6743\u9650\u3002\u5982\u679c\u6388\u4e88\u9009\u9879\u540e\u6765\u88ab\u64a4\u9500\uff0c\u90a3\u4e48\u6240\u6709\u90a3\u4e9b\u4ece\u8fd9\u4e2a\u63a5\u53d7\u8005\u63a5\u53d7\u4e86\u6743\u9650\u7684\u7528\u6237(\u76f4\u63a5\u6216\u8005\u901a\u8fc7\u7ea7\u8fde\u7684\u6388\u6743)\u90fd\u5c06\u5931\u53bb\u8be5\u6743\u9650\u3002\n\n\u6570\u636e\u5e93\u590d\u5236\uff1a\nsu - postgres ;psql\n\n\u5148\u628a\u5f53\u524d\u5e93rename :\nALTER DATABASE abc RENAME TO cba;\n\u4ee5\u5df2\u7ecf\u6709\u7684\u6570\u636e\u5e93\uff0c\u4f5c\u4e3a\u6a21\u677f\uff0c\u7136\u540e\u8fd9\u77ac\u95f4\u5c31\u521b\u5efa\u597d\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93.\nCREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;\n\n-------\n\u521b\u5efa\u7528\u6237\u53ca\u89d2\u8272\uff1a\ncreate user ocspi with password &#039;ocspi@test&#039;;\n\u7ed9\u521b\u5efa\u7684\u8d4b\u6743\uff1a postgres# GRANT ALL PRIVILEGES ON DATABASE xxxx2 to ocspi;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u8868\u7684\u5b9a\u4e49\uff1a \u5bf9\u4e8e\u4efb\u4f55\u4e00\u79cd\u5173\u7cfb\u578b\u6570\u636e\u5e93\u800c\u8a00\uff0c\u8868\u90fd\u662f\u6570\u636e\u5b58\u50a8\u7684\u6700\u6838\u5fc3\u3001\u6700\u57fa\u7840\u7684\u5bf9\u8c61\u5355\u5143\u3002\u73b0\u5728\u5c31\u8ba9\u6211\u4eec\u4ece\u8fd9\u91cc\u8d77\u6b65\u5427\u3002 1. \u521b\u5efa\u8868\uff1a CREATE TABLE products ( product_no integer, name text, price numeric ); 2. \u5220\u9664\u8868\uff1a DROP TABLE products; 3. \u521b\u5efa\u5e26\u6709\u7f3a\u7701\u503c\u7684\u8868\uff1a CREATE TABLE products ( product_no integer, name text, price numeric DEFAULT 9.99 &#8211;DEFAULT\u662f\u5173\u952e\u5b57\uff0c\u5176\u540e\u7684\u6570\u503c9.99\u662f\u5b57\u6bb5price\u7684\u9ed8\u8ba4\u503c\u3002 ); CREATE TABLE products ( product_no SERIAL, &#8211;SERIAL\u7c7b\u578b\u7684\u5b57\u6bb5\u8868\u793a\u8be5\u5b57\u6bb5\u4e3a\u81ea\u589e\u5b57\u6bb5\uff0c\u5b8c\u5168\u7b49\u540c\u4e8eOracle\u4e2d\u7684Sequence\u3002 name text, price numeric DEFAULT 9.99 ); \u8f93\u51fa\u4e3a\uff1a NOTICE: CREATE TABLE will create implicit sequence &quot;products_product_no_seq&quot; for serial column &quot;products.product_no&quot; 4. \u7ea6\u675f\uff1a \u68c0\u67e5\u7ea6\u675f\u662f\u8868\u4e2d\u6700\u4e3a\u5e38\u89c1\u7684\u7ea6\u675f\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u4f60\u58f0\u660e\u5728\u67d0\u4e2a\u5b57\u6bb5\u91cc\u7684\u6570\u503c\u5fc5\u987b\u6ee1\u8db3\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u58f0\u660e\u8868\u7ea7\u522b\u7684\u68c0\u67e5\u7ea6\u675f\u3002 CREATE TABLE products ( product_no integer, name text, &#8211;price\u5b57\u6bb5\u7684\u503c\u5fc5\u987b\u5927\u4e8e0\uff0c\u5426\u5219\u5728\u63d2\u5165\u6216\u4fee\u6539\u8be5\u5b57\u6bb5\u503c\u662f\uff0c\u5c06\u5f15\u53d1\u8fdd\u89c4\u9519\u8bef\u3002\u8fd8\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u8be5\u68c0\u67e5\u7ea6\u675f &#8211;\u662f\u533f\u540d\u7ea6\u675f\uff0c\u5373\u5728\u8868\u5b9a\u4e49\u65f6\u6ca1\u6709\u663e\u793a\u547d\u540d\u8be5\u7ea6\u675f\uff0c\u8fd9\u6837PostgreSQL\u5c06\u4f1a\u6839\u636e\u5f53\u524d\u7684\u8868\u540d\u3001\u5b57\u6bb5\u540d\u548c\u7ea6\u675f\u7c7b\u578b\uff0c &#8211;\u4e3a\u8be5\u7ea6\u675f\u81ea\u52a8\u547d\u540d\uff0c\u5982\uff1aproducts_price_check\u3002 price numeric CHECK (price &gt; 0) ); CREATE TABLE products ( product_no integer, name text, &#8211;\u8be5\u5b57\u6bb5\u7684\u68c0\u67e5\u7ea6\u675f\u88ab\u663e\u793a\u547d\u540d\u4e3apositive_price\u3002\u8fd9\u6837\u505a\u597d\u5904\u5728\u4e8e\u4eca\u540e\u7ef4\u62a4\u8be5\u7ea6\u675f\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u8be5\u540d\u8fdb\u884c\u76f4\u63a5\u64cd\u4f5c\u3002 price numeric CONSTRAINT positive_price CHECK (price &gt; 0) ); \u4e0b\u9762\u7684\u7ea6\u675f\u662f\u975e\u7a7a\u7ea6\u675f\uff0c\u5373\u7ea6\u675f\u7684\u5b57\u6bb5\u4e0d\u80fd\u63d2\u5165\u7a7a\u503c\uff0c\u6216\u8005\u662f\u5c06\u5df2\u6709\u6570\u636e\u66f4\u65b0\u4e3a\u7a7a\u503c\u3002 CREATE TABLE products ( product_no integer NOT NULL, name text NOT NULL, price numeric ); \u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u4e2d\u5b58\u5728\u591a\u4e2a\u7ea6\u675f\uff0c\u5728\u5b9a\u4e49\u65f6\u53ef\u4ee5\u4e0d\u7528\u8003\u8651\u7ea6\u675f\u7684\u58f0\u660e\u987a\u5e8f\u3002 CREATE TABLE products ( product_no integer NOT NULL, name text NOT NULL, price numeric NOT NULL CHECK (price &gt; 0) ); \u552f\u4e00\u6027\u7ea6\u675f\uff0c\u5373\u6307\u5b9a\u7684\u5b57\u6bb5\u4e0d\u80fd\u63d2\u5165\u91cd\u590d\u503c\uff0c\u6216\u8005\u662f\u5c06\u67d0\u4e00\u8bb0\u5f55\u7684\u503c\u66f4\u65b0\u4e3a\u5f53\u524d\u8868\u4e2d\u7684\u5df2\u6709\u503c\u3002 CREATE TABLE products ( product_no integer UNIQUE, name text, price numeric ); CREATE TABLE products ( product_no integer, name text, price numeric, UNIQUE (product_no) ); \u4e3a\u8868\u4e2d\u7684\u591a\u4e2a\u5b57\u6bb5\u5b9a\u4e49\u8054\u5408\u552f\u4e00\u6027\u3002 CREATE TABLE example ( a integer, b integer, c integer, UNIQUE (a, c) ); \u4e3a\u552f\u4e00\u6027\u7ea6\u675f\u547d\u540d\u3002 CREATE TABLE products ( product_no integer CONSTRAINT must_be_different UNIQUE, name text, price numeric ); \u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u7a7a\u503c(NULL)\u4e4b\u95f4\u88ab\u89c6\u4e3a\u4e0d\u76f8\u7b49\u7684\u6570\u636e\uff0c\u56e0\u6b64\u5bf9\u4e8e\u67d0\u4e00\u552f\u4e00\u6027\u5b57\u6bb5\uff0c\u53ef\u4ee5\u591a\u6b21\u63d2\u5165\u7a7a\u503c\u3002\u7136\u800c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e00\u89c4\u5219\u5e76\u4e0d\u662f\u88ab\u6240\u6709\u6570\u636e\u5e93\u90fd\u9075\u5b88\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u6570\u636e\u5e93\u79fb\u690d\u65f6\u53ef\u80fd\u4f1a\u9020\u6210\u4e00\u5b9a\u7684\u9ebb\u70e6\u3002 5. \u4e3b\u952e\u548c\u5916\u952e\uff1a \u4ece\u6280\u672f\u4e0a\u6765\u8bb2\uff0c\u4e3b\u952e\u7ea6\u675f\u53ea\u662f\u552f\u4e00\u7ea6\u675f\u548c\u975e\u7a7a\u7ea6\u675f\u7684\u7ec4\u5408\u3002 CREATE TABLE products ( product_no integer PRIMARY KEY, &#8211;\u5b57\u6bb5product_no\u88ab\u5b9a\u4e49\u4e3a\u8be5\u8868\u7684\u552f\u4e00\u4e3b\u952e\u3002 name text, p&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[89,82],"tags":[],"class_list":["post-1625","post","type-post","status-publish","format-standard","hentry","category-postgres-db","category-82"],"_links":{"self":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/1625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1625"}],"version-history":[{"count":1,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/1625\/revisions"}],"predecessor-version":[{"id":1626,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/1625\/revisions\/1626"}],"wp:attachment":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}