当前位置:网站首页>Description of the difference between character varying and character in PostgreSQL database
Description of the difference between character varying and character in PostgreSQL database
2022-07-07 21:13:00 【1024 questions】
Postgresql character varying and character The difference between
SQL standard
postgreSQL Character type
Postgresql Of character varying = bytea problem
Postgresql character varying and character The difference between SQL standardSQL Two basic character types are defined :character varying(n) and character(n) , there n Is a positive integer . Both types can store up to n Character string .
Trying to store longer strings into these types of fields produces an error , Unless the characters beyond the length are blank , In this case, the string will be truncated to the maximum length .
The odd looking exception is SQL Standard requirements . If the string to be stored is shorter than the declared length , The type is character Will be filled with blanks ; And the type is character varying The value of will just store a shorter string .
If we explicitly convert a number to character varying(n) or character(n) , Then the super long value will be truncated to n Characters , And it doesn't throw an error . This is also SQL Standard requirements .
postgreSQL Character typecharacter varying(n), varchar(n) | Lengthening , Limited length |
character(n), char(n) | Fixed length , Fill in the blanks |
text | Lengthening , No length limit |
Length difference
varchar(n) and char(n) Namely character varying(n) and character(n) Another name for , No declaration of length character be equal to character(1) ;character varying Use without length , Then the type accepts strings of any length . The latter is PostgreSQL An extension of .
in addition ,PostgreSQL Provide text type , It can store strings of any length . Even though text The type is not SQL standard , But many SQL Database systems also have it .
Physical storage
character Data of type , Use spaces in Physics ( original text : space) Fill to the specified length n And store and display in this way . however , The blank filled in is meaningless .
Comparing two character When it's worth it , The blank filled in will be ignored , When converting to other string types ,character The space in the value will be deleted . Please note that , stay character varying and text Type of data , The blank at the end is meaningful .
These types of storage requirements are 4 Bytes plus the actual string , If it is character Add the filled bytes ( The space mentioned above ).
Long strings will be automatically compressed by the system , So there may be less physical requirements on disk . Long strings are also stored in the background table , So they don't interfere with quick access to short fields ( My understanding is that : Disk addressing paths or fewer times ? If you know something, please point out !!!). No matter what , The longest string allowed to be stored is about 1GB .
Allowed in data type declaration n The maximum value of is less than the maximum value . Changing this value is not very useful , Because when using multi byte character encoding , The number of characters and bytes may be completely different .
If you want to store long strings without a specific upper limit , So use text Or without length declarative words character varying , Instead of setting length limits .
Performance differences
There is no performance difference between the three types , Just using character The storage size is increased .
Although in some other database systems ,character(n) There are certain performance advantages , But in PostgreSQL Not in it .
in the majority of cases , You should use text or character varying .
Reference resources :pg 8.2.23 file
Postgresql Of character varying = bytea problemJava Development Postgresql Database compatibility problems , And Oracle There are some differences :
Java Type mapping database types ,Oracle jdbc Driver handling Java String The type can be normally mapped to the Numberic(Integer), and Postgres Then there will be mistakes .
in addition , Is the use hibernate Frequent mistakes : operator non-existent :character varying = bytea problem , Don't be misled by this description , Not at all sql In the sentence character varying = bytea Type comparison , But when using hibernate Use parameter binding , The value of the parameter is null when ,hibernate perhaps Postgresql The driver sets this parameter It maps to varbinary type ,Postgresql take varbinary Consider as bytea type
The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .
边栏推荐
- Deadlock conditions and preventive treatment [easy to understand]
- 写一下跳表
- Measure the height of the building
- 阿洛的烦恼
- 单词反转实现「建议收藏」
- Validutil, "Rethinking the setting of semi supervised learning on graphs"
- Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
- Mysql子查询关键字的使用方式(exists)
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- npm uninstall和rm直接删除的区别
猜你喜欢
目标:不排斥 yaml 语法。争取快速上手
Codesonar Webinar
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
CodeSonar如何帮助无人机查找软件缺陷?
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
神兵利器——敏感文件发现工具
程序猿赚的那点钱算个P啊!
Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
随机推荐
GridView defines its own time for typesetting "suggestions collection"
[concept of network principle]
Implementation of mahout Pearson correlation
Numerical method for solving optimal control problem (0) -- Definition
POJ 3140 contents division "suggestions collection"
Jetty:配置连接器[通俗易懂]
静态测试工具
Referrer和Referrer-Policy简介
Cocos2d-x 游戏存档[通俗易懂]
数值法求解最优控制问题(〇)——定义
AADL Inspector 故障树安全分析模块
uva 12230 – Crossing Rivers(概率)「建议收藏」
OneSpin | 解决IC设计中的硬件木马和安全信任问题
Intelligent transportation is full of vitality. What will happen in the future? [easy to understand]
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
How to choose financial products? Novice doesn't know anything
Intelligent software analysis platform embold
万字总结数据存储,三大知识点
awk处理JSON处理
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization