1. 程式人生 > >資料庫 引擎,資料型別,約束

資料庫 引擎,資料型別,約束

引擎(****)

前提: 引擎是建表是規定, 提供給表使用的, 不是資料庫

mysql> show engines; # 展示所有引擎
# 重點:
# innodb(預設): 支援事務, 行級鎖, 外來鍵
# myisam: 查詢效率要優於innodb, 當不需要支援事務, 行級鎖, 外來鍵, 可以通過設定myisam來優化資料庫

mysql> use db1;

mysql> create table t1(id int)engine=innodb;
mysql> create table t2(id int)engine=myisam;
mysql> create table t3(id int)engine=blackhole;
mysql> create table t4(id int)engine=memory;

insert into t1 values(1);
insert into t2 values(1);
insert into t3 values(1);
insert into t4 values(1);

select * from t1; ...

 

建立表完整語法

'''
create table 表名(
欄位名1 型別[(寬度) 約束條件],
欄位名2 型別[(寬度) 約束條件],
欄位名3 型別[(寬度) 約束條件]
)engine=innodb charset=utf8;
'''
# []可選引數
# create table db1.t1(name char(3) not null);
# 資料插入時,name不能為空(null), 且最長只能存放三個字元
# 總結: 寬度和約束條件為可選引數, 用來限制存放資料的規則

資料庫的模式(**)

# sql_mode: 反映資料庫的全域性變數
# 資料庫模式限制的是客戶端對伺服器操作資料的方式(是否嚴格)

# 兩種模式
no_engine_substitution:非安全性,預設
strict_trans_tables:安全模式

# 檢視當前資料庫模式:
show variables like "%sql_mode%"; # %匹配0~n個任意字元 => 模糊查詢

# 設定為安全模式
set global sql_mode="strict_trans_tables";

# 重啟連線(客戶端)
quit

# 應用
create table t1(name char(2));
insert into t1 values ("ab") # 正常
insert into t1 values ("zero") # 錯誤 Data too long for column 'name' at row 1

資料型別(*****)

# mysql資料庫支援存放哪些資料

# 整型* | 浮點型 | 字元型* | 時間型別 | 列舉型別 | 集合型別

 

整型

'''型別
tinyint:1位元組 -128~127 *
smallint:2位元組
mediumint:3位元組
int:4位元組 -2147483648~2147483647 *
bigint:8位元組
'''
'''約束 *
unsigned:無符號
zerofill:0填充
'''
# 不同型別所佔位元組數不一樣, 決定所佔空間及存放資料的大小限制
# eg:
create table t8(x tinyint);
insert into t8 values(200);  # 非安全模式存入,值只能到最大值127
select (x) from t8;


'''寬度
1.不能決定整型存放資料的寬度, 超過寬度可以存放, 最終由資料型別所佔位元組決定
2.如果沒有超過寬度,且有zerofill限制, 會用0填充前置位的不足位
3.沒有必要規定整型的寬度, 預設設定的寬度就為該整型能存放資料的最大寬度 *
'''
# eg:1
create table t9(x int(5));
insert into t9 values(123456);
select x from t9; # 結果: 123456
insert into t9 values(2147483648);
select x from t9; # 結果: 2147483647
insert into t9 values(10);
select x from t9; # 結果: 10
# eg:2
create table t10(x int(5) unsigned zerofill); # 區域0~4294967295
insert into t10 values(10);
select x from t10; # 結果: 00010
insert into t10 values(12345678900);
select x from t10; # 結果: 4294967295

 

浮點型

'''型別
float:4位元組,3.4E–38~3.4E+38 *
double:8位元組,1.7E–308~1.7E+308
decimal:M,D大值基礎上+2
'''
'''寬度:
限制儲存寬度
(M, D) => M為位數,D為小數位
float(255, 30):精度最低,最常用
double(255, 30):精度高,佔位多
decimal(65, 30):字串存,全精度
'''
# eg:1
create table t11 (age float(256, 30)); # Display width out of range for column 'age' (max = 255)
create table t11 (age float(255, 31)); # Too big scale 31 specified for column 'age'. Maximum is 30.
# eg:2
create table t12 (x float(255, 30));
create table t13 (x double(255, 30));
create table t14 (x decimal(65, 30));

insert into t12 values(1.11111111111111111111);
insert into t13 values(1.11111111111111111111);
insert into t14 values(1.11111111111111111111);

select * from t12; # 1.111111164093017600000000000000 => 小資料,精度要求不高, 均採用float來儲存 *
select * from t13; # 1.111111111111111200000000000000
select * from t14; # 1.111111111111111111110000000000

alter table t14 modify x decimal(10, 5); # 1.11111 => 限制了資料的儲存寬度

字元型

'''型別
char:定長
varchar:不定長
'''
'''寬度
限制儲存寬度
char(4):以4個字元儲存定長儲存資料
varchar(4):資料長度決定字元長度,為可變長度儲存資料
'''
# eg:
create table t15 (x char(4), y varchar(4));
insert into t15 values("zero", 'owen'); # '' | "" 均可以表示字元
select x,y from t15; # 正常
insert into t15 values("yanghuhu", 'lxxVSegon'); # 非安全模式資料丟失,可以存放, 安全模式報錯
select x,y from t15; # 可以正常顯示丟失後(不完整)的資料
insert into t15 values('a', 'b');

