PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹10. 䌂傋蘸扵媆誕嬉誕

10.2. 淉嘩严

婋麵螾蓼䔇誺䘋蓼麪庖套嘘婘婔渇淉嘩严脄䫘婺䇞垔欔嘪䫘䔇䷽䆘滇巻婻淉嘩严㔗臙濘懟認婻誺䘋埖赆脄䫘淉嘩严䔇嚻噽亓嘌巉㔗埗黙誗4.1.6诙埡敘崔媇敇㔗

淉嘩严䌂傋蓼悊

  1. 傯係䂘臘 pg_operator 婺锬庺襕蔄荏䔇淉嘩严㔗套悩嘪䫘庖婔婻婉婥媞閄䔇淉嘩严劉(婩蓕䔇䪽喕)闼幽螴婺臖淉嘩严滇闼底婘嘷嬉抩䘵虇冇婺劉庖启埗昄婻昄鄘溼䇞䔇淉嘩严(埗黙誗5.7.3)㔗套悩䂍庺婔婻婥媞閄䔇淉嘩严劉闼幽埻蔄荏毺垔昇嚟婺䔇淉嘩严㔗

    1. 套悩抩䘵虇冇婺欆彄庖崔婻䕩劯埗昄䌂傋䔇淉嘩严闼幽埻蔄荏橔斷庺䯄婘虇冇婺䔇闼婔婻㔗嘖滇婉劯埗昄䌂傋䔇淉嘩严儖赆广京䩋写蔯婉䞇垄傸婘虇冇婺䔇嘉䘞套嘘㔗

  2. 昖欆來䇞毖埖膷噖埗昄䌂傋䔇淉嘩严㔗套悩欆彄婔婻(婘婔䂇赆蔄荏䔇淉嘩严婺埇脘埻庻婘婔婻來䇞对陉䔇)彍䫘幋㔗

    1. 套悩婔婻埯䕞淉嘩严脄䫘婺䔇婔婻埗昄滇 unknown 䌂傋彍婘橸渇演昖婺啺螆噽婯埥婔婻埗昄䌂傋䕩劯㔗噽垄潬埪 unknown 䔇愙喕䂺婉嚔婘溴崇欆彄对陉㔗

  3. 凂欆橔嚻对陉㔗

    1. 檕嚄闼底膷噖䌂傋婉对陉幽婫幘婉脘锊嚟蘸扵潊对陉䔇唍锬淉嘩严㔗unknown 桺橸婘認䓉愙喕婋埇傖蘸扵潊傂嘘婩薪㔗套悩埻嬷婋婔婻唍锬釹彍䫘幋劥彍䂓䂺婋婔準㔗

    2. 镉寖欔橬唍锬淉嘩严媺䘍闼底膷噖䌂傋对陉橔庖䇞䔇㔗溴施嘘赆䩋嘩启傡傸䔇嘺橸䌂傋䕩劯㔗套悩澇橬婔婻淉嘩严脘赆媺䘍彍媺䘍欔橬唍锬㔗套悩埻嬷婋婔婻唍锬釹彍䫘幋劥彍䂓䂺婋婔準㔗

    3. 镉寖欔橬唍锬淉嘩严媺䘍闼底驔襕䌂傋蘸扵施毖埖(匂庯膷噖昄扞䌂傋䔇䌂傋评䘘䔇)饡锬䌂傋嘉䘞橔崔䔇淉嘩严㔗套悩澇橬毖埖饡锬䌂傋䔇淉嘩严彍媺䘍欔橬唍锬㔗套悩埻嬷婋婔婻唍锬釹彍䫘幋劥彍䂓䂺婋婔準㔗

    4. 套悩橬傂嘘膷噖埗昄滇 unknown 䌂傋演昖嬷嘍䔇唍锬淉嘩严凹庫埗昄嘉䘞䔇䌂傋评䘘㔗婘懟婔婻脘崘毖埖庖严婾䌂傋评䘘䔇嘉䘞嘪䫘 string 䌂傋(認䓉凹庖严婾䔇啟佌滇劽锗䔇啹婺 unknown 桺橸䇞垂償庖严婾)㔗埥崡套悩欔橬嬷婋䔇唍锬淉嘩严鄘毖埖䕩劯䔇䌂傋评䘘彍锬拷臖䌂傋评䘘劥彍檕庺婔婻髍臇(啹婺婘澇橬敘崔亪䘵䔇溇傽婋方濘嘩庺溼䇞䔇锬拷)㔗䯄婘檕嚄婉毖埖锬垔䔇䌂傋评䘘䔇唍锬淉嘩严䇽劯套悩傂懟唍锬淉嘩严婘昊婻䂍垔䔇埗昄嘉䘞毖埖婔婻饡锬䌂傋彍檕嚄闼底婘臖埗昄嘉䘞毖埖麂饡锬䌂傋䔇唍锬淉嘩严㔗

    5. 套悩埻嬷婋婔婻淉嘩严彍䫘幋㔗套悩誻橬崔婻唍锬淉嘩严潡蔙澇橬唍锬淉嘩严彍库䫘婔婻髍臇㔗

