2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MySQLのデータ型について

Posted at

MySQLのデータ型についてまとめた記事です。
全部で35種類

整数型:5種類
(1) TINYINT
-128 ~ 127
※ Booleanを指定した場合,TINYINTの0(false)、1(true)となる

(2) SMALLINT
-32768 ~ 32767

(3) MEDIUMINT
-8388608 ~ 8388607	

(4) INT
-2147483648 ~ 2147483647

(5) BIGINT
-2^63 ~ 2^63-1
固定小数点型:1種類
(6) DECIMAL
DECIMAL(M, N) 全部でM桁で少数部がN桁
未指定の場合はデフォルト10桁
最大桁数は 65
※ NUMERICを指定した場合、DECIMALに変換される
浮動小数点型:2種類
(7) Float
0 から 23 の精度は、4 バイト単精度

(8) DOUBLE
24 から 53 の精度は、8 バイト倍精度
※ REALを指定した場合、DOUBLEに変換される
ビット値型:1種類
(9) BIT
BIT(M) Mは桁数で、1~64桁の範囲
日時型:5種類
(10) DATE
'1000-01-01' ~ '9999-12-31'

(11) TIME
'HH:MM:SS' 
'-838:59:59' ~ '838:59:59' 

(12) DATETIME
'1000-01-01 00:00:00' ~ '9999-12-31 23:59:59'

(13) TIMESTAMP
'1970-01-01 00:00:01' UTC ~ '2038-01-19 03:14:07' UTC 

(14) YEAR
4文字または2文字の表示幅を指定できる
・YEAR(4)
1901 ~ 2155の範囲の4桁の数値。
'1901' ~ '2155'の範囲の4桁の文字列。
'0'または'00'の場合は、2000年として扱う
  
・YEAR(2)
1~69の場合は、2001~2069のYEAR値に変換。
70~99の場合は、1970~1999のYEAR値に変換。
'0'~'69'の場合は、 2000~2069のYEAR値に変換。 
'70'~'99'の場合は、1970~1999のYEAR値に変換。
00の場合は、2000年として扱う
文字列型:2種類
(15)CHAR
0 ~ 255文字
指定された長さになるように右側がスペースで埋められる。

(16)VARCHAR
0 ~ 65,535文字
バイナリデータ型:10種類
(17) BINARY
最大長255バイト

(18) VARBINARY
最大長65,535バイト

(19) TINYBLOB
最大長255バイト

(20) BLOB
最大長65,535バイト
BLOB(M) でバイト数を指定できる

(21) MEDIUMBLOB
最大長16,777,215バイト

(21) LONGBLOB
最大長4,294,967,295バイト

(22) TINYTEXT
最大長255バイト

(23) TEXT
最大長65,535バイト

(24) MEDIUMTEXT
最大長16,777,215バイト

(25) LONGTEXT
最大長4,294,967,295バイト

列挙型:2種類
(26) ENUM
最大65,535個

(27) SET
最大64個
空間データ型:8種類
(28) Geometry(単一のジオメトリ値)
GEOMETRY にはどの型のジオメトリ値でも格納できる。

(29) Point(単一のジオメトリ値)
座標

(30) LineString(単一のジオメトリ値)
河川や通りなど

(31) Polygon(単一のジオメトリ値)
森林や区域など

(32) GeometryCollection
GeomCollection は、任意のクラスのゼロ個以上のジオメトリの集合であるジオメトリ

(33) MultiPoint
一連の小さな島々やチケットオフィスの系列店など

(34) MultiLineString
河川系や高速道路システムなど

(35) MultiPolygon
湖の系列など
数値テーブルの型確認
DROP TABLE NumColumn;
CREATE TABLE IF NOT EXISTS NumColumn(
    num1 TINYINT,
    num2 SMALLINT,
    num3 MEDIUMINT,
    num4 INT,
    num5 BIGINT,
    num6 DECIMAL,
    num7 NUMERIC,
    num8 FLOAT,
    num9 DOUBLE PRECISION,
    num10 REAL,
    num11 BIT
);
show columns from NumColumn;
日時テーブルの型確認
DROP TABLE DateColumn;
CREATE TABLE IF NOT EXISTS DateColumn(
    d1 DATE,
    d2 TIME,
    d3 DATETIME,
    d4 TIMESTAMP,
    d5 YEAR
);
show columns from DateColumn;
文字列テーブルの型確認
DROP TABLE CharColumn;
CREATE TABLE IF NOT EXISTS CharColumn(
    ch1 CHAR,
    ch2 VARCHAR(32)
);
show columns from CharColumn;
バイナリデータテーブルの型確認
DROP TABLE BinaryColumn;
CREATE TABLE IF NOT EXISTS BinaryColumn(
    bi1 BINARY,
    bi2 VARBINARY(32),
    bi3 TINYBLOB,
    bi4 BLOB,
    bi5 MEDIUMBLOB,
    bi6 LONGBLOB,
    bi7 TINYTEXT,
    bi8 TEXT,
    bi9 MEDIUMTEXT,
    bi10 LONGTEXT
);
show columns from BinaryColumn;
列挙型データテーブルの型確認
DROP TABLE SEColumn;
CREATE TABLE IF NOT EXISTS SEColumn(
    se1 ENUM('one', 'two', 'three', 'four'),
    se2 SET('1', '2', '3', '4')
);
show columns from SEColumn;
空間データテーブルの型確認
DROP TABLE GeoColumn;
CREATE TABLE IF NOT EXISTS GeoColumn(
    g1 Geometry,
    g2 Point,  
    g3 LineString,
    g4 Polygon,
    g5 GeometryCollection,
    g6 MultiPoint,
    g7 MultiLineString,
    g8 MultiPolygon
);
show columns from GeoColumn;
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?