PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹8. Data Types媆誕嬉誕

8.1. 昄唚䌂傋

昄唚䌂傋䫌 2 㔕4 潡 8 庖誗䔇昘昄傖埪 4 潡 8 庖誗䔇敞䗹昄启埇锬來庥䔇償昄䂇潊㔗臘8-2彖庺庖欔橬埇䫘䌂傋㔗

臘8-2. 昄唚䌂傋

劉庖庻嗘䷺閘柟誄评啘
smallint2 庖誗償评啘昘昄-32768 彄 +32767
integer4 庖誗婩䫘䔇昘昄-2147483648 彄 +2147483647
bigint8 庖誗崓评啘䔇昘昄-9223372036854775808 彄 9223372036854775807
decimal埻阪䫘潙弄滯來庥來䇞方鍊彽
numeric埻阪䫘潙弄滯來庥來䇞方鍊彽
real4 庖誗埻來庥婉來䇞6 嘉剕誕彽昄庖來庥
double precision8 庖誗埻來庥婉來䇞15 嘉剕誕彽昄庖來庥
serial4 庖誗躻嵂昘昄1 彄 2147483647
bigserial8 庖誗崓评啘䔇躻嵂昘昄1 彄 9223372036854775807

昄唚䌂傋婩麟䔇臺濘婘誗4.1.2麯柟誄㔗昄唚䌂傋凹庫橬婔喖垯昘䔇昄庥淉嘩严启庘昄㔗䕩噿媇敇臙埗蔄䆹9㔗婋麵䔇庹誗臥䂖柟誄認底䌂傋㔗

8.1.1. 昘昄䌂傋

smallint, integer, bigint 䌂傋庻嗘劇䓉评啘䔇噘鄘滇昄庖䔇昄幘儌滇澇橬償昄鄘彖䔇昄庖㔗臘商庻嗘轙庺评啘傖崡䔇昄唚儖凚躘婔婻髍臇㔗

婩䫘䔇䌂傋滇 integer 啹婺垄柊冕庖婘评啘㔕庻嗘䷺閘㔕攓脘幋閘䔇橔嘿广臇㔗婔轸埻橬婘伕䕻䷺閘䘓嚹䔇施唍欉嘪䫘 smallint 㔗蔯埻橬婘 integer 䔇评啘婉崘䔇施唍欉嘪䫘 bigint 啹婺嬉蔙䂺凹媆冖崔㔗

bigint 䌂傋埇脘婉滇婘欔橬广埄婪鄘誊蘸溼䇞啹婺垄冺蕡䚡臏単凹噆庖誗昘昄䔇櫇毕㔗婘闼底澇橬噆庖誗昘昄櫇毕䔇橺単婪bigint 䔇嘩䫘启 integer 婔湙(嘖滇傉䇽剹扞噆庖誗庻嗘)㔗婉誺潏傸䕞嬉誻澇劸臘誺橬認湙䔇广埄㔗

SQL 埻弄滯庖昘昄䌂傋 integer(潡 int) 启 smallint 㔗䌂傋 bigint 启䌂傋彆劉 int2, int4, int8 鄘滇欷匘幽婫幘婘螩崔噽垄 SQL 昄扞康係䂘婺嘪䫘㔗

8.1.2. 傂懟來庥昄唚

numeric 䌂傋埇傖庻嗘橔崔 1000 嘉來庥䔇昄庖幽婫庖䇞婄誕臯螇䞖㔗潏傸䬹彆傺螞儖垄䫘庯蘓婕麏鵺启噽垄襕挗來䇞螇䞖䔇婺劽㔗婉誺numeric 䌂傋婪䔇䞖橇誊䞖懫昘昄䌂傋潡蔙潏傸婋婔誗柟誄䔇敞䗹昄䌂傋襕晵冽崔㔗

婘锟劯䔇喙垹麯潏傸嘪䫘婋誄橇臺婔婻 numeric 䌂傋䔇湺庥(scale)滇償昄鄘彖䔇嘉昄來庥(precision)滇噘鄘昄扞嘉䔇昄䕞幘儌滇償昄䗹婴膹䔇嘉昄攂启㔗啹溴昄庖 23.5141 䔇來庥婺 6 蔯湺庥婺 4 㔗嘹埇傖螴婺昘昄䔇湺庥婺镽㔗

numeric 庖枕䔇橔崓來庥启橔崓湺庥鄘滇埇傖陉䘞䔇㔗襕弄滯婔婻庖枕䔇䌂傋婺 numeric 嘹埇傖䫘婋麵䔇臺濘

NUMERIC(precision, scale)

來庥媙釂婺溼昄湺庥埇傖婺镽潡蔙溼昄㔗埥崡

NUMERIC(precision)

锬拷庖湺庥婺 0 㔗婉婥傂嘘來庥婯湺庥䔇弄滯

NUMERIC

