1.整數(shù)
| 類型 |
范圍 |
存儲(chǔ)大小 |
| tinyint |
-128 ~ 127 |
1字節(jié) |
| tinyint unsigned |
0 ~ 255 |
1字節(jié) |
| smallint |
-32768 ~ 32767 |
2字節(jié) |
| smallint unsigned |
0 ~ 65535 |
2字節(jié) |
| int |
-2147483648 ~ 2147483647 |
4字節(jié) |
| int unsigned |
0 ~ 4294967295 |
4字節(jié) |
| bigint |
-9223372036854775808 ~ 9223372036854775807 |
8字節(jié) |
| bigint unsigned |
0 ~ 18446744073709551615 |
8字節(jié) |
- 整數(shù)類型后接數(shù)字僅代表顯示寬度,不代表存儲(chǔ)長(zhǎng)度。如 int(4) 與 int(10) 一致,固定存儲(chǔ)為4字節(jié),故建表語句中不必填寫
- 用作表主鍵時(shí)應(yīng)該選用
int unsigned 或者 bigint unsigned
create table `example1` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='示例1';
--
create table `example2` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='示例2';
- 使用整數(shù)代替枚舉類
enum能獲取更高的效率,建議選用 tinyint 或者 tinyint unsigned
create table `example3` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
`sex` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '性別:0-女 1-男',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='示例3';
- 計(jì)算機(jī)處理整數(shù)類型比字符串類型快,故存儲(chǔ)IPV4地址時(shí)候可使用
int unsigned,通過inet_ntoa和inet_aton進(jìn)行轉(zhuǎn)化
create table `example4` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
`ip_addr` tinyint unsigned NOT NULL DEFAULT 0 COMMENT 'ipv4地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='示例4';
select inet_aton('209.207.224.40'); --3520061480
select inet_ntoa('3520061480'); --209.207.224.40
2.小數(shù)
| 類型 |
范圍 |
存儲(chǔ) |
特點(diǎn) |
| float(M, D) |
M表示位寬,D是小數(shù)點(diǎn)后位數(shù) |
4字節(jié) |
浮點(diǎn)數(shù)-保存近似值 |
| double(M, D) |
M表示位寬,D是小數(shù)點(diǎn)后位數(shù) |
8字節(jié) |
浮點(diǎn)數(shù)-保存近似值 |
| decimal(M, D) |
M表示總位數(shù),D是小數(shù)點(diǎn)后位數(shù) |
M+2字節(jié) |
定點(diǎn)數(shù)-以字符串型式保存數(shù)值 |
- 建議小數(shù)統(tǒng)一使用 decimal ,尤其是與貨幣、金融相關(guān)的數(shù)據(jù)
create table `example5` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
`price` decimal(10,2) NOT NULL DEFAULT 0 COMMENT '價(jià)格',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='示例5';
3.字符串
| 類型 |
范圍 |
存儲(chǔ) |
| char(n) |
n個(gè)字符(非字節(jié)) |
固定存儲(chǔ)長(zhǎng)度 |
| varchar(n) |
n個(gè)字符(非字節(jié)) |
字符串實(shí)際長(zhǎng)度加上用于記錄長(zhǎng)度的字節(jié) |
- 實(shí)際取值為定長(zhǎng)時(shí)應(yīng)選用
char(n)
- 變長(zhǎng)字符串
varchar(n) 存在用于記錄長(zhǎng)度的字節(jié),故字符串長(zhǎng)度<=255時(shí)僅需1字節(jié)記錄長(zhǎng)度 varchar(255)
- 不要對(duì)過長(zhǎng)的 varchar 建立索引
- **重要: varchar 字段為 null 時(shí)無法使用索引,應(yīng)使用 not null 定義并將默認(rèn)值設(shè)為空串 **
create table `example6` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
`remark` varchar(255) NOT NULL DEFAULT '' COMMENT '備注',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='示例6';
4.日期時(shí)間
| 類型 |
范圍 |
存儲(chǔ)大小 |
說明 |
| year |
1901 ~ 2155 |
1字節(jié) |
年份 |
| date |
1000-01-01 ~ 9999-12-31 |
3字節(jié) |
日期 |
| time |
-838:59:59 ~ 838:59:59 |
3字節(jié) |
時(shí)間 |
| timestamp |
1970-01-01 00:00:00~ 2037年 |
4字節(jié) |
日期時(shí)間 |
| datetime |
1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 |
8字節(jié) |
日期時(shí)間 |
- 使用 timestamp 時(shí)可設(shè)置自動(dòng)初始化和自動(dòng)更新
create table `example7` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間'
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='示例7';
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。