当前位置:网站首页>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 type| character 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 .
边栏推荐
- awk处理JSON处理
- 嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
- Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
- 数值法求解最优控制问题(〇)——定义
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- Codesonar enhances software reliability through innovative static analysis
- [uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
- Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
- 反诈困境,国有大行如何破局?
- Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
猜你喜欢

Tensorflow2. How to run under x 1 Code of X

Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
Mysql子查询关键字的使用方式(exists)

目标:不排斥 yaml 语法。争取快速上手
CodeSonar通过创新型静态分析增强软件可靠性

软件缺陷静态分析 CodeSonar 5.2 新版发布

最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
MySQL约束之默认约束default与零填充约束zerofill

【C语言】指针进阶---指针你真的学懂了吗?

使用枚举实现英文转盲文
随机推荐
Codesonar Webinar
恶魔奶爸 B3 少量泛读,完成两万词汇量+
[uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
Unity3d 4.3.4f1 execution project
How to meet the dual needs of security and confidentiality of medical devices?
如何满足医疗设备对安全性和保密性的双重需求?
Is it safe to open an account of BOC shares in kainiu in 2022?
Implementation of mahout Pearson correlation
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
程序猿赚的那点钱算个P啊!
MinGW MinGW-w64 TDM-GCC等工具链之间的差别与联系「建议收藏」
Tensorflow2. How to run under x 1 Code of X
Mysql子查询关键字的使用方式(exists)
Codeforces Round #275 (Div. 2) C – Diverse Permutation (构造)[通俗易懂]
私募基金在中国合法吗?安全吗?
CodeSonar网络研讨会
UVA 11080 – Place the Guards(二分图判定)
Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
Deployment, recall and deletion solutions - stsadm and PowerShell "suggestions collection"
DataTable数据转换为实体