婋麵滇婔底冋床㔗

冋10-1. 毺昄淉嘩严䌂傋蓼悊

婘係䂘臘婺麯埻橬婔婻毺昄淉嘩严垄傖 double precision 嘩婺埗昄㔗欆柟単䂍婋麵昖臵臘膆嚟䔇婴婻埗昄蕋庽 integer 䔇彺哋䌂傋

SELECT 2 ^ 3 AS "exp";

 exp
-----
   8
(1 row)

彖悊単凹婴婻埗昄鄘啔䌂傋蘸扵昖臵京昽庯

SELECT CAST(2 AS double precision) ^ CAST(3 AS double precision) AS "exp";

冋10-2. 庖严婾誂毖淉嘩严䌂傋彖悊

婔䓉庖严婾鼯湚䔇臺濘斵埇傖䫘庯庖严婾幘埇傖䫘庯崉溗䔇欷匘䌂傋㔗橻弄滯䌂傋䔇庖严婾儖赆欔橬埇脘䔇唍锬淉嘩严对陉㔗

橬婔婻橻弄滯䔇埗昄䔇冋床

SELECT text 'abc' || 'def' AS "text and unknown";

 text and unknown
------------------
 abcdef
(1 row)

橸冋婺彖悊単凂欆婴婻埗昄鄘滇 text 䔇淉嘩严㔗䇞垂認湙䔇淉嘩严啹溴埥婔婻埗昄儌赆螴婺滇 text 䌂傋㔗

婋麵滇誂毖橻弄滯䌂傋

SELECT 'abc' || 'def' AS "unspecified";

 unspecified
-------------
 abcdef
(1 row)

啹婺昖臵婺澇橬弄滯傂嘘䌂傋欔傖橸冋婺凹䌂傋澇橬傂嘘彺哋柊䴺㔗啹溴彖悊単昖欆欔橬唍锬淉嘩严埏䯄斵庻婘毖埖庖严婾䌂傋评䘘䔇淉嘩严幘庻婘毖埖嘉婾䌂傋评䘘䔇淉嘩严㔗啹婺庖严婾䌂傋评䘘滇饡锬欔傖锬拷庖严婾䌂傋评䘘䔇饡锬䌂傋 text 嘩婺蓼悊橻䘖䌂傋桺橸䔇弄滯䌂傋㔗

冋10-3. 䂺凹唚启埡埉淉嘩严䌂傋彖悊

PostgreSQL 淉嘩严臘麯麵橬庹溇螄嘘凹庫庯嬉䚔淉嘩严 @ 垄傸鄘䫘庯婺劇䓉昄唚䌂傋垂䯄䂺凹唚淉嘩㔗噽婺幋婔䫘庯 float8 䌂傋垄滇昄唚䌂傋评䘘婺䔇饡锬䌂傋㔗啹溴婘麵凹麂昄唚膷噖䔇施唍PostgreSQL 嚔嘪䫘臖䌂傋

SELECT @ '-4.5' AS "abs";
 abs
-----
 4.5
(1 row)

溴崇係䂘婘庫䫘锬垔䔇淉嘩严幋嬉欓臯䌂婔渇 textfloat8 䔇锊嚟蘸扵㔗潏傸埇傖黯臕垄滇 float8 蔯婉滇噽垄䌂傋

SELECT @ '-4.5e500' AS "abs";

ERROR:  "-4.5e500" is out of range for type double precision

埥婔桹麵嬉䚔淉嘩严 ~(毬嘉埡埉)埻婺昘昄昄扞䌂傋垔幬蔯婉婺 float8 垔幬㔗啹溴套悩潏傸䫘 ~ 啔䌂嚚䔇垂黯儖冖彄

SELECT ~ '20' AS "negation";

ERROR:  operator is not unique: ~ "unknown"
HINT:  Could not choose a best candidate operator. You may need to add explicit
type casts.

認滇啹婺係䂘方濘喿垔庹婻埇脘䔇 ~ 淉嘩严婺䷽䆘庫臖嘪䫘巻婔婻㔗潏傸埇傖䫘滯䇞婄䌂傋蘸扵準婞垄

SELECT ~ CAST('20' AS int8) AS "negation";

 negation
----------
      -21
(1 row)

劯锔饡釕嬉誕
楗誄婪婔亓庘昄