この記事は公開から3年以上経過しています。
Microsoft® SQL Servre®を使ったDBの物理設計に必要なデータ型とストレージサイズを、備忘がてら一覧に纏めてみました(Microsoft DocsのSQL Server 2019のドキュメントから抽出)。
日時型
データ型 | ストレージサイズ(バイト) | 備考 |
---|---|---|
date | 3 | – |
DATETIME | 8 | – |
datetime2(p) datetime2 = datetime2(7) |
6, 7, 8 | 0 ≦ p ≦ 2 ⇒ 6 3 ≦ p ≦ 4 ⇒ 7 5 ≦ p ≦ 7 ⇒ 8 |
datetimeoffset(p) datetimeoffset = datetimeoffset(7) |
8, 9, 10 | 0 ≦ p ≦ 2 ⇒ 8 3 ≦ p ≦ 4 ⇒ 9 5 ≦ p ≦ 7 ⇒ 10 |
smalldatetime | 4 | – |
time(s) time = time(7) |
4, 5 | 0 ≦ s ≦ 2 ⇒ 3 3 ≦ s ≦ 4 ⇒ 4 5 ≦ s ≦ 7 ⇒ 5 |
数値型
データ型 | ストレージサイズ(バイト) | 備考 |
---|---|---|
bit | 1, 2 | n = テーブル内のビット列数 1 ≦ n ≦ 8 ⇒ 1 9 ≦ n ≦ 16 ⇒ 2 |
decimal(p) numeric(p) |
5, 9, 13, 17 | 1 ≦ p ≦9 ⇒ 5 10 ≦ p ≦19 ⇒ 9 20 ≦ p ≦28 ⇒ 13 29 ≦ p ≦38 ⇒ 17 |
float(n) real = float(24) |
4, 8 | 1 ≦ n ≦24 ⇒ 4 25 ≦ n ≦53 ⇒ 8 |
bigint | 8 | – |
int | 4 | – |
smallint | 2 | – |
tinyint | 1 | – |
money | 8 | – |
smallmoney | 4 | – |
文字列型
データ型 | ストレージサイズ(バイト) | 備考 |
---|---|---|
char(n) | n | 1 ≦ n ≦ 8000 |
varchar(n|max) | 文字列データサイズ + 2 | 1 ≦ n ≦ 8000 max = 2³¹ – 1 |
nchar(n) | n * 2 | nchar(n) 1 ≦ n ≦ 4000 |
nvarchar(n|max) | 文字列データサイズ + 2 | nvarchar(n) 1 ≦ n ≦ 4000 max = 2³¹ – 1 |
ntext | 文字列長 * 2 | Unicode(UCS-2) ≦ 2³⁰ – 1 |
text | 文字列データサイズ | 非Unicode ≦ 2³¹ – 1(文字コード依存) |
※文字列データサイズ=バイト数
バイナリ型
データ型 | ストレージサイズ(バイト) | 備考 |
---|---|---|
binary(n) | n | 1 ≦ n ≦ 8000 |
varbinary(n|max) | データサイズ + 2 | 1 ≦ n ≦ 8000 max = 2³¹ – 1 |
image | データサイズ | ≦ 2³¹ – 1 |
その他
データ型 | ストレージサイズ(バイト) | 備考 |
---|---|---|
rowversion | 8 | – |
hierarchyid | 5〜892 | 平均ファンアウト(ノードの子の平均数)に依存 |
uniqueidentifier | 16 | – |
ちなみに、ntext
, text
, image
型は将来のSQL Serverバージョンで削除予定のため非推奨となっていますので、今後これらの型を利用したい場合はnvarchar(max)
, varchar(max)
, varbinary(max)
を使用する必要があります。
参考ウェブサイトなど
- Microsoft Docs
Transact-SQL Reference (Database Engine)
以上です。