当前位置:网站首页>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 .
边栏推荐
- gridView自己定义做时间排版「建议收藏」
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- easyui 日期控件清空值
- uva 12230 – Crossing Rivers(概率)「建议收藏」
- Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
- 权限不足
- 恶魔奶爸 B1 听力最后壁垒,一鼓作气突破
- 开户还得用身份证银行卡安全吗,我是小白不懂
- MySQL约束之默认约束default与零填充约束zerofill
- 寫一下跳錶
猜你喜欢
随机推荐
DataTable数据转换为实体
Mahout-Pearson correlation的实现
UVA 11080 – Place the Guards(二分图判定)
Unity3d 4.3.4f1执行项目
Écrivez une liste de sauts
程序猿赚的那点钱算个P啊!
Tensorflow2.x下如何运行1.x的代码
gridView自己定义做时间排版「建议收藏」
I have to use my ID card to open an account. Is the bank card safe? I don't understand it
刚开户的能买什么股票呢?炒股账户安全吗
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
Guava multithreading, futurecallback thread calls are uneven
easyui 日期控件清空值
CodeSonar网络研讨会
Is it safe to open a stock account at present? Can I open an account online directly.
Datatable data conversion to entity
object-c编程tips-timer「建议收藏」
Mysql子查询关键字的使用方式(exists)
Is it safe to open an account of BOC shares in kainiu in 2022?
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》