表關(guān)系
一對一:一張表的一條記錄一定只能與另外一張表的一條記錄進行對應(yīng),反之亦然
一對多:一張表(1)中有一條記錄可以對應(yīng)另外一張表(2)的多條記錄板祝,但是反過來另外一張表(2)的一條記錄只能對應(yīng)第一張表(1)的一條記錄
多對多:一張A表中的一條記錄宫静,能夠?qū)?yīng)B表中的多條記錄,同時B表中的一條記錄也能對應(yīng)A表中的多條記錄
范式
范式是一種分層結(jié)構(gòu)的規(guī)范,分為六層:
? ? ? ? 1)1NF孤里、2NF伏伯、3NF、……6NF
? ? ? ? 2)1NF是最底層捌袜,要求最低
? ? ? ? 3)6NF是最高層说搅,要求最嚴格
第一范式(1NF)?
要求字段的數(shù)據(jù)具有原子性(也就是不可以再分,直接拿來用虏等,而不是切割后用)
第二范式(2NF)
?要解決表的設(shè)計不允許出現(xiàn)部分依賴
第三范式(3NF)
?解決傳遞依賴的問題
逆規(guī)范化? 磁盤利用率與效率的對抗
高級數(shù)據(jù)操作
新增數(shù)據(jù)
基本語法 insert into 表名【(字段列表)】 values(值列表)
主鍵沖突(Duplicate key)
當主鍵存在沖突的時候弄唧,可以選擇性的進行處理,鏡像更改和替換
更新操作 insert into 表名【(字段列表)】values(值列表)on duplicate key update 字段 = 新值
替換 replace insert into 表名【(字段列表)】 values(值列表)
表創(chuàng)建的高級操作
從已有表創(chuàng)建新表(復制表結(jié)構(gòu))? create table 表名 like 數(shù)據(jù)庫霍衫。表名
蠕蟲復制??
:先查出數(shù)據(jù)候引,讓后將查詢出的數(shù)據(jù)增一遍
insert into 表名【(字段列表)】 selert 字段列表/*from 數(shù)據(jù)表名
蠕蟲復制的意義
從已有表拷貝數(shù)據(jù)到新表中
可以迅速地讓表中的數(shù)據(jù)澎湃到一定的數(shù)據(jù)量,用來測試表的壓力以及效率
更新數(shù)據(jù)
基本語法 update 表名set 字段 = 值【where條件】
高級語句 update 表名set 字段 = 值【where條件】【limit更新數(shù)量】
刪除數(shù)據(jù)
deletefrom 表名 【where條件】【limit數(shù)量】
truncate 表名? 先刪除該表慕淡,后新增該表
查詢數(shù)據(jù)
基本語句 select 字段列表/*from 表名 【where條件】
完整語法 select【選擇】 字段列表【字段別名】/*from 數(shù)據(jù)源【where條件子句】【group by子句】【having子句】【order by 子句】【limit子句】
select 選項 :select 對查出來的結(jié)果的處理方式
all:默認值背伴,保留所有結(jié)果
distinct : 去重,查出來的結(jié)果峰髓,將重復給去除
字段別名
字段名【as】別名
數(shù)據(jù)源:單表數(shù)據(jù)源? 多表數(shù)據(jù)源? 查詢語句
單表數(shù)據(jù)源? select*from 表名
多表數(shù)據(jù)源? select*from 表名1 表名2
子查詢 select*from(select語句)as別名
where子句? :? 返回結(jié)果0 或1 傻寂,0 代表false1代表true
判斷條件
比較運算符 >,<, >=, <=, !=, <>, =,like, between, and , in/not in
? ? ? ? ? ? 邏輯運算符 &&(and) , ll(or) 携兵, <碴(not)
group by 子句
基本語法 group by 字段名 【asc|desc】
統(tǒng)計函數(shù)
count(): 統(tǒng)計分組后的記錄數(shù),每一組有多少記錄
max(): 統(tǒng)計每組中最大值
min(): 統(tǒng)計每組中最小值
avg(): 統(tǒng)計平均數(shù)
sum(): 統(tǒng)計和
多字段排序
group_concat(字段)
回溯統(tǒng)計
with rollup
having子句
where和having的區(qū)別 磁盤where 內(nèi)存having
having子句與where子句相同點:都是進行條件判斷的
having能夠使用字段別名
排序order by子句
基本語法 order by 字段名 【asc|desc】
排序不會出現(xiàn)交叉徐紧,內(nèi)部還會重新排序
多字段排序静檬,先排班級后排性別select*from my_student order bhy c_id ,sex desc;
desc代表倒敘,無desc代表正序
limit子句(限制結(jié)果語句)
方案一 只用來限制長度并级,即數(shù)據(jù)量:limit 數(shù)據(jù)量
查詢前兩個學生selset * from my_student limit 2
方案二 限制起始位置拂檩,限制數(shù)量: limit 起始位置,長度(從指定位置出發(fā)查詢包含的長度)
從開始查詢學生到被查詢學生后面兩位select * from my_student limit 0,2? 可以用來頁面分頁
更改ID為班級的第一列 alter table my_id id int frist
limit offset length
length:每頁顯示數(shù)據(jù)量嘲碧,基本不變
offset = (頁碼-1)*每頁顯示量
連接查詢(join)
內(nèi)鏈接稻励,外鏈接,自然鏈接愈涩,交叉連接? ??
使用方式? 左表join右表
交叉連接(cross join)
select * from my_student cross join my_class? ? ? ? ? ? ? --my_student cross join my_class? 是數(shù)據(jù)源
基本語法:左表cross join 右表? ? ?等價于from 左表望抽,右表
內(nèi)連接([inner]join)
基本語法 左表【inner】 join 右表on左表.字段= 右表.字段
on表示連接條件
select * from my_student inner join my_class on my_student.c_id = my_class.id? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 等價于? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? select * from my_student inner join my_class on c_id = my_class.id(只有學生表才有c_id 所以可以省略my_student但是id不行,【建議不省略】)
字段和表 別名1.字段別名select s.*,c.name as c_name,c.room2.表別名from my_student as s inner join my_class as c on s.c_id = c.id
把學生表id 為5的記錄的c_id 設(shè)置為NULLupdate my_student set c_id = null where id=5
不寫on條件履婉,就是笛卡爾積select s * ,c.name as c_name,c.room
from my_student as s inner join my_class as c?
where代替on? ?select s.*,c,name as c_name ,c.room (字段別名)? ??
from my_student as s inner join my_class as c where s.c_id = c.id(表別名)
外連接(outer join)
left join:左外連接(左連接)煤篙,以左表為主表
right join:右外連接(右連接),以右表為主表
基本語法:左表 left/right join 右表 on 左表.字段=右表.字段;
自然連接(natural join)
自然內(nèi)連接:左表 natural join 右表;select * from my_student natural join my_calss? 自然內(nèi)鏈接? ? alter table my_class change name c_name varchar(20) not null修改班級表的name字段名為c_name? ??
自然外連接:左表 natural left/right join 右表;? ? ?自然左外連接select *from my_student? natural left join my_class? ?
模擬自然連接:左表 left/right/inner join 右表 using(字段名);? ? ? ??外連接模擬自然外鏈接(using)select * from my_student left join my_class using(id)
外鍵
增加外鍵
創(chuàng)建表的時候增加外鍵:在所有的表字段之后毁腿,使用foreign key(外鍵字段) references 外部表(主鍵字段)
創(chuàng)建外鍵? ?create table my_foreignl(id int primary key auto_increment,name varchar(20) not null comment'學生姓名'辑奈,--增加字段? ? ? foreign key(c_idrefernces my_class (id)?)charset utf8
在新增表之后增加外鍵:修改表結(jié)構(gòu)苛茂,使用alter table 表名 add [constraint 外鍵名字] foreign key(外鍵字段) references 父表(主鍵字段);
--增加外鍵alter table my_foreign2 add? ? ?
--指定外鍵名constraint student_class_1? ??
--指定外鍵字段foreign key(c_id)? ? -
--引用父表主鍵references my_class(id)
修改外鍵&刪除外鍵
alter table 表名 drop foreign key 外鍵名;
刪除外鍵alter table my_foreignl drop foreign key my_foreignl_ibfk_1;
插入數(shù)據(jù),外鍵字段在父表中不存在insert into my_foreign2 values(null,'郭富城'身害,5)? ?--沒有5班級
? ?insert into my_foreign2 values(null,'項羽'味悄,1)
insert into my_foreign2 values(null,'項羽'草戈,2)
insert into my_foreign2 values(null,'項羽'塌鸯,2)
更新父表元素update my_class set id = 5 where id=1;? ---失敗
原因:因為id=1 的記錄是班級的已經(jīng)被學生引用了
update my_class set in=5 where id =3? ?可以使用? 沒有引用
插入數(shù)據(jù)insert into my_foreignl values(null,'馬超',3)
增加外鍵 alter table my_foreignl key(c_id) references my_class(id)
外鍵條件
外鍵要存在唐片,首先必須保證表的存儲引擎是innodb
列類型必須與父表的主鍵類型一致
一張表中的外鍵名字不能重復
增加外鍵的字段數(shù)據(jù)已經(jīng)存在丙猬,必須保證數(shù)據(jù)與父表主鍵要求對應(yīng)
外鍵約束
有三種約束模式
district:嚴格模式(默認的)
cascade:級聯(lián)模式
set null:置空模式
語法:foreign key(外鍵字段) references 父表(主鍵字段) on delete 模式 on update 模式;