PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔媆誕嬉誕

CREATE CAST

劉䓄

CREATE CAST -- 垔幬婔婻䫘潙垔幬䔇蘸扵

臺濘

CREATE CAST (sourcetype AS targettype)
    WITH FUNCTION funcname (argtypes)
    [ AS ASSIGNMENT | AS IMPLICIT ]

CREATE CAST (sourcetype AS targettype)
    WITHOUT FUNCTION
    [ AS ASSIGNMENT | AS IMPLICIT ]

柟誄

CREATE CAST 垔幬婔婻桄䔇蘸扵㔗婔婻蘸扵臘滯套嘘婘婴婻䌂傋幋閘誕臯蘸扵㔗懫套

SELECT CAST(42 AS text);

锔誺脄䫘嬉麵弄滯䔇婔婻庘昄檪昘昄婩麟 42 蘸扵潊 text 䌂傋婘認婻冋床麯滇 text(int4)(套悩澇橬鵇噽垔幬喘劽锗䔇蘸扵闼幽認婻蘸扵儖嚔崌蘖)㔗

婴䓉䌂傋埇傖滇庯誕彽噚垹䔇懟攺滇垄傸幋閘埇傖"躻䫌蘸扵"蔯婉䫘脄䫘傂嘘庘昄㔗認儌驔襕婴䓉䌂傋嘪䫘劯湙䔇喙鄘臘䯄嘵嚟㔗懫套䌂傋 textvarchar 滇庯誕彽噚垹䔇㔗

䚺䩕施埻橬婘滯䇞襕挗蘸扵䔇愙喕婋欉脄䫘婔婻蘸扵幘儌滇婔婻滯䇞䔇 CAST(x AS typename)x::typename 悇锹㔗

套悩蘸扵赆湺螄婺 AS ASSIGNMENT 闼幽婘䂍䕞湺昄扞䌂傋䔇庖枕蕋唚䔇施唍埇傖锊劆脄䫘垄㔗懫套啺螆 foo.f1 滇婔婻䌂傋婺 text 䔇庖枕闼幽

INSERT INTO foo (f1) VALUES (42);

套悩傯 integer 䌂傋彄 text 䌂傋䔇蘸扵湺螄婺 AS ASSIGNMENT 婪麵䔇認埖儌赆噕螩劥彍儌婉噕螩㔗锔婩䫘橇臺蕋唚蘸扵準柟誄認䓉蘸扵㔗

套悩蘸扵赆湺螄婺 AS IMPLICIT 闼幽垄儌埇傖婘傂嘘䯇嵄麯脄䫘婉䞇滇蕋唚誻滇婘臘膆嚟䔇喙鄘㔗懫套啹婺 || 毖埖 text 淉嘩昄

SELECT 'The time is ' || now();

儖埻橬婘䌂傋 timestamptext 䔇蘸扵赆湺螄婺 AS IMPLICIT 䔇施唍欉噕螩㔗劥彍儌媙釂滯䇞幥喍蘸扵懫套

SELECT 'The time is ' || CAST(now() AS text);

锔婩嘪䫘橇臺锊劆蘸扵準柟誄認䓉䌂傋䔇蘸扵㔗

婘滇劥儖蘸扵湺螄婺锊劆䔇閞鵻婪媺垽婔底滇滯捺䔇㔗誺庯婄凯䔇锊劆蘸扵虇冇嚔凚躘 PostgreSQL 锬拷螷庺喺攻䔇变傴蓼悊潡蔙滇垯噘婉脘蓼悊变傴啹婺庻婘崔婻埇脘䔇蓼悊㔗婔溇喘䔇拺毺垔冋滇埻橬婘劯婔婻锔䫘䌂傋评䘘麯麵䔇闼底埇傖媺䘍蘸扵媇敇䔇䌂傋幋閘欉湺螄婺埇锊劆脄䫘蘸扵㔗懫套傯 int2int4 埇傖劽䊖婄湺螄婺锊劆蘸扵嘖滇傯 float8int4 幘螩庫臖湺螄婺蕋唚蘸扵㔗虘䌂傋评䘘䔇蘸扵懫套 textint4 橔喘埻脘滯䇞婄蘸扵㔗

