{"id":20756,"date":"2024-01-02T05:25:23","date_gmt":"2024-01-02T05:25:23","guid":{"rendered":"https:\/\/server.hk\/blog\/20756\/"},"modified":"2024-11-14T10:39:16","modified_gmt":"2024-11-14T02:39:16","slug":"postgresql-command-create-collation","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/20756\/","title":{"rendered":"PostgreSQL \u547d\u4ee4: CREATE CONVERSION"},"content":{"rendered":"<h1 id=\"postgresql-command-create-conversion-HVfWxwrECb\">PostgreSQL \u547d\u4ee4: CREATE CONVERSION<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u5f0f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u591a\u7a2e\u529f\u80fd\u548c\u7279\u6027\u3002\u5b83\u7684\u5176\u4e2d\u4e00\u500b\u4e3b\u8981\u512a\u52e2\u662f\u652f\u63f4\u570b\u969b\u5316\u548c\u672c\u5730\u5316\uff0c\u5141\u8a31\u7528\u6236\u5132\u5b58\u548c\u64cd\u4f5c\u4e0d\u540c\u8a9e\u8a00\u548c\u5b57\u7b26\u96c6\u7684\u6578\u64da\u3002<code>CREATE CONVERSION<\/code> \u547d\u4ee4\u662f\u4e00\u500b\u6709\u7528\u7684\u5de5\u5177\uff0c\u8b93\u7528\u6236\u53ef\u4ee5\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u5b57\u7b26\u96c6\u8f49\u63db\uff0c\u5be6\u73fe\u4e0d\u540c\u7de8\u78bc\u4e4b\u9593\u7684\u7121\u7e2b\u6578\u64da\u4ea4\u63db\u3002<\/p>\n<h2 id=\"understanding-character-set-conversions-HVfWxwrECb\">\u4e86\u89e3\u5b57\u7b26\u96c6\u8f49\u63db<\/h2>\n<p>\u5728\u591a\u8a9e\u8a00\u7684\u6578\u64da\u5eab\u74b0\u5883\u4e2d\uff0c\u7d93\u5e38\u6703\u9047\u5230\u9700\u8981\u5c07\u6578\u64da\u5f9e\u4e00\u500b\u5b57\u7b26\u96c6\u8f49\u63db\u5230\u53e6\u4e00\u500b\u5b57\u7b26\u96c6\u7684\u60c5\u6cc1\u3002\u5b57\u7b26\u96c6\u8f49\u63db\u5728\u6578\u64da\u5132\u5b58\u5728\u4e00\u7a2e\u7de8\u78bc\u65b9\u5f0f\u4e0b\uff0c\u4f46\u9700\u8981\u4ee5\u53e6\u4e00\u7a2e\u7de8\u78bc\u986f\u793a\u6216\u8655\u7406\u6642\u5c31\u8b8a\u5f97\u5fc5\u8981\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u7684\u6578\u64da\u5eab\u4f7f\u7528 UTF-8 \u7de8\u78bc\u5132\u5b58\u6578\u64da\uff0c\u4f46\u9700\u8981\u4ee5 ISO-8859-1 \u7de8\u78bc\u986f\u793a\uff0c\u5c31\u9700\u8981\u9032\u884c\u5b57\u7b26\u96c6\u8f49\u63db\u3002<\/p>\n<p>PostgreSQL \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9810\u5b9a\u7fa9\u7684\u8f49\u63db\uff0c\u6db5\u84cb\u5927\u591a\u6578\u5e38\u898b\u60c5\u6cc1\u3002\u7136\u800c\uff0c\u6709\u6642\u60a8\u53ef\u80fd\u9700\u8981\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u8f49\u63db\u4f86\u6eff\u8db3\u7279\u5b9a\u9700\u6c42\uff0c\u9019\u6642\u5c31\u53ef\u4ee5\u4f7f\u7528 <code>CREATE CONVERSION<\/code> \u547d\u4ee4\u3002<\/p>\n<h2 id=\"the-create-conversion-command-HVfWxwrECb\">CREATE CONVERSION \u547d\u4ee4<\/h2>\n<p>PostgreSQL \u7684 <code>CREATE CONVERSION<\/code> \u547d\u4ee4\u5141\u8a31\u60a8\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u7684\u5b57\u7b26\u96c6\u8f49\u63db\u3002\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CREATE CONVERSION conversion_name FOR source_encoding TO dest_encoding FROM function_name;<\/code><\/pre>\n<p>\u8b93\u6211\u5011\u4f86\u89e3\u91cb\u9019\u500b\u547d\u4ee4\u7684\u5404\u500b\u7d44\u6210\u90e8\u5206\uff1a<\/p>\n<ul>\n<li><strong>conversion_name\uff1a<\/strong>\u60a8\u7d66\u9019\u500b\u8f49\u63db\u53d6\u7684\u540d\u5b57\uff0c\u5728\u6578\u64da\u5eab\u4e2d\u61c9\u8a72\u662f\u552f\u4e00\u7684\u3002<\/li>\n<li><strong>source_encoding\uff1a<\/strong>\u9700\u8981\u8f49\u63db\u7684\u6578\u64da\u6e90\u7de8\u78bc\u3002<\/li>\n<li><strong>dest_encoding\uff1a<\/strong>\u6578\u64da\u61c9\u8f49\u63db\u5230\u7684\u76ee\u6a19\u7de8\u78bc\u3002<\/li>\n<li><strong>function_name\uff1a<\/strong>\u57f7\u884c\u5be6\u969b\u8f49\u63db\u7684\u51fd\u6578\u540d\u7a31\u3002<\/li>\n<\/ul>\n<p>\u4e00\u65e6\u5b9a\u7fa9\u4e86\u81ea\u5b9a\u7fa9\u8f49\u63db\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u6d89\u53ca\u5b57\u7b26\u96c6\u8f49\u63db\u7684\u5404\u7a2e PostgreSQL \u547d\u4ee4\u548c\u51fd\u6578\u4e2d\u4f7f\u7528\u5b83\uff0c\u4f8b\u5982 <code>CAST<\/code>\u3001<code>CONVERT<\/code> \u548c <code>COLLATE<\/code>\u3002<\/p>\n<h2 id=\"example-creating-a-custom-conversion-HVfWxwrECb\">\u7bc4\u4f8b\uff1a\u5275\u5efa\u81ea\u5b9a\u7fa9\u8f49\u63db<\/h2>\n<p>\u5047\u8a2d\u60a8\u6709\u4e00\u500b\u6578\u64da\u5eab\u5132\u5b58\u7684\u6578\u64da\u4f7f\u7528 UTF-8 \u7de8\u78bc\uff0c\u4f46\u60a8\u9700\u8981\u5c07\u5176\u8f49\u63db\u70ba Shift_JIS \u7de8\u78bc\u7528\u65bc\u67d0\u500b\u7279\u5b9a\u61c9\u7528\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u81ea\u5b9a\u7fa9\u8f49\u63db\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre><code>CREATE CONVERSION utf8_to_shift_jis FOR 'UTF8' TO 'SHIFT_JIS' FROM utf8_to_shift_jis_function;<\/code><\/pre>\n<p>\u5728\u6b64\u7bc4\u4f8b\u4e2d\uff0c<code>utf8_to_shift_jis<\/code> \u662f\u8f49\u63db\u7684\u540d\u7a31\uff0c<code>UTF8<\/code> \u662f\u6e90\u7de8\u78bc\uff0c<code>SHIFT_JIS<\/code> \u662f\u76ee\u6a19\u7de8\u78bc\uff0c<code>utf8_to_shift_jis_function<\/code> \u662f\u57f7\u884c\u8f49\u63db\u7684\u51fd\u6578\u540d\u7a31\u3002<\/p>\n<p>\u4e00\u65e6\u5275\u5efa\u4e86\u8f49\u63db\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u9700\u8981\u5b57\u7b26\u96c6\u8f49\u63db\u7684\u67e5\u8a62\u6216\u51fd\u6578\u4e2d\u4f7f\u7528\u5b83\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT CONVERT('Hello, world!', 'utf8_to_shift_jis');<\/code><\/pre>\n<p>\u9019\u500b\u67e5\u8a62\u5c07\u4f7f\u7528\u6211\u5011\u5b9a\u7fa9\u7684\u81ea\u5b9a\u7fa9\u8f49\u63db\uff0c\u5c07\u5b57\u7b26\u4e32 &#8216;Hello, world!&#8217; \u5f9e UTF-8 \u7de8\u78bc\u8f49\u63db\u70ba Shift_JIS \u7de8\u78bc\u3002<\/p>\n<h2 id=\"summary-HVfWxwrECb\">\u7e3d\u7d50<\/h2>\n<p>PostgreSQL \u4e2d\u7684 <code>CREATE CONVERSION<\/code> \u547d\u4ee4\u5141\u8a31\u60a8\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u7684\u5b57\u7b26\u96c6\u8f49\u63db\uff0c\u5be6\u73fe\u4e0d\u540c\u7de8\u78bc\u4e4b\u9593\u7684\u7121\u7e2b\u6578\u64da\u4ea4\u63db\u3002\u901a\u904e\u5275\u5efa\u81ea\u5b9a\u7fa9\u8f49\u63db\uff0c\u60a8\u53ef\u4ee5\u8655\u7406 PostgreSQL \u9810\u5b9a\u7fa9\u8f49\u63db\u7121\u6cd5\u8986\u84cb\u7684\u7279\u5b9a\u9700\u6c42\u3002\u9019\u7a2e\u9748\u6d3b\u6027\u4f7f PostgreSQL \u6210\u70ba\u570b\u969b\u5316\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\u7684\u5f37\u5927\u9078\u64c7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u652f\u63f4 PostgreSQL \u4e26\u63d0\u4f9b\u5353\u8d8a\u6027\u80fd\u7684\u53ef\u9760 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002Server.HK \u63d0\u4f9b\u591a\u6a23\u5316\u7684\u8a08\u5283\u53ca\u4e00\u6d41\u7684\u5ba2\u6236\u652f\u6301\uff0c\u662f\u60a8\u6240\u6709\u8a17\u7ba1\u9700\u6c42\u7684\u53ef\u4fe1\u8cf4\u63d0\u4f9b\u5546\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL \u547d\u4ee4: C&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-20756","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20756","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=20756"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20756\/revisions"}],"predecessor-version":[{"id":24357,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20756\/revisions\/24357"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=20756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=20756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=20756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}