范式:Normal Format嵌削,是一種離散數(shù)學中的知識,是為了解決一種數(shù)據(jù)的存儲與優(yōu)化的問題,它的終極目標是為了減少數(shù)據(jù)的冗余
范式是一種分層結(jié)構(gòu)的規(guī)范,分為六層:
1NF本今、2NF、3NF主巍、……6NF
1NF是最底層冠息,要求最低
6NF是最高層,要求最嚴格
第一范式:要求字段的數(shù)據(jù)具有原子性孕索,就是需要的數(shù)據(jù)是否需要拆分才能得到
第二范式:(2NF):要解決表的設計不允許出現(xiàn)部分依賴铐达,給表添加一個id唯一
第三范式(3NF):解決傳遞依賴的問題
逆規(guī)范化:磁盤利用率與效率的對抗,空間換時間
高級數(shù)據(jù)操作:
新增數(shù)據(jù)
基本語法:insert into 表名 [(字段列表)] values(值列表);
主鍵沖突(Duplicate key)
當主鍵存在沖突的時候檬果,可以選擇性地進行處理,進行更新和替換
更新操作:insert into 表名 [(字段列表)] values(值列表) on duplicate key update 字段=新值;
替換:replace insert into 表名 [(字段列表)] values(值列表);
-- 給班級表增加主鍵
alter table my_classadd primary key (name);
-- 插入數(shù)據(jù)
insert into my_class
values ('python1910','b409');
insert into my_class
values ('python1910','b501');
-- 主鍵沖突:更新
insert into my_class
values ('python1910','b510')
-- 沖突處理
on duplicate key update
-- 更新教室
room ='b510';
insert into my_class
values ('python1907','b406');
-- 主鍵沖突:替換
replace into my_class
values ('python1907','a203');
replace into my_class
values ('python1903','b408');
replace into my_class
values ('python1811','b407');
where子句:返回結(jié)果0或1唐断,0代表false选脊,1代表true
判斷條件
比較運算符:>、<脸甘、>=恳啥、<=、!=丹诀、<>钝的、=、like铆遭、between硝桩、and、in/not in
邏輯運算符:&&(and)枚荣、||(or)碗脊、!(not)
-- 復制創(chuàng)建表
create table my_copylike my_class;
-- 蠕蟲復制
insert into my_copyselect * from my_class;
-- 刪除主鍵
alter table my_copydrop primary key ;
insert into my_copyselect * from my_copy;
-- 更新部分1811變成1911
update my_copyset name='python1911' where name='python1811' limit 3;
-- 刪除數(shù)據(jù):限制記錄數(shù)為5
delete from my_copywhere name='python1903' limit 5;
-- 給my_student表增加主鍵
alter table my_studentmodify idint primary key
auto_increment;
-- 清空表:重置自增長
truncate my_student;
-- select 選項
select * from my_copy;
select all * from my_copy;
-- 去重
select distinct * from my_copy;
-- 向?qū)W生表插入數(shù)據(jù)
insert into my_student
values (null,'bc20200001','張三','男'),
(null,'bc20200002','李四','男'),
(null,'bc20200003','王五','男'),
(null,'bc20200004','小明','男');
-- 字段別名
select id, number as 學號, name as 姓名, sexas 性別from my_student;
-- 多表數(shù)據(jù)源
select * from my_student,my_class;
-- 子查詢
select * from (select * from my_student)as s;
-- 增加age年齡和height身高字段
alter table my_student add age tinyint unsigned;
alter table my_student add height tinyint unsigned;
-- 增加字段值:rand取得一個0到1之間的隨機數(shù),floor向下取整
update my_student set age=floor(rand()*20+20),height=floor(rand()*20+170);
-- 找學生id為1橄妆、3衙伶、5的學生
select * from my_student where id=1 || id=5 || id=3;-- 邏輯判斷
select * from my_student where id in (1,3,5); -- 落在集合中
-- 找身高在185到190之間的學生
select * from my_student where height>=185 and height<=190;
select * from my_student where height between 185 and 190;
select * from my_student where height>=185 and height<=190;
select * from my_student where 1; -- 所有條件都滿足
-- 根據(jù)性別分組
select * from my_student group by sex;
-- 分組統(tǒng)計:身高高矮、平均年齡害碾、總年齡
select sex,count(*),max(height),min(height),avg(age),sum(age)from my_student group by sex;
-- 修改id為1的記錄矢劲,把年齡設置為null
update my_student set age=null where id=4;
select sex,count(*),count(age),max(height),min(height),avg(age),sum(age)from my_student group by sex;
-- 修改id為1的記錄,把性別置位女
update my_student set sex='女' where id=1;
-- nan nv
-- 倒序
select sex,count(*),count(age),max(height),min(height),avg(age),sum(age)from my_studentgroup by sexdesc;
-- 刪除班級表主鍵
alter table my_class drop primary key ;
-- 給班級表增加主鍵id
alter table my_class add id int primary key auto_increment;
-- 給學生表增加一個班級表的id
alter table my_student add c_id int;
update my_student set c_id=ceil(rand()*4);
insert into my_student values (6,'bc20200006','小芳','女',18,160,2);
-- 多字段分組:先班級慌随,后男女
select c_id,sex,count(*)from my_student group by c_id,sex;-- 多字段排序
select c_id,sex,count(*),group_concat(name)from my_student group by c_id,sex;