# 驗證資料所在字元長度
# 前提: 安全模式下以空白填充字元
set global sql_mode="strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH";
# 重啟連線
select char_length(x), char_length(y) from t15; # a佔4 b佔1

'''重點: 儲存資料的方式 ** => 資料庫優化
char: 一定按規定的寬度存放資料, 以規定寬度讀取資料, 通常更佔空間
varchar: 首先根據資料長度計算所需寬度, 並在資料開始以資料頭方式將寬度資訊儲存起來, 是一個計算耗時過程, 取先讀取寬度資訊,以寬度資訊為依準讀取資料, 通常節省空間
'''
8: zero    egon    lxx     yanghuhu
8: 4zero4egon3lxx8yanghuhu
注: varchar的資料頭佔1~2位元組
   規定char|varchar寬度均為4,用來存放4個字元的資料, char存取更高效,char佔4字元,varchar佔5字元,char更省空間

總結: 資料長度相近的資料提倡用char來存放資料, 資料需要高速存取,以空間換時間, 採用char

時間型別

'''型別
year:yyyy(1901/2155)
date:yyyy-MM-dd(1000-01-01/9999-12-31)
time:HH:mm:ss
datetime:yyyy-MM-dd HH:mm:ss(1000-01-01 00:00:00/9999-12-31 23:59:59)
timestamp:yyyy-MM-dd HH:mm:ss(1970-01-01 00:00:00/2038-01-19 ??)
'''
# eg: 1
create table t16(my_year year, my_date date, my_time time);
insert into t16 values(); # 三個時間型別的預設值均是null
insert into t16 values(2156, null, null); # 在時間範圍外,不允許插入該資料
insert into t16 values(1, '2000-01-01 12:00:00', null); # 2001 2000-01-01 null
insert into t16 values(2019, '2019-01-08', "15-19-30"); # time報格式錯誤 => 按照時間規定格式存放資料

alter table t16 change my_year myYear year(2); # 時間的寬度修改後還是採用預設寬度 => 不需要關係寬度


# eg:2
create table t17(my_datetime datetime, my_timestamp timestamp);
insert into t17 values(null, null); # 可以為空, 不能為null,賦值null採用預設值current_timestamp
insert into t17 values('4000-01-01 12:00:00', '2000-01-01 12:00:00'); # 在各自範圍內可以插入對應格式的時間資料

# datetime VS timestamp
datetime:時間範圍,不依賴當前時區,8位元組,可以為null
timestamp:時間範圍,依賴當前時區,4位元組,有預設值CURRENT_TIMESTAMP

列舉與集合

'''型別
enum:單選
set:多選
'''

create table t19(
   sex enum('male','female','wasai') not null default 'wasai', # 列舉
   hobbies set('play','read','music') # 集合
);

insert into t19 values (null, null); # sex不能設定null
insert into t19 values (); # wasai null
insert into t19 (hobbies) values ('play,read'), ('music,play'); # sex採用預設值, 對hobbies欄位新增兩條記錄
insert into t19 (sex,hobbies) values ('male,female', 'play'); # sex欄位只能單選,此處sex雙選沒有錄入

 

約束條件(*****)

"""
primary key:主鍵,唯一標識,表都會擁有,不設定為預設找第一個 不空,唯一 欄位,未標識則建立隱藏欄位
foreing key:外來鍵
unique key:唯一性資料, 該條欄位的值需要保證唯一,不能重複

auto_increment:自增,只能加給key欄位輔助修飾

not null:不為空
default:預設值

unsigned:無符號
zerofill:0填充
"""

注:
1.鍵是用來講的io提供存取效率
2.聯合唯一
create table web (
   ip char(16),
   port int,
   unique(ip,port)
);
3.聯合主鍵
create table web (
   ip char(16),
   port int,
   primary key(ip,port)
);

# eg:1
# 單列唯一
create table t20 (
id int unique
);
# 聯合唯一
create table web (
   ip char(16),
   port int,
   unique(ip,port)
);
# 如果聯合兩個欄位,兩個欄位全相同才相同,否則為不同
insert into web values ('10.10.10.10', 3306), ('10.10.10.10', 3306);

# 注:
# 1.表預設都有主鍵, 且只能擁有一個主鍵欄位(單列主鍵 | 聯合主鍵)
# 2.沒有設定主鍵的表, 資料庫系統會自上而下將第一個規定為unique not null欄位自動提升為primary key主鍵
# 3.如果整個表都沒有unique not null欄位且沒有primary key欄位, 系統會預設建立一個隱藏欄位作為主鍵
# 4.通常必須手動指定表的主鍵, 一般用id欄位, 且id欄位一般型別為int, 因為int型別可以auto_increment

# eg:2
create table t21(id int auto_increment); # 自增約束必須新增給key的欄位
# eg:3
create table t21(id int primary key auto_increment); # 自增要結合key,不賦值插入,資料會自動自增, 且自增的結果一直被記錄保留
# eg:4
# 聯合主鍵
create table t22(
ip char(16),
   port int,
   primary key(ip,port)
);
# 如果聯合兩個欄位,兩個欄位全相同才相同,否則為不同
insert into web values ('10.10.10.10', 3306), ('10.10.10.10', 3306);