mysql設計技巧 MySQL庫表設計小技巧

2021-10-18 10:14:28 字數 3713 閱讀 1073

前言:

在我們專案開發中,資料庫及表的設計可以說是非常重要,我遇到過很多庫表設計比較雜亂的專案,像表名、欄位名命名混亂、字段型別設計混亂等等,此類資料庫後續極難維護與拓展。我一直相信只有優秀的庫表設計才能發揮出mysql最大的效能,前面有篇文章也分享了資料庫的使用規範,本篇文章主要講幾個庫表設計的小技巧,希望對大家有所啟發。

1.int型別的選用

整型字段型別包含 tinyint、smallint、mediumint、int、bigint 五種,占用空間大小及儲存範圍如下圖所示:

儲存位元組越小,占用空間越小。所以本著最小化儲存的原則,我們要盡量選擇合適的整型,下面給出幾個常見案例及選擇建議。

根據儲存範圍選擇合適的型別,比如人的年齡用 unsigned tinyint(範圍 0~255,人的壽命不會超過 255 歲);海龜就必須是smallint,但如果是太陽的年齡,就必須是int。

若儲存的資料為非負數值,建議使用 unsigned 標識,可以擴大正數的儲存範圍。

短資料使用 tinyint 或 smallint,比如:人類年齡,城市**。

儲存狀態變數的字段用 tinyint ,比如:是否刪除,0代表未刪除 1代表已刪除。

主鍵列,無負數,建議使用 int unsigned 或者 bigint unsigned;預估字段數字取值會超過 42 億,使用 bigint 型別。

下面給出建表語句示範:

create table `tb_int` (

`increment_id` int unsigned not null auto_increment comment '自增主鍵',

`stu_age` tinyint unsigned not null comment '學生年齡',

`is_deleted` tinyint unsigned default '0' comment '0:未刪除 1:刪除',

`col1` bigint not null comment 'bigint欄位',

primary key (`increment_id`)

) engine=innodb default charset=utf8 comment='int測試表';

2.時間型別的選用

時間字段型別可以選用datetime和timestamp,下面用一張表展示下二者的區別:

timestamp翻譯為漢語即"時間戳",它是當前時間到 unix元年(1970 年 1 月 1 日 0 時 0 分 0 秒)的秒數,占用4個位元組,而且是以utc的格式儲存,它會自動檢索當前時區並進行轉換。datetime以8個位元組儲存,不會進行時區的檢索。也就是說,對於timestamp來說,如果儲存時的時區和檢索時的時區不一樣,那麼拿出來的資料也不一樣。對於datetime來說,存什麼拿到的就是什麼。下面給出幾個常見案例及選擇建議。

根據儲存範圍來選取,比如生產時間,保質期等時間建議選取datetime,因為datetime能儲存的範圍更廣。

記錄本行資料的插入時間和修改時間建議使用timestamp。

和時區相關的時間字段選用timestamp。

如果只是想表示年、日期、時間的還可以使用 year、 date、 time,它們分別佔據 1、3、3 位元組,而datetime就是它們的集合。

如果timestamp欄位經常用於查詢,我們還可以使用mysql內建的函式from_unixtime()、unix_timestamp(),將日期和時間戳數字來回轉換,轉換後可以用 int unsigned 儲存時間,數字是連續的,占用空間更小,並且可以使用索引提公升查詢效能。下面給出示範建表語句及時間戳相關轉換sql:

create table `tb_time` (

`increment_id` int unsigned not null auto_increment comment '自增主鍵',

`col1` datetime not null default '2020-10-01 00:00:00' comment '到期時間',

`unix_createtime` int unsigned not null comment '建立時間戳',

`create_time` timestamp not null default current_timestamp comment '建立時間',

`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改時間',

primary key (`increment_id`),

key `idx_unix_createtime` (`unix_createtime`)

) engine=innodb default charset=utf8 comment='time測試表';

# 插入資料

insert into tb_time (unix_createtime,create_time) values

(unix_timestamp(now()),now());

# 時間戳數字與時間相互轉換

select unix_timestamp('2020-05-06 00:00:00')

select from_unixtime(1588694400)

3.儲存ip值

ip值一般使用char或varchar進行儲存,但是當進行查詢和統計時,字元型別不是很高效。mysql資料庫內建了兩個ip相關的函式inet_aton()、inet_ntoa(),可以實現 ip 位址和整數型別的轉換。轉換後使用可以int unsigned 來儲存ip,轉換後的數字是連續的,提高了查詢效能,占用空間更小。

create table `tb_ip` (

`increment_id` int(10) unsigned not null auto_increment comment '自增主鍵',

`name` varchar(100) not null comment '姓名',

`inet_ip` int(10) unsigned not null comment 'ip',

primary key (`increment_id`),

key `idx_inet_ip` (`inet_ip`)

) engine=innodb default charset=utf8 comment='ip測試表';

# 插入資料

insert into `tb_ip` (`name`,`inet_ip`) values

('wang',inet_aton('192.168.0.1')),('lisi',inet_aton('192.168.0.2'));

# 相互轉換

select inet_aton('192.168.0.1');

select inet_ntoa(3232235521);

總結:本篇文章分享了幾個庫表設計及欄位型別選取的建議。這些案例都是常常見到的場景,對於int型別及時間型別的選取,本文也根據常見場景給出相關建議,希望大家讀完這篇文章有所收穫。其實庫表設計是件複雜的事情,需要在專案前期多方人員共同規劃討論。還是那句話,只有優秀的庫表設計才能發揮出mysql最大的效能。

mysql資料庫設計小技巧

1 字段資料型別選擇 1.1 各資料型別占用的位元組數 tinyint 1 位元組 smallint 2 個位元組 mediumint 3 個位元組 int 4 個位元組 integer 4 個位元組 bigint 8 個位元組 float x 4 如果 x 24 或 8 如果 25 x 53 fl...

mysql技巧 MySQL技巧

一 儲存引擎 儲存引擎是mysql資料庫的核心 心臟 發動機,它決定了資料如何儲存,查詢的時候如何搜尋資料,索引如何建立等等 是對於資料庫檔案的一種訪問機制,如何實現儲存資料,如何為儲存的資料建立索引以及如何更新,查詢資料等技術實現的方法。常用儲存引擎 innodb 1.事務處理 回滾 崩潰修復能力...

mysql 資料庫設計 MySQL 資料庫設計總結

本文由雲 社群發表 規則 1 一般情況可以選擇 myisam 儲存引擎,如果需要事務支援必須使用 innodb 儲存引擎。注意 myisam 儲存引擎 b tree 索引有乙個很大的限制 參與乙個索引的所有欄位的長度之和不能超過 1000 位元組。另外 myisam 資料和索引是分開,而 innod...