索引
- 什么是索引?
索引用于快速找出在某個(gè)列中有一特定值的行,不使用索引,MySQL必須從第一條記錄開(kāi)始讀完整個(gè)表,直到找出相關(guān)的行,表越大,查詢數(shù)據(jù)所花費(fèi)的時(shí)間就越多,如果表中查詢的列有一個(gè)索引,MySQL能夠快速到達(dá)一個(gè)位置去搜索數(shù)據(jù)文件,而不必查看所有數(shù)據(jù),那么將會(huì)節(jié)省很大一部分時(shí)間。
- MySQL中索引的優(yōu)缺點(diǎn)和使用原則
- 優(yōu)點(diǎn)
- 所有的MySql字段都可以用作索引
- 大大加快數(shù)據(jù)的查詢速度
- 原則
- 對(duì)經(jīng)常用于查詢的字段應(yīng)該創(chuàng)建索引,
- 數(shù)據(jù)量小的表最好不要使用索引,索引就可能不會(huì)產(chǎn)生優(yōu)化效果。
- 索引分類
- 普通索引: MySQL中基本索引類型,沒(méi)有什么限制,允許在定義索引的列中插入重復(fù)值和空值,純粹為了查詢數(shù)據(jù)更快一點(diǎn)。
- 唯一索引(唯一鍵): 索引列中的值必須是唯一的,但是允許為空值,
- 主鍵索引:是一種特殊的唯一索引,不允許有空值。
- 具體可以參考文檔
示例一:
create table test1(
id int,
name varchar(20),
index idx_name(name) #創(chuàng)建索引
);
create table test2(
id int,
name varchar(20),
);
create index idx_name on test2(name); #創(chuàng)建索引
create table test3(
id int,
name varchar(20),
);
alter table test3 add index idx_name(name);
示例二:
create table test4(
id int,
name varchar(20),
unique index idx_name(name) #創(chuàng)建索引
);
create table test5(
id int,
name varchar(20),
);
create unique index idx_name on test5(name); #創(chuàng)建索引
create table test6(
id int,
name varchar(20),
);
alter table test6 add unique index idx_name(name);
4.刪除索引
drop index idx_name on test6
數(shù)據(jù)完整性
- 實(shí)體完整性(行的完整性)
一行數(shù)據(jù)是否完整, 如果多條數(shù)據(jù)相同, 無(wú)法區(qū)分, 我們稱之為實(shí)體不完整
name score
lnj 100
lnj 100
解決方案:
- 添加主鍵約束
id name score
1 lnj 100
2 lnj 100
- 添加唯一鍵約束
name score
lnj 100
zq 100
- 自動(dòng)增長(zhǎng)列
id name score
1 lnj 100
2 lnj 100
- 域完整性(列完整性)
某一列數(shù)據(jù)是否完整, 如果出現(xiàn)null, 不匹配的數(shù)據(jù) 都表示不完整
id name score
1 lnj 100
2 zq null
2 zq tyt
- 數(shù)據(jù)類型約束
id name score
1 lnj 100
2 zq null
2 zq 0
解決方案:
- 非空約束
id name score
1 lnj 100
2 zq 0
2 zq 0
- 默認(rèn)值約束
id name score
1 lnj 100
2 zq 59.5
2 zq 0
外鍵
- 什么是外鍵?
如果一張表中有一個(gè)字段引用了另一張表中的主鍵, 那么這個(gè)字段我們就稱之為外鍵
例如: 成績(jī)表中的stuid引用了學(xué)生表中的id, 那么stuid我們就稱之為外鍵
注意點(diǎn): 成績(jī)表中的stuid引用了學(xué)生表中的id, 那么成績(jī)表我們稱之為"從表", 學(xué)生表稱之為主表
示例二:
create table stugrade2(
id int auto_increment primary key,
stuid int,
score float,
#告訴MySQL將stuid作為外鍵, 值是引用stuinfo中的id
foreign key(stuid) references stuinfo(id)
);
insert into stugrade2 values(null, 3, 100); #報(bào)錯(cuò), 因?yàn)閟utinfo中沒(méi)有id為3的人
insert into stuinfo values(null, 'lnj');
insert into stugrade2 values(null, 3, 100); #報(bào)錯(cuò), 因?yàn)閟utinfo中沒(méi)有id為3的人
insert into stugrade2 values(null, 1, 100);
delete from stuinfo where id=1; #報(bào)錯(cuò), 因?yàn)橛衅渌硪眠@這條數(shù)據(jù)
- 外鍵的特點(diǎn)
- 主表中沒(méi)有對(duì)應(yīng)的數(shù)據(jù), 從表不能插入, 因?yàn)椴迦胫髷?shù)據(jù)也不完整
- 從表引用這主表的數(shù)據(jù), 主表不能刪除該數(shù)據(jù), 因?yàn)閯h除之后數(shù)據(jù)也不完整
- 查看外鍵
show create table stugrade3\G;
- 刪除外鍵
alter table stugrade3 drop foreign key stugrade3_ibfk_1;
- 外鍵相關(guān)的操作
- 嚴(yán)格模式(默認(rèn))
+ 主表不存在對(duì)應(yīng)的數(shù)據(jù), 從表不允許插入
+ 從表引用著主表的數(shù)據(jù), 主表不能刪除
+ 從表引用著主表的數(shù)據(jù), 主表不能修改
- 置空操作
+ 如果主表中的數(shù)據(jù)被刪除了, 從表中對(duì)應(yīng)的字段變?yōu)閚ull, 我們就稱之為置空操作
+ 流入: 主表中id為1的人被刪除了, 那么從表中的stuid變?yōu)閚ull
- 級(jí)聯(lián)操作
+ 如果主表發(fā)生了變化, 從表也跟著變化, 我們就稱之為級(jí)聯(lián)操作
+ 例如: 主表中'lnj'的id從1變?yōu)?, 那么從表中的stuid也從1變?yōu)?
- 格式:
foreign key(字段) references 主表名(主表主鍵)[主表刪除的動(dòng)作][主表更新的動(dòng)作]
示例一:
create table stugrade(
id int auto_increment primary key,
stuid int,
score float,
#注意點(diǎn): 一般在企業(yè)開(kāi)發(fā)中都是刪除就清空, 更新就隨著更新
foreign key(stuid) references stuinfo(id) on delete set null on update cascade
);
insert into stugrade values(null, 1, 100);
update stuinfo set id=666 where id=1;
delete from stuinfo where id=666;