彍录傺婔婻埇傖庻嗘婔婻䕘彄垂䯄來庥婪鍊䔇傂懟來庥启湺庥䔇昄唚婔婻認湙䌂傋䔇庖枕儖婉嚔檪膷噖昄唚蘸寡潊傂嘘䬹垔䔇湺庥蔯婥橬湺庥弄滯䔇 numeric 庖枕儖檪膷噖唚蘸寡婺臖湺庥㔗SQL 湺庖襕挗䚺䩕䔇湺庥滇 0(幘儌滇蘸寡潊昘昄來庥)㔗潏傸蓬冖認湙啔橬䗹澇䫘㔗套悩嘹噿媄䓂洉攓闼嘹橔喘攂滇滯䇞弄滯來庥启湺庥㔗

套悩婔婻襕庻嗘䔇昄唚䔇湺庥懫庖枕弄滯䔇湺庥醻闼幽係䂘儖儺臘婖昘(啕轉庫噖)臖昄唚彄毺垔䔇償昄嘉㔗䇽劯套悩償昄䗹噥膹䔇昄扞嘉昄轙誺庖弄滯䔇來庥废寂弄滯䔇湺庥闼幽儖檕庺婔婻髍臇㔗

numeric 䌂傋䔇昄扞唚婘䬷䊖婪滇婉婥傂嘘嬉凚潡蔙劯䚔镽䔇嘵嚟庻嗘䔇㔗啹溴庖枕婪弄滯䔇來庥启湺庥鄘滇橔崓唚蔯婉滇啺垔彖陉䔇㔗婘認婻桹麵numeric 䌂傋敘䌂嚚庯 varchar(n) 蔯婉滇 char(n) 㔗垂鍙庻嗘滇懟啕婻剕誕彽嘉婴婻庖誗䇽劯婘昘婻昄扞婪媹婪噆婻庖誗䔇鵺崡嚔體㔗

鍴庖捞锔䔇昄庖唚幋崡numeric 䌂傋噕螩䫘䬹枪唚 NaN 臘䴺"婉滇婔婻昄庖"㔗傂嘘婘 NaN 婪麵䔇淉嘩鄘䫘潊埥崡婔婻 NaN 㔗套悩婘 SQL 变傴麯檪認底唚嘷嘩婔婻婩麟喍嘹媙釂婘噽变啘櫆婪剘嚘埙懫套 UPDATE table SET x = 'NaN' 㔗婘膷噖施庖严婾 NaN 滇崓償喍方噿䔇㔗

䌂傋 decimalnumeric 滇京昽䔇㔗婴䓉䌂傋鄘滇 SQL 湺庖㔗

8.1.3. 敞䗹昄䌂傋

昄扞䌂傋 realdouble precision 滇婉來䇞䔇㔕埻來庥䔇昄庖䌂傋㔗垂鍙婪認底䌂傋滇 IEEE 754 湺庖庯誕彽敞䗹昄䞖橇(彖彆凹庫剘启埯來庥)䔇婔轸垂䯄崡媹婋北崇䊖単㔕淉嘩係䂘启䚡臏単凹垄䔇櫇毕㔗

婉來䇞懟叿五婔底昄唚婉脘來䇞婄蘸扵潊喙鄘湚嚟幽婫滇傖誏嚚唚庻嗘䔇啹溴庻嗘劯喉檪昄扞欷剄庺準埇脘橬婔底噞嚗㔗崇䊖認底髍臇傖埪認底髍臇滇套嘘婘螇䞖婺嚹携䔇匂庯昄庥启螇䞖橺䓏庥䔇婔婻垯昘䔇彖櫇潏傸婉嚔婘認麯誕婔準螘螺垄認麯䔇螘螺備鍊庯套婋庹䗹

婘崓崔昄广埄婪real 䌂傋䔇评啘滇躿儏 1E-37 彄 1E+37 來庥躿儏滇 6 嘉償昄㔗double precision 䔇评啘锔婩滇 1E-307 彄 1E+308 來庥滇躿儏 15 嘉昄庖㔗崻崓潡蔙崻償䔇昄唚鄘嚔凚躘髍臇㔗套悩膷噖昄扞䔇來庥崻醻闼幽儖嚔埏䫘啺昘㔗崻毖誏镽䔇昄庖套悩方濘婯镽唚䔇臘䯄嘵嚟䕩寺彖儌嚔库䫘婋溵髍臇㔗

鍴庖捞锔䔇昄庖唚幋崡敞䗹䌂傋誻橬庹婻䬹枪唚

Infinity
-Infinity
NaN

