{"id":2578,"date":"2025-03-09T18:56:53","date_gmt":"2025-03-09T10:56:53","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2578"},"modified":"2025-03-09T22:59:40","modified_gmt":"2025-03-09T14:59:40","slug":"postgresql-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/09\/postgresql-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b\/","title":{"rendered":"PostgreSQL \u6570\u636e\u7c7b\u578b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">PostgreSQL \u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u4ee5\u4e0b\u662f PostgreSQL \u4e2d\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b\u5206\u7c7b\u548c\u8be6\u7ec6\u4ecb\u7ecd\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. \u6570\u5b57\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6574\u6570\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SMALLINT<\/code>\uff1a\u5b58\u50a8 -32,768 \u5230 32,767 \u8303\u56f4\u7684\u6574\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5c0f\u8303\u56f4\u6574\u6570\u3002<\/li>\n\n\n\n<li><code>INTEGER<\/code>\uff1a\u5b58\u50a8 -2,147,483,648 \u5230 2,147,483,647 \u8303\u56f4\u7684\u6574\u6570\uff0c\u5e38\u7528\u4e8e\u4e00\u822c\u6574\u6570\u3002<\/li>\n\n\n\n<li><code>BIGINT<\/code>\uff1a\u5b58\u50a8 -9,223,372,036,854,775,808 \u5230 9,223,372,036,854,775,807 \u8303\u56f4\u7684\u6574\u6570\uff0c\u9002\u7528\u4e8e\u5927\u6574\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6d6e\u52a8\u70b9\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>REAL<\/code>\uff1a\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff0c\u5360\u7528 4 \u5b57\u8282\u3002\u9002\u7528\u4e8e\u8f83\u5c0f\u7cbe\u5ea6\u7684\u6d6e\u52a8\u70b9\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>DOUBLE PRECISION<\/code>\uff1a\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff0c\u5360\u7528 8 \u5b57\u8282\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66f4\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u52a8\u70b9\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>NUMERIC<\/code> \/ <code>DECIMAL<\/code>\uff1a\u7cbe\u786e\u7684\u6570\u5b57\u7c7b\u578b\uff0c\u53ef\u4ee5\u6307\u5b9a\u5c0f\u6570\u4f4d\u6570\uff0c\u9002\u7528\u4e8e\u9ad8\u7cbe\u5ea6\u8981\u6c42\u7684\u573a\u666f\uff0c\u6bd4\u5982\u8d22\u52a1\u8ba1\u7b97\u3002\u53ef\u4ee5\u8bbe\u7f6e\u6700\u5927\u603b\u4f4d\u6570\u548c\u5c0f\u6570\u4f4d\u6570\uff0c\u5982 <code>NUMERIC(10, 2)<\/code> \u8868\u793a\u6700\u591a 10 \u4f4d\u6570\uff0c\u5176\u4e2d 2 \u4f4d\u4e3a\u5c0f\u6570\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. \u5b57\u7b26\u4e32\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CHAR(n)<\/strong>\uff1a\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff0c\u5b58\u50a8\u957f\u5ea6\u4e3a n \u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u63d2\u5165\u7684\u5b57\u7b26\u4e32\u4e0d\u8db3 n \u4e2a\u5b57\u7b26\uff0c\u4f1a\u7528\u7a7a\u683c\u586b\u5145\u3002<\/li>\n\n\n\n<li><strong>VARCHAR(n)<\/strong>\uff1a\u53ef\u53d8\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff0c\u6700\u591a\u53ef\u4ee5\u5b58\u50a8 n \u4e2a\u5b57\u7b26\uff0c\u9002\u7528\u4e8e\u8f83\u77ed\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><strong>TEXT<\/strong>\uff1a\u6ca1\u6709\u957f\u5ea6\u9650\u5236\u7684\u5b57\u7b26\u4e32\uff0c\u9002\u7528\u4e8e\u957f\u6587\u672c\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>3. \u65e5\u671f\u548c\u65f6\u95f4\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>DATE<\/strong>\uff1a\u5b58\u50a8\u65e5\u671f\uff0c\u683c\u5f0f\u4e3a <code>YYYY-MM-DD<\/code>\uff0c\u4e0d\u5305\u542b\u65f6\u95f4\u3002<\/li>\n\n\n\n<li><strong>TIME<\/strong>\uff1a\u5b58\u50a8\u65f6\u95f4\uff0c\u683c\u5f0f\u4e3a <code>HH:MI:SS<\/code>\uff0c\u4e0d\u5305\u542b\u65e5\u671f\u3002<\/li>\n\n\n\n<li><strong>TIMESTAMP<\/strong>\uff1a\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u683c\u5f0f\u4e3a <code>YYYY-MM-DD HH:MI:SS<\/code>\uff0c\u5e38\u7528\u4e8e\u8bb0\u5f55\u4e8b\u4ef6\u7684\u53d1\u751f\u65f6\u95f4\u3002<\/li>\n\n\n\n<li><strong>TIMESTAMPTZ<\/strong>\uff1a\u4e0e <code>TIMESTAMP<\/code> \u76f8\u4f3c\uff0c\u4f46\u5305\u542b\u65f6\u533a\u4fe1\u606f\u3002\u9002\u7528\u4e8e\u8de8\u65f6\u533a\u7684\u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>INTERVAL<\/strong>\uff1a\u5b58\u50a8\u65f6\u95f4\u95f4\u9694\uff0c\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u4f8b\u5982\u201c3 days\u201d\u8868\u793a 3 \u5929\u7684\u65f6\u95f4\u95f4\u9694\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. \u5e03\u5c14\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>BOOLEAN<\/strong>\uff1a\u8868\u793a\u903b\u8f91\u503c\uff0c\u53ea\u6709\u4e09\u4e2a\u53ef\u80fd\u7684\u503c\uff1a<code>TRUE<\/code>\u3001<code>FALSE<\/code> \u548c <code>NULL<\/code>\u3002\u9002\u7528\u4e8e\u5b58\u50a8\u771f\/\u5047\u6761\u4ef6\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. \u4e8c\u8fdb\u5236\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>BYTEA<\/strong>\uff1a\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5982\u6587\u4ef6\u3001\u56fe\u50cf\u7b49\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u975e\u6587\u672c\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>6. \u6570\u7ec4\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL \u652f\u6301\u6570\u7ec4\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u5b58\u50a8\u540c\u4e00\u7c7b\u578b\u7684\u591a\u4e2a\u503c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>INTEGER[]<\/strong>\uff1a\u6574\u6570\u6570\u7ec4\uff0c\u5b58\u50a8\u591a\u4e2a\u6574\u6570\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  CREATE TABLE test_array (numbers INTEGER&#91;]);\n  INSERT INTO test_array (numbers) VALUES ('{1, 2, 3}');<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u4e3a\u4efb\u610f\u6570\u636e\u7c7b\u578b\u521b\u5efa\u6570\u7ec4\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>7. JSON \u548c JSONB \u7c7b\u578b<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>JSON<\/strong>\uff1a\u7528\u4e8e\u5b58\u50a8 JSON \u683c\u5f0f\u7684\u6587\u672c\u6570\u636e\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u7ed3\u6784\u5316\u7684\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u6a21\u5f0f\u7684\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>JSONB<\/strong>\uff1a\u4e8c\u8fdb\u5236 JSON\uff0c\u63d0\u4f9b\u6bd4 JSON \u66f4\u9ad8\u6548\u7684\u5b58\u50a8\u548c\u67e5\u8be2\u6027\u80fd\uff0c\u5c24\u5176\u5728\u8fdb\u884c\u590d\u6742\u67e5\u8be2\u65f6\u66f4\u4f18\u3002\u9002\u5408\u5b58\u50a8\u4e0d\u5b9a\u7ed3\u6784\u7684 JSON \u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE test_json (\n    data JSONB\n);\nINSERT INTO test_json (data) VALUES ('{\"name\": \"John\", \"age\": 30}');<\/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>8. \u679a\u4e3e\u7c7b\u578b (ENUM)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u679a\u4e3e\u7c7b\u578b\u5141\u8bb8\u4f60\u4e3a\u5217\u5b9a\u4e49\u4e00\u7ec4\u6709\u9650\u7684\u503c\u3002\u9002\u7528\u4e8e\u67d0\u4e9b\u5b57\u6bb5\u503c\u7684\u96c6\u5408\u662f\u56fa\u5b9a\u7684\uff0c\u6bd4\u5982\u72b6\u6001\u3001\u7c7b\u578b\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');\nCREATE TABLE test_enum (\n    current_mood mood\n);\nINSERT INTO test_enum (current_mood) VALUES ('happy');<\/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>9. \u81ea\u5b9a\u4e49\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL \u5141\u8bb8\u7528\u6237\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u6765\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u590d\u5408\u7c7b\u578b\uff08\u7c7b\u4f3c\u4e8e\u8868\u7ed3\u6784\uff09\u6216\u8303\u56f4\u7c7b\u578b\uff08\u8868\u793a\u4e00\u4e2a\u8303\u56f4\u533a\u95f4\uff09\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u590d\u5408\u7c7b\u578b<\/strong>\uff1a\u7531\u591a\u4e2a\u5b57\u6bb5\u7ec4\u6210\uff0c\u7c7b\u4f3c\u4e8e\u8868\u7ed3\u6784\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TYPE person AS (\n    first_name VARCHAR(50),\n    last_name VARCHAR(50),\n    age INTEGER\n);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8303\u56f4\u7c7b\u578b<\/strong>\uff1a\u8868\u793a\u8303\u56f4\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u4e00\u4e2a\u6574\u6570\u8303\u56f4\u3001\u65e5\u671f\u8303\u56f4\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE test_range (\n    age_range INT4RANGE\n);\nINSERT INTO test_range (age_range) VALUES ('&#91;30,40]');<\/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>10. \u7f51\u7edc\u5730\u5740\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL \u63d0\u4f9b\u4e86\u51e0\u79cd\u7528\u4e8e\u5b58\u50a8\u7f51\u7edc\u5730\u5740\u7c7b\u578b\u7684\u5b57\u6bb5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>INET<\/strong>\uff1a\u5b58\u50a8 IP \u5730\u5740\uff0c\u652f\u6301 IPv4 \u548c IPv6\u3002<\/li>\n\n\n\n<li><strong>CIDR<\/strong>\uff1a\u5b58\u50a8\u7f51\u7edc\u5730\u5740\uff08\u5982\u7f51\u7edc\u524d\u7f00\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE test_network (\n    ip_address INET\n);\nINSERT INTO test_network (ip_address) VALUES ('192.168.1.1');<\/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>11. \u5176\u4ed6\u5e38\u7528\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UUID<\/strong>\uff1a\u5b58\u50a8\u5168\u7403\u552f\u4e00\u6807\u8bc6\u7b26\uff08UUID\uff09\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u552f\u4e00\u6807\u8bc6\u7684\u573a\u666f\uff0c\u5982\u7528\u6237 ID\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE test_uuid (\n    user_id UUID\n);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>XML<\/strong>\uff1a\u5b58\u50a8 XML \u6570\u636e\uff0c\u7528\u4e8e\u5b58\u50a8\u7ed3\u6784\u5316\u7684\u6807\u8bb0\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE test_xml (\n    data XML\n);\nINSERT INTO test_xml (data) VALUES ('&lt;user&gt;&lt;name&gt;John&lt;\/name&gt;&lt;age&gt;30&lt;\/age&gt;&lt;\/user&gt;');<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TSVECTOR<\/strong> \u548c <strong>TSQUERY<\/strong>\uff1a\u5206\u522b\u7528\u4e8e\u5168\u6587\u641c\u7d22\u7684\u5b58\u50a8\u548c\u67e5\u8be2\u64cd\u4f5c\uff0c\u652f\u6301\u9ad8\u6548\u7684\u6587\u672c\u7d22\u5f15\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>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6ee1\u8db3\u4e0d\u540c\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u6570\u636e\u7c7b\u578b\uff0c\u80fd\u63d0\u9ad8\u6570\u636e\u5e93\u6027\u80fd\u5e76\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL \u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u4ee5\u4e0b\u662f PostgreSQL \u4e2d\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2578","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\/2578","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=2578"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2578\/revisions"}],"predecessor-version":[{"id":2580,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2578\/revisions\/2580"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2591"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}