当前位置:网站首页>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

Catalog

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 standard

SQL 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 NameDescription
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 problem

Java 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 .


原网站

版权声明
本文为[1024 questions]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071838135291.html