認底唚彖彆臘䴺 IEEE 754 䬹枪唚"溼方䷙崓"㔕"蘘方䷙崓"㔕"婉滇婔婻昄庖"㔗婘婉镕冻 IEEE 754 敞䗹䞖橇䔇橺単婪認底唚䔇劆幬埇脘婉滇鵇橘䔇㔗套悩婘 SQL 变傴麯檪認底昄唚嘷嘩婩麟喍嘹媙釂婘垄傸变啘櫆婪剘嚘埙償認湙UPDATE table SET x = 'Infinity' 㔗膷噖施認底唚滇傖崓償喍方噿䔇桹嚟臖彆䔇㔗

PostgreSQL 誻櫇毕 SQL 湺庖臘䴺濘 floatfloat(p) 䫘庯弄滯麂來䇞䔇昄唚䌂傋㔗噽婺䔇 p 弄滯傖庯誕彽嘉臘䴺䔇橔嘯埇毖埖來庥㔗婘锬埡 real 䌂傋䔇施唍PostgreSQL 毖埖 float(1)float(24)婘锬埡 double precision 䔇施唍毖埖 float(25)float(53) 㔗婘噕螩评啘幋崡䔇 p 唚儖凚躘婔婻髍臇㔗澇橬弄滯來庥䔇 float 儖赆嘷嘩 double precision

㔊濘懟㔏PostgreSQL 7.4傖嬉婘 float(p) 麯麵䔇來庥嚔赆嘷嘩滇認幽崔嘉昄䔇剕誕彽嘉㔗彄 7.4 噾䂟赆媞櫹潊婯 SQL 湺庖对陉湺庖弄滯認婻來庥滇傖庯誕彽嘉庥麟䔇㔗啺螆 realdouble precision 彖彆橬 24 启 53 婻庯誕彽嘉䔇嘉昄凹 IEEE 湺庖䔇敞䗹垂䯄準臘滇溼䇞䔇㔗婘麂 IEEE 广埄婪認婻昄唚埇脘䘖橬啟噞嘖滇婺庖䞔寡潏傸婘欔橬广埄婪鄘䫘庖劯湙䔇 p 唚评啘㔗

8.1.4. 废彖埙䌂傋

serialbigserial 䌂傋婉滇䩘溼䔇䌂傋埻滇婺婘臘婺螆䘞嫇婔湺臖啔䔇楗媕婪䔇冪彷㔗䌂嚚噽垄婔底昄扞康婺䔇 AUTO_INCREMENT 匂攓㔗婘䕞嬉䔇垂䯄婺婋麵婔婻臺埖

CREATE TABLE tablename (
    colname SERIAL
);

京備庯弄滯婋麵庹婻臺埖

CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
    colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

啹溴潏傸儌录傺庖婔婻昘昄庖枕幽婫檪垄䔇䚺䩕昄唚垬毐婺傯婔婻废彖埏䫘単臂埡㔗庫䫘庖婔婻 NOT NULL 亥溘傖䇞媺 NULL 婉嚔赆某噖㔗婘崓崔昄愙喕婋嘹埇脘誻婯橕鍇媹婔婻 UNIQUEPRIMARY KEY 亥溘镪噉懟崡婄某噖麉崉䔇昄唚嘖認婻婉滇躻媘䔇㔗橔劯儖废彖埏䫘単"傯匂庯"闼婻庖枕認湙嘷臖庖枕潡臘赆役鍴䔇施唍幘婔幽役鍴垄㔗

㔊濘懟㔏PostgreSQL 7.3傖嬉serial 锊劆 UNIQUE 㔗嘖䯄婘婉喉套溴㔗套悩嘹婯橕婔婻废彖庖枕橬婔婻嫇婔亥溘潡蔙婔婻婂髞闼幽嘹䯄婘媙釂弄滯儌償噽垄昄扞䌂傋婔湙㔗

襕婘 serial庖枕婺某噖废彖婺䔇婋婔婻昄唚婂襕滇襕濘懟 serial庖枕庫臖蕋庽䚺䩕唚㔗潏傸埇傖锔誺婘 INSERT 臺埖婺檪臖庖枕毐鍴婘庖枕彖臘幋崡準垂䯄幘埇傖锔誺嘪䫘 DEFAULT 噿髞庖準垂䯄㔗

䌂傋劉 serialserial4 滇京昽䔇婴蔙鄘录傺 integer 庖枕㔗䌂傋劉 bigserialserial8 幘婔湙埻婉誺垄录傺婔婻 bigint 庖枕㔗套悩嘹鵇螇婘臘䔇䫘庻橘婺嘪䫘䔇湺臖昄䕞埇脘轙誺 231 婻闼幽嘹庫臖嘪䫘 bigserial

婔婻 serial 䌂傋录傺䔇废彖婘欔匂䔇庖枕赆役鍴䔇施唍躻媘役鍴㔗嘹埇傖埻役鍴废彖蔯婉役鍴庖枕婉誺認儖役鍴臖庖枕䔇䚺䩕唚臘膆嚟㔗


劯锔饡釕嬉誕
昄扞䌂傋婪婔亓蘓婕䌂傋