29 Ocak 2019 Salı

Sütun Tipleri - VARCHAR - Variable Length Standart Sütun Tipi

Giriş
Hibernate ile DDL üretiyorsak değişken tipimiz String ise varchar(255) olarak sütun yaratılır.

CHARACTER VARYING
Açıklaması şöyle. Yani VARCHAR ve CHARACTER VARYING aynı şey
In PostgreSQL, VARCHAR is an alias for CHARACTER VARYING. This means that you can use either CHARACTER VARYING or VARCHAR to define a column that stores variable-length character strings. However, CHARACTER VARYING is the preferred term in PostgreSQL.
VARCHAR Tipe LENGTH Verirsek
Açıklaması şöyle. Yani en fazla 10 milyon küsur bir sayı verilebilir
The limit for varchar when you specify a length limit (type modifier) is 10485760
Görmek için şöyle yaparız. Burada LENGTH  olarak 10 milyar veriliyor ve hata alıyoruz
SELECT 'x'::varchar(1000000000);
ERROR:  length for type varchar cannot exceed 10485760
VARCHAR Tipe LENGTH Vermezsek
Bir yazıda şöyle bir cümle geçiyordu. Diğer veri tabanlarında LENGTH vermezsek varsayılan değer 1.
In SQL, if you declare a string in any of the four formats, CHAR, NCHAR, VARCHAR, or NVARCHAR, without specifying its length, the string is given the length of one character. If you coerce a string using CAST or CONVERT and make the same mistake, it gets a length of thirty characters. Why one and thirty? Historical reasons only, but other RDBMSs react in similar ways.
Ancak PostgreSQL açısından durum farklı. Açıklaması şöyle. Yani length vermezsek, TEXT gibi davranır.
If you do not specify the n integer for the varchar data type, it behaves like the text data type. The performance of the varchar (without n) and text are the same.
CHAR vs VARCHAR 
Başka veri tabanlarında CHAR ve VARCHAR arasında performans farkı olabiliyor. Örneğin SQL Server için açıklama şöyle
Because of the fixed field lengths, data is pulled straight from the column without doing any data manipulation and index lookups against varchar are slower than that of char fields. CHAR is better than VARCHAR performance wise, however, it takes unnecessary memory space when the data does not have a fixed-length. So in cases where disk size is not an issue, it is recommended to use CHAR.
Ancak PostgreSQL için performans farkı yok. Açıklaması şöyle
CHAR and VARCHAR are implemented exactly the same in Postgres (and Oracle). There is no difference in speed when using those data types.

Hiç yorum yok:

Yorum Gönder