{"id":20758,"date":"2024-01-02T05:27:22","date_gmt":"2024-01-02T05:27:22","guid":{"rendered":"https:\/\/server.hk\/blog\/20758\/"},"modified":"2024-11-14T10:39:13","modified_gmt":"2024-11-14T02:39:13","slug":"postgresql-command-create-cast","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/20758\/","title":{"rendered":"PostgreSQL \u547d\u4ee4\uff1aCREATE CAST"},"content":{"rendered":"<h1 id=\"postgresql-command-create-cast-VbpwCqGErz\">PostgreSQL \u547d\u4ee4\uff1aCREATE CAST<\/h1>\n<p>PostgreSQL \u662f\u4e00\u6b3e\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u5f0f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u5ee3\u6cdb\u7684\u529f\u80fd\u548c\u7279\u6027\u3002\u5176\u4e2d\u4e00\u500b\u986f\u8457\u7684\u7279\u9ede\u662f\u80fd\u5920\u4f7f\u7528 <code>CREATE CAST<\/code> \u547d\u4ee4\u5275\u5efa\u81ea\u5b9a\u7fa9\u6578\u64da\u985e\u578b\u8f49\u63db\u3002\u8a72\u547d\u4ee4\u5141\u8a31\u7528\u6236\u5b9a\u7fa9\u6578\u64da\u5728\u6578\u64da\u5eab\u4e2d\u4e0d\u540c\u985e\u578b\u4e4b\u9593\u7684\u8f49\u63db\u65b9\u5f0f\u3002<\/p>\n<h2 id=\"understanding-data-type-conversion-VbpwCqGErz\">\u7406\u89e3\u6578\u64da\u985e\u578b\u8f49\u63db<\/h2>\n<p>\u5728 PostgreSQL \u4e2d\uff0c\u8868\u4e2d\u7684\u6bcf\u500b\u6b04\u4f4d\u90fd\u5177\u6709\u7279\u5b9a\u7684\u6578\u64da\u985e\u578b\uff0c\u8a72\u985e\u578b\u78ba\u5b9a\u5b83\u53ef\u4ee5\u5b58\u5132\u7684\u6578\u64da\u985e\u578b\u3002\u6709\u6642\u9700\u8981\u5c07\u6578\u64da\u5f9e\u4e00\u7a2e\u985e\u578b\u8f49\u63db\u70ba\u53e6\u4e00\u7a2e\u985e\u578b\uff0c\u7279\u5225\u662f\u5728\u9032\u884c\u8a08\u7b97\u6216\u6bd4\u8f03\u6642\u3002PostgreSQL \u63d0\u4f9b\u5167\u7f6e\u7684\u6578\u64da\u985e\u578b\u8f49\u63db\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u7528\u6236\u53ef\u80fd\u9700\u8981\u4f7f\u7528 <code>CREATE CAST<\/code> \u547d\u4ee4\u5b9a\u7fa9\u81ea\u5df1\u7684\u8f49\u63db\u3002<\/p>\n<h2 id=\"the-create-cast-command-VbpwCqGErz\">CREATE CAST \u547d\u4ee4<\/h2>\n<p>\u5728 PostgreSQL \u4e2d\uff0c<code>CREATE CAST<\/code> \u547d\u4ee4\u5141\u8a31\u7528\u6236\u5b9a\u7fa9\u5169\u7a2e\u73fe\u6709\u6578\u64da\u985e\u578b\u4e4b\u9593\u7684\u65b0\u8f49\u63db\u3002\u5275\u5efa\u8f49\u63db\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CREATE CAST (source_type AS target_type) WITH FUNCTION cast_function_name (argument_type);<\/code><\/pre>\n<p>\u8b93\u6211\u5011\u4f86\u4e86\u89e3\u4e00\u4e0b\u9019\u500b\u547d\u4ee4\u7684\u5404\u500b\u7d44\u6210\u90e8\u5206\uff1a<\/p>\n<ul>\n<li><code>source_type<\/code>\uff1a\u8f49\u63db\u4f86\u6e90\u7684\u6578\u64da\u985e\u578b\u3002<\/li>\n<li><code>target_type<\/code>\uff1a\u8981\u57f7\u884c\u8f49\u63db\u7684\u6578\u64da\u985e\u578b\u3002<\/li>\n<li><code>cast_function_name<\/code>\uff1a\u57f7\u884c\u8f49\u63db\u7684\u51fd\u6578\u540d\u7a31\u3002<\/li>\n<li><code>argument_type<\/code>\uff1a\u50b3\u905e\u7d66\u8f49\u63db\u51fd\u6578\u7684\u53c3\u6578\u985e\u578b\u3002<\/li>\n<\/ul>\n<p>\u901a\u904e\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u8f49\u63db\uff0c\u7528\u6236\u53ef\u4ee5\u6307\u5b9a\u5728\u5169\u7a2e\u7279\u5b9a\u6578\u64da\u985e\u578b\u4e4b\u9593\u9032\u884c\u8f49\u63db\u6642\u61c9\u8a72\u5982\u4f55\u9032\u884c\u3002<\/p>\n<h2 id=\"example-creating-a-custom-cast-VbpwCqGErz\">\u7bc4\u4f8b\uff1a\u5275\u5efa\u81ea\u5b9a\u7fa9\u8f49\u63db<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>employees<\/code> \u7684\u8868\uff0c\u8a72\u8868\u5177\u6709\u4e00\u500b\u540d\u70ba <code>salary<\/code> \u7684<code>integer<\/code>\u985e\u578b\u7684\u5217\u3002\u6211\u5011\u5e0c\u671b\u80fd\u5920\u4f7f\u7528\u4e00\u500b\u540d\u70ba <code>money<\/code> \u7684\u81ea\u5b9a\u7fa9\u6578\u64da\u985e\u578b\u4f86\u6bd4\u8f03 <code>salary<\/code> \u503c\u3002\u70ba\u4e86\u5be6\u73fe\u9019\u4e00\u9ede\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>CREATE CAST<\/code> \u547d\u4ee4\u5275\u5efa\u4e00\u500b\u81ea\u5b9a\u7fa9\u8f49\u63db\u3002<\/p>\n<pre><code>CREATE CAST (integer AS money) WITH FUNCTION convert_to_money(integer);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u540d\u70ba <code>convert_to_money<\/code> \u7684\u51fd\u6578\u5f9e<code>integer<\/code>\u8f49\u63db\u70ba<code>money<\/code>\u3002\u6b64\u51fd\u6578\u63a5\u53d7\u4e00\u500b<code>integer<\/code>\u53c3\u6578\u4e26\u5c07\u5176\u8f49\u63db\u70ba<code>money<\/code>\u6578\u64da\u985e\u578b\u3002<\/p>\n<p>\u5275\u5efa\u4e86\u8f49\u63db\u5f8c\uff0c\u6211\u5011\u73fe\u5728\u53ef\u4ee5\u4f7f\u7528 <code>money<\/code> \u6578\u64da\u985e\u578b\u4f86\u6bd4\u8f03 <code>salary<\/code> \u503c\uff1a<\/p>\n<pre><code>SELECT * FROM employees WHERE salary &gt;= '1000'::money;<\/code><\/pre>\n<p>\u8a72\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u85aa\u6c34\u5927\u65bc\u6216\u7b49\u65bc $1000 \u7684\u54e1\u5de5\u3002<\/p>\n<h2 id=\"conclusion-VbpwCqGErz\">\u7d50\u8ad6<\/h2>\n<p>PostgreSQL \u4e2d\u7684 <code>CREATE CAST<\/code> \u547d\u4ee4\u5141\u8a31\u7528\u6236\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u6578\u64da\u985e\u578b\u8f49\u63db\u3002\u901a\u904e\u5275\u5efa\u8f49\u63db\uff0c\u7528\u6236\u53ef\u4ee5\u6307\u5b9a\u5728\u6578\u64da\u5eab\u4e2d\u4e0d\u540c\u6578\u64da\u985e\u578b\u4e4b\u9593\u9032\u884c\u8f49\u63db\u6642\u61c9\u8a72\u5982\u4f55\u9032\u884c\u3002\u6b64\u529f\u80fd\u63d0\u4f9b\u4e86\u5c0d\u6578\u64da\u64cd\u4f5c\u548c\u6bd4\u8f03\u64cd\u4f5c\u7684\u9748\u6d3b\u6027\u548c\u63a7\u5236\u3002<\/p>\n<h2 id=\"summary-VbpwCqGErz\">\u6458\u8981<\/h2>\n<p>\u7e3d\u4e4b\uff0cPostgreSQL \u4e2d\u7684 <code>CREATE CAST<\/code> \u547d\u4ee4\u4f7f\u7528\u6236\u80fd\u5920\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u6578\u64da\u985e\u578b\u8f49\u63db\u3002\u4f7f\u7528\u9019\u500b\u547d\u4ee4\uff0c\u7528\u6236\u53ef\u4ee5\u6307\u5b9a\u5728\u6578\u64da\u5eab\u4e2d\u4e0d\u540c\u6578\u64da\u985e\u578b\u4e4b\u9593\u9032\u884c\u8f49\u63db\u6642\u61c9\u8a72\u5982\u4f55\u9032\u884c\u3002\u8981\u4e86\u89e3\u6709\u95dc PostgreSQL \u53ca\u5176\u529f\u80fd\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8acb\u8003\u616e\u63a2\u7d22\u9818\u5148\u7684 VPS \u8a17\u7ba1\u516c\u53f8 <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL \u547d\u4ee4\uff1aCR&#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-20758","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20758","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=20758"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20758\/revisions"}],"predecessor-version":[{"id":24354,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20758\/revisions\/24354"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=20758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=20758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=20758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}