postgres

 Character Types

character varying(_`n`_)varchar(_`n`_)variable-length with limit
character(_`n`_)char(_`n`_)bpchar(_`n`_)fixed-length, blank-padded
bpcharvariable unlimited length, blank-trimmed
textvariable unlimited length

char (bpchar) khác varchar(character varying) ở chỗ nếu set char(10) thì input chỉ có 8 ký tự, kết quả vẫn được lưu 10 ký tự(2 ký tự thừa sẽ tự đồng chèn khoảng trắng vào) còn varchar thì kết quả vẫn được lưu 8 ký tự.

textvarchar gần như k có sự khác biệt về performance. varchar(n) sẽ limit được input đầu vào k vượt quá n. string - Difference between text and varchar (character varying) - Stack Overflow

To sum it all up:

  • char(n) – takes too much space when dealing with values shorter than n (pads them to n), and can lead to subtle errors because of adding trailing spaces, plus it is problematic to change the limit
  • varchar(n) – it’s problematic to change the limit in live environment (requires exclusive lock while altering table)
  • varchar – just like text
  • text – for me a winner – over (n) data types because it lacks their problems, and over varchar – because it has distinct name

Tip

There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(_`n`_) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(_`n`_) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.