襕愿录傺婔婻蘸扵嘹媙釂拖橬溊潡䕞湺昄扞䌂傋㔗襕录傺婔婻庯誕彽噚垹䔇蘸扵嘹媙釂滇轙亓䫘潙㔗啔認婻鍊彽滇啹婺婔䓉橬閞鵻䔇庯誕彽噚垹蘸扵埇傖冽垹滷揓懕橉媇単㔗

埗昄

sourcetype

蘸扵䔇溊昄扞䌂傋

targettype

蘸扵䔇䕞湺昄扞䌂傋

funcname(argtypes)

䫘庯欓臯蘸扵䔇庘昄㔗認婻庘昄劉埇傖滇䫘昇嚟劉媞閄䔇㔗套悩垄澇橬䫘昇嚟劉媞閄闼幽臖庘昄儖傯昇嚟抩䘵虇冇婺欆庺準㔗庘昄䔇䂷悩昄扞䌂傋媙釂对陉蘸扵䔇䕞湺䌂傋㔗垄䔇埗昄婘婋麵螘螺㔗

WITHOUT FUNCTION

臘䴺溊昄扞䌂傋启䕞湺昄扞䌂傋滇庯誕彽噚垹䔇欔傖婉驔襕傔幽庘昄準欓臯蘸扵㔗

AS ASSIGNMENT

臘䴺蘸扵埇傖婘蕋唚䯇嵄麯锊劆脄䫘

AS IMPLICIT

臘䴺蘸扵埇傖婘傂嘘䯇嵄麯锊劆脄䫘

蘸扵垂䯄庘昄埇傖橬婔彄婬婻埗昄㔗丸婔婻埗昄䌂傋媙釂启蘸扵䔇溊䌂傋䕩劯㔗套悩橬丸庯婻埗昄闼幽媙釂滇 integer 䌂傋垄毖櫽婯䕞湺䌂傋䕩噿蕫䔇䌂傋媞閄臉套悩澇橬儌滇 -1 㔗套悩橬丸婬婻埗昄儌媙釂滇 boolean 䌂傋套悩認婻蘸扵滇婔婻滯䇞䔇蘸扵闼幽垄櫽彄 true 劥彍櫽彄 false 㔗婉誺冽喺攻䔇滇SQL 蓇评婘昊底婺劽婋襕挗婉劯䔇滯䇞启锊劆蘸扵䔇臯婺㔗認婻埗昄柊冕䂍闼底媙釂垂䯄認䓉臯婺䔇蘸扵庘昄㔗婉誺婉傺螞嘹螆螇溴䌂庘昄㔗

溼婩愙喕婋䌂傋蘸扵媙釂橬婉劯䔇溊䌂傋启䕞湺昄扞䌂傋㔗婉誺埻襕垄橬崔婻埗昄䔇䌂傋蘸扵垂䯄庘昄儌噕螩弄滯婔婻溊䌂傋启䕞湺䌂傋䕩劯䔇蘸扵㔗認婻庘昄䫘庯婘係䂘臘麯臘䴺䌂傋䕩噿䔇阪庥蘸扵庘昄㔗溴变劉庘昄䫘庯蘸扵婔婻臖䌂傋䔇昄唚婺噽丸庯婻埗昄䂍庺䔇婥䌂傋媞閄臉䔇唚㔗啹婺䕞嬉臺濘埻噕螩昊底喙䘞䔇昄扞䌂傋拖橬䌂傋媞閄臉啹溴認婻䬹攓凹䫘潙垔幬䔇䕞湺䌂傋澇橬傔幽䫘柊彄垄埻滇婺庖垯昘㔗

套悩婔婻䌂傋蘸扵䔇溊䌂傋启䕞湺䌂傋婉劯幽婫毖櫽崔庯婔婻埗昄垄儌臘䴺傯婔䓉䌂傋蘸扵潊埥崡婔䓉䌂傋埻䫘婔婻準黴幽婫劯施垂桘阪庥蘸扵㔗套悩澇橬認湙䔇釹埇䫘闼幽蘸扵潊婔婻嘪䫘庖䌂傋媞閄臉䔇䌂傋儖潬埪婴婻準黴婔婻滇婘昄扞䌂傋幋閘蘸扵埥崡婔婻滇桘媹媞閄臉㔗

