mysql二
一、mysql索引
二、MySQL存儲(chǔ)引擎
+++++++++++++++++++++++++++++++++++
一、mysql索引
1.1 索引介紹 : 相當(dāng)于 “書的目錄”
5000頁(yè)
1~200 目錄信息
拼音排序
部首排序
筆畫排序
201~5000 正文
1.2 索引的優(yōu)點(diǎn)與缺點(diǎn)?
優(yōu)點(diǎn) 加快查詢的速度
缺點(diǎn) 占用物理存儲(chǔ)空間,減慢寫的速度。
姓名 性別 班級(jí) 年齡
jim
jim
NULL
1.3 使用普通索引index:(在表中的字段上創(chuàng)建索引)
使用規(guī)則?
查看 desc 表名; key ----> MUL
show index from 表名;
Table: t23
Key_name: name
Column_name: name
Index_type: BTREE ( B+TREE Hash )
二叉樹
1-10
1-5 6-10
1-2.5 2.6-5
創(chuàng)建index 索引:
創(chuàng)建表時(shí) 指定索引字段
create table 表名(
字段名列表,
index(字段名1),
index(字段名2),
);
create table db1.t23(
name char(10) ,
sex enum("boy","girl") default "boy" ,
age tinyint(2) unsigned not null default 18,
index(name),
index(sex)
);
desc db1.t23;
show index from db1.t23\G;
在已有表里創(chuàng)建index索引字段
create index 索引名 on 表名(字段);
刪除index索引 drop index 索引名 on 表名;
二、主鍵primary key 的使用
2.1 使用規(guī)則?
2.2 查看
desc 表名; key ----> MUL
show index from 表名;
2.3 創(chuàng)建
2.3.1 建表時(shí)創(chuàng)建
create table t25(
stu_id char(9) primary key ,
name char(10),
sex enum("boy","girl"),
age tinyint(2) unsigned
);
create table t24(
stu_id char(9),
name char(10),
sex enum("boy","girl"),
age tinyint(2) unsigned,
primary key(stu_id)
);
2.3.2 在已有表里創(chuàng)建
alter table 表名 add primary key(字段名);
2.4 刪除主鍵 alter table 表名 drop primary key;
2.5復(fù)合主鍵的使用(表中多個(gè)字段一起做主鍵 ,復(fù)合主鍵字段的值不允許同時(shí)重復(fù),要一起創(chuàng)建)
PRI PRI
name class pay
jim nsd1710 yes
jim nsd1712 no
建表時(shí)創(chuàng)建
create table db1.xfb(
name char(10),
class char(7),
pay enum("yes","no") default "no",
primary key(name,class)
);
驗(yàn)證
insert into db1.xfb values("jim","nsd1710","yes");
insert into db1.xfb values("jim","nsd1710","yes");
insert into db1.xfb values("jim","nsd1710","no");
insert into db1.xfb values("bob","nsd1710","yes");
insert into db1.xfb values("bob","nsd1703","no");
刪除復(fù)合主鍵 alter table 表名 drop primary key;
在已有表里添加復(fù)合主鍵。
alter table 表名 add primary key(字段名列表);
++++++++++++++++++++++++++++++++++++++++++
2.6 primary key 與 auto_increment 連用
字段的值自動(dòng)增長(zhǎng)i++ i=$i+1
數(shù)值類型
primary key
id name age sex
1 jim 21 boy
2 tom 19 boy
create table db1.t26(
id int(2) zerofill primary key auto_increment,
name char(10),
age tinyint(2) unsigned,
sex enum("boy","girl","no")
);
insert into db1.t26(name,age,sex) values("bob",21,"boy");
insert into db1.t26(name,age,sex) values("bob",21,"boy");
insert into db1.t26(name,age,sex) values("bob",21,"boy");
select * from db1.t26;
唯一索引 unique
pri pri姓名 護(hù)照編號(hào) 駕駛證號(hào)
null null
使用規(guī)則?
查看 desc 表名; key ----> UNI
創(chuàng)建
建表時(shí)創(chuàng)建
create table db1.t27(
name char(10),
hz_id char(5),
jsz_id char(5),
unique(hz_id),
unique(jsz_id)
);
desc db1.t27;
insert into db1.t27 values("jim","aaa","bbb");
insert into db1.t27 values("jim","aaa","bbb");
insert into db1.t27 values("jim","aaab","bbbc");
insert into db1.t27 values("jim",null,null);
create table db1.t28(
name char(10),
hz_id char(5) not null,
jsz_id char(5),
unique(hz_id),
unique(jsz_id)
);
desc db1.t28;
在已有表里創(chuàng)建unique
create unique index 索引名 on 表名(字段名);
刪除 drop index 索引名 on 表名;
+++++++++++++++++++++++++++++++++++++++
三、外鍵的使用
外鍵作用?
外鍵的使用規(guī)則?
創(chuàng)建外鍵:
foreign key(字段名) references 表名(字段名)
on update cascade on delete cascade
jfb 繳費(fèi)表
學(xué)號(hào)
jfb_id name pay
create table db1.jfb(
jfb_id int(2) primary key auto_increment,
name char(10),
pay float(7,2) default 20000
)engine=innodb;
insert into db1.jfb(name)values("bob"),("tom");
bjb 班級(jí)表
外鍵
學(xué)號(hào)
bjb_id name pay
create table db1.bjb(
bjb_id int(2) ,
name char(10),
pay float(7,2) default 20000,
foreign key(bjb_id) references jfb(jfb_id)
on update cascade on delete cascade
)engine=innodb;
alter table bjb add primary key(bjb_id);
use db1;
show create table bjb;
驗(yàn)證外鍵?
insert into bjb values(1,"bob",20000);
insert into bjb values(3,"lucy",20000);
insert into jfb(name)values("lucy");
insert into bjb values(3,"lucy",20000);
update 表名 set 字段名=值 where 條件;
update jfb set jfb_id=8 where jfb_id=2;
select from jfb;
select from bjb;
delete from 表名 where條件;
delete from jfb where jfb_id=3;
select from jfb;
select from bjb;
使用要注意的事項(xiàng)?
刪除外鍵 alter table 表名 drop foreign key 外鍵;
show create table bjb;
alter table bjb drop foreign key bjb_ibfk_1;
在已有表里創(chuàng)建外鍵:
alter table bjb add foreign key(bjb_id) references jfb(jfb_id)
on update cascade on delete cascade;
+++++++++++++++++++++++++++++++++++
四、MySQL存儲(chǔ)引擎
4.1 MySQL存儲(chǔ)引擎介紹:是數(shù)據(jù)庫(kù)服務(wù)自帶的功能程序,
處理表的處理器
每種存儲(chǔ)引擎的功能和數(shù)據(jù)存儲(chǔ)方式都不同
4.2 查看
表使用的存儲(chǔ)引擎 show create table 表名;
數(shù)據(jù)服務(wù)使用的存儲(chǔ)引擎
show engines;
InnoDB | DEFAULT
4.3 修改
表使用的存儲(chǔ)引擎?
alter table 表名 engine=存儲(chǔ)引擎名;
建表時(shí)指定表使用的存儲(chǔ)引擎?
create table 表名(
字段名列表
.....
)engine=存儲(chǔ)引擎名;
數(shù)據(jù)服務(wù)使用的存儲(chǔ)引擎?
vim /etc/my.cnf
[mysqld]
default-storage-engine=myisam
.....
:wq
#systemctl restart mysqld
++++++++++++++++++++++++++++++++
4.4 生產(chǎn)環(huán)境中常用存儲(chǔ)引擎及特點(diǎn)
myisam特點(diǎn)
支持表級(jí)鎖
不支持外鍵 、事務(wù)、事務(wù)回滾
數(shù)據(jù)存儲(chǔ)方式 .frm .MYI .MYD
表結(jié)構(gòu) 索引 數(shù)據(jù)
innodb特點(diǎn)
支持行級(jí)鎖、 外鍵 、事務(wù)、事務(wù)回滾
數(shù)據(jù)存儲(chǔ)方式 .frm .ibd
表結(jié)構(gòu) 索引+數(shù)據(jù)
鎖的作用:解決并發(fā)訪問的沖突問題
鎖類型:讀鎖(共享鎖) 寫鎖(排它鎖)
鎖粒度:表鎖 行鎖 (頁(yè)鎖)
事務(wù):一次sql操作從連接到斷開連接的過程稱為事務(wù)。要么全部執(zhí)行成功,任意一步錯(cuò)誤,執(zhí)行都失敗。
ATM
插卡
轉(zhuǎn)賬: 對(duì)方卡號(hào) 11111
匯款金額 50000
轉(zhuǎn)賬中。。。。。
退卡
事務(wù)回滾:事務(wù)執(zhí)行過程,任意一步執(zhí)行不成功,會(huì)恢復(fù)所有的操作。
innodb存儲(chǔ)引擎的表使用事務(wù)文件記錄執(zhí)行過的sql操作。
cd /var/lib/mysql/db1/t1.*
ls
ib_logfile0
ib_logfile1
ibdata1
insert into t1 values(101),(102);
4.5 建表時(shí)如何決定表使用哪種存儲(chǔ)引擎
接收查訪問多的表,適合使用myisam存儲(chǔ)引擎,節(jié)省系統(tǒng)資源。
接收寫訪問多的表,適合使用innodb存儲(chǔ)引擎,并發(fā)訪問量大。
轉(zhuǎn)載于:https://blog.51cto.com/2168836/2102903
總結(jié)
- 上一篇: iOS多线程:『pthread、NSTh
- 下一篇: Sanic 连接postgresql数据