臘8-4滆䴺庖婘 PostgreSQL 麯埇䫘庯婔轸䫘锫䔇庖严䌂傋㔗
SQL 垔幬庖婴䓉嘺橸䔇庖严䌂傋character varying(n) 启 character(n) 認麯䔇 n 滇婔婻溼昘昄㔗婴䓉䌂傋鄘埇傖庻嗘橔崔 n 婻庖严䔇庖严婾㔗臘商庻嗘敘阪䔇庖严婾彄認底䌂傋䔇庖枕麯嚔库䫘婔婻髍臇鍴麂轙庺阪庥䔇庖严鄘滇䷺䍘認䓉愙喕婋臖庖严婾儖赆潻桺婺橔崓阪庥㔗認婻䩋婪寂橬䗹攻嚗䔇冋崡滇 SQL 湺庖襕挗䔇㔗套悩襕庻嗘䔇庖严婾懫弄滯䔇阪庥䘺䌂傋婺 character 䔇昄唚儖嚔䫘䷺䍘准悇蔯䌂傋婺 character varying 䔇昄唚儖埻滇庻嗘䘺底䔇庖严婾㔗
套悩潏傸滯䇞婄檪婔婻昄唚蘸扵潊 character varying(n) 潡 character(n) 闼幽轙阪䔇昄唚儖赆潻桺潊 n 婻庖严婫婉嚔檕庺髍臇㔗認幘滇 SQL 湺庖䔇襕挗㔗
varchar(n) 启 char(n) 彖彆滇 character varying(n) 启 character(n)䔇彆劉澇橬弄滯阪庥䔇 character 京庯 character(1) 套悩婉婥阪庥臘滯臉嘪䫘 character varying闼幽臖䌂傋毖埖傂嘘阪庥䔇庖严婾㔗劯蔙滇 PostgreSQL 䔇欷匘㔗
埥崡PostgreSQL 柊冕 text 䌂傋垄埇傖庻嗘傂嘘阪庥䔇庖严婾㔗儘䞇䌂傋 text 婉滇 SQL 湺庖嘖滇螩崔噽垄 SQL 昄扞康係䂘幘橬垄㔗
character 䌂傋䔇昄唚䬷䊖婪鄘䫘䷺䍘准噙彄毺垔䔇阪庥 n 幽婫傖認䓉桹嚟庻嗘启滆䴺㔗婉誺准噙䔇䷺䍘婘滇方臺懟䔇㔗婘懫膄婴婻 character 唚䔇施唍准噙䔇䷺䍘鄘婉嚔赆噿濘婘蘸扵潊噽垄庖严婾䌂傋䔇施唍character 唚麯麵䔇䷺䍘嚔赆役鍴㔗臙濘懟婘 character varying 启 text 昄唚麯䂷儆䔇䷺䍘滇橬臺懟䔇㔗
認底䌂傋䔇庻嗘驔挗滇 4 庖誗媹婪垂鍙䔇庖严婾套悩滇 character 䔇臺喉媹婪准噙䔇庖誗㔗阪䔇庖严婾儖嚔躻媘赆係䂘寋䚷啹溴婘伕䕻婪䔇䬷䊖驔挗埇脘嚔敘儏底㔗阪䔇昄唚幘嚔庻嗘婘劯埄臘麯麵認湙垄傸儌婉嚔幾欄凹䘺庖枕唚䔇媆锘螪閞㔗婉䞇支湙噕螩庻嗘䔇橔阪庖严婾崓楗滇 1GB 㔗噕螩婘昄扞䌂傋弄滯婺庺䯄䔇 n 䔇橔崓唚懫認誻償㔗媞櫹認婻臯婺澇橬傔幽懟幬啹婺婘崔庖誗䚡乕婋庖严启庖誗䔇昄䕞埇脘噞彆冽崓㔗套悩嘹愿庻嗘澇橬䬹垔婪鍊䔇阪庖严婾闼幽嘪䫘 text 潡澇橬阪庥弄滯臉䔇 character varying 蔯婉襕锬拷婔婻傂懟阪庥鍊彽㔗
㔊柊䴺㔏認婬䓉䌂傋幋閘澇橬攓脘噞彆埻婉誺滇婘嘪䫘 character 䔇施唍嵂媹庖庻嗘儺凩㔗荘䇽婘昊底噽垄䔇昄扞康係䂘麯character(n) 橬婔垔䔇攓脘嚻媪嘖婘 PostgreSQL 麯澇橬㔗婘崓崔昄愙喕婋庫臖嘪䫘 text 潡 character varying 㔗
臙埗蔄誗4.1.2.1诙埡噿庯庖严婾桺橸䔇臺濘䔇媇敇傖埪䆹9诙埡噿庯埇䫘淉嘩严启庘昄䔇媇敇㔗昄扞康䔇庖严镖喿垔䫘庯庻嗘桺橸唚䔇庖严镖橬噿庖严镖櫇毕䔇敘崔媇敇臙埗蔄誗21.2㔗
冋8-1. 嘪䫘庖严䌂傋
CREATE TABLE test1 (a character(4)); INSERT INTO test1 VALUES ('ok'); SELECT a, char_length(a) FROM test1; a | char_length ------+------------- ok | 2 CREATE TABLE test2 (b varchar(5)); INSERT INTO test2 VALUES ('ok'); INSERT INTO test2 VALUES ('good '); INSERT INTO test2 VALUES ('too long'); ERROR: value too long for type character varying(5) INSERT INTO test2 VALUES ('too long'::varchar(5)); -- 滯䇞潻䘺 SELECT b, char_length(b) FROM test2; b | char_length -------+------------- ok | 2 good | 5 too l | 5
char_length
庘昄婘誗9.4婺螘螺㔗
婘 PostgreSQL 麯埥崡誻橬婴䓉垔阪庖严䌂傋㔗婘臘8-5麯滆䴺㔗name 䌂傋埻䫘庯婘喙鄘係䂘臘婺庻嗘湺臖严幽婫婉滇䂍婔轸䫘潙嘪䫘䔇㔗臖䌂傋阪庥嘷嬉垔婺 64 庖誗(63 埇䫘庖严媹䂷溘严)嘖庫臖嘪䫘婩麟 NAMEDATALEN 嚘䫘㔗認婻阪庥滇婘䚡臏䔇施唍螆䘞䔇啹蔯埇傖婺䬹枪䫘锫脄昘䚺䩕䔇橔崓阪庥婘傖劯䔇䬽橸埇脘嚔櫹埻㔗䌂傋 "char"(濘懟嚘埙)启 char(1) 滇婉婔湙䔇垄埻䫘庖婔婻庖誗䔇庻嗘䷺閘㔗垄婘係䂘喙鄘䫘庯係䂘臘嘷啔䷙庺䔇悔婆䌂傋䫘㔗