18 Temmuz 2018 Çarşamba

MySQL Sütun Tipleri

Sütun Tipleri

date tipi
Şöyle yaparız. Bu sütün tipi için default değer olarak Now(), Current_Date() gibi metodlar kullanılamaz.
date DEFAULT NULL
datetime tipi
Açıklaması şöyle
The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
Diğer veritablarındaki karşılığı şöyle
MySQL: DATETIME(6)
Postgres: TIMESTAMP WITHOUT TIME ZONE
Microsoft SQL: DATETIME
Şöyle yaparız
updated_on datetime NOT NULL
MySQL 5.5'te noktadan sonra kaç tane hane istediğimizi belirtemiyoruz. Şu satır hata verir. İleriki sürümlerde ise çalışır.
updated_on datetime(6) NOT NULL
Default değer için şöyle yaparız.
created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
Açıklaması şöyle. Eğer sütuna null değer vermeye çalışırsak sunucunun saatini kullanılır.
this will ensure that every record inserted will default to the current server date and time
decimal tipi
Şöyle yaparız. Para tipi veri için çok uygundur
DECIMAL(10)
double tipi
Şöyle yaparız.
double NOT NULL DEFAULT '0'
mediumtext tipi
text tipine bakınız.


text tipi
Java'daki String tipine denk gelir.

Açıklaması şöyle. İsmi text olsa da aslında en fazla 65535 byte saklayabilir. Kaç karaktere denk geldiği kullanılan encoding ile değişebilir.
In MySQL, TEXT is 64K bytes. You might be better off with MEDIUMTEXT, which has a limit of 16MB. I say "bytes" because, for example, Chinese needs 3, sometimes 4, bytes per character, so only about 25K characters of Chinese text will fit in TEXT.
Açıklaması şöyle.
Variable-length string types are stored using a length prefix plus data. The length prefix requires from one to four bytes depending on the data type, and the value of the prefix is L (the byte length of the string). For example, storage for a MEDIUMTEXT value requires L bytes to store the value plus three bytes to store the length of the value.
VARCHAR, VARBINARY, and the BLOB and TEXT types are variable-length types. For each, the storage requirements depend on these factors:
  • The actual length of the column value
  • The column's maximum possible length
  • The character set used for the column, because some character sets contain multibyte characters
Sütunu yaratırken şöyle yaparız.
TEXT CHARACTER SET utf8mb4
time tipi
Şöyle yaparız.
time DEFAULT NULL

varchar tipi
Şöyle yaparız.
varchar(50)
Kısıt koymak istersek şöyle yaparız.
varchar(250) COLLATE utf8mb4_bin NOT NULL
Diğer Notlar
identity
auto_increment yerine identity de kullanılabilir. Aralarıda ne fark var bilmiyorum. Şöyle yaparız.
CREATE TABLE foo (
  id int NOT NULL IDENTITY(1,1),
  ...
);



Hiç yorum yok:

Yorum Gönder