濘懟

DROP CAST 役鍴䫘潙垔幬䔇蘸扵㔗

臙濘懟套悩婯橕脘埯劏蘸扵䌂傋闼幽嘹驔襕滯䇞婄垔幬婴婻桹劏䔇蘸扵㔗

婘 PostgreSQL 7.3 幋嬉套悩婔婻庘昄䔇劉庖启婔婻昄扞䌂傋䕩劯幽婫誫啂臖䓉昄扞䌂傋蔯婫誻毖埖埥崡婔䓉䌂傋䔇埗昄躻媘儌滇婔婻蘸扵庘昄㔗認婻嚹䂘锟五昇嚟䔇嚘噖傖埪婺庖脘婘係䂘臘䓉臘䴺庯誕彽噚垹䔇蘸扵䯄婘噾䂟赆庘嚄庖㔗喙䘞䔇蘸扵庘昄傉䇽镕冻認婻变劉蓇彍嘖滇垄傸䯄婘劯施幘婘係䂘臘 pg_cast 麯滆䴺婺蘸扵㔗

荘䇽婉滇媙釂䔇嘖滇誻滇傺螞嘹镕冻斓䔇变劉䌂傋蘸扵垂䯄庘昄䔇幹愇幘儌滇臘庘昄劉启䕞湺昄扞䌂傋劯劉㔗螩崔䫘潙幹愇庯嘪䫘庘昄鼯湚䔇臘䴺濘(typename(x))準啔昄扞䌂傋蘸扵㔗認䓉臘䴺濘敄喘儌滇脄䫘䌂傋蘸扵垂䯄庘昄認湙幽婉嚔赆嘷嘩婔䓉䌂傋蘸扵蔯赆䬹枪䩋写㔗套悩嘹䔇蘸扵庘昄澇橬毬䙓認䓉嚹䂘变劉闼幽嘹儌嚔螷䫘潙冽喺攻㔗啹婺 PostgreSQL 噕螩劯劉婉劯埗昄䔇庘昄麉蘘啹溴劯施庻婘崔婻傯婉劯䌂傋劏劯湙䌂傋蘸扵䔇劯劉蘸扵庘昄婔䗹閞鵻鄘澇橬㔗

㔊濘懟㔏嬉麵䔇枕芘搐庖婔婻償償䔇脯傉䇽橬婔䓉愙喕婋係䂘儖襕嘪䫘 pg_cast 準蓼悊婔婻滯滆䔇庘昄脄䫘䔇劆幬㔗套悩庘昄脄䫘 name(x) 幽婉对陉傂嘘垂鍙庘昄嘖 name 滇婔婻昄扞䌂傋䔇劉庖幽婫 pg_cast 滆䴺庖垄滇傯䌂傋 x 彄認䓉䌂傋䔇婔婻庯誕彽噚垹䔇蘸扵闼幽臖脄䫘儖赆悇锹潊婔婻滯䇞䔇蘸扵㔗庻婘認婻冋崡儌埇傖螷庯誕彽噚垹䔇䌂傋蘸扵嘪䫘庘昄鼯湚䔇臺濘脄䫘剿嘪傡傸䚺儏庘昄幘埇傖㔗

冋床

嘪䫘庘昄 int4(text) 录傺婔婻傯 text 䌂傋彄 int4 䌂傋䔇蘸扵

CREATE CAST (text AS int4) WITH FUNCTION int4(text);

認婻蘸扵婘係䂘婺噾䂟鵇噽垔幬庖㔗

噚垹攓

The CREATE CAST 变傴镕冻 SQL 湺庖埻婉誺認婻 SQL 澇橬柊冕庯誕彽噚垹䌂傋潡蔙鵺崡䔇埗昄䫘庯垂䯄庘昄㔗AS IMPLICIT 幘滇 PostgreSQL 䔇欷匘㔗

埽蓕

CREATE FUNCTION, CREATE TYPE, DROP CAST


劯锔饡釕嬉誕
CREATE AGGREGATE婪婔亓CREATE CONSTRAINT TRIGGER