高級(jí)數(shù)據(jù)操作
-- 給班級(jí)表增加主鍵
alter table my_classadd primary
key(name);
新增數(shù)據(jù)
基本語(yǔ)法:insert into 表名 [(字段列表)] values(值列表);
-- 插入數(shù)據(jù)
insert into my_class
values('python1907','B408');
values('python1907','B407');-- 錯(cuò)誤:主鍵沖突
主鍵沖突(Duplicate key)
當(dāng)主鍵存在沖突的時(shí)候,可以選擇性地進(jìn)行處理,進(jìn)行更新和替換
更新操作:
insert into 表名 [(字段列表)] values(值列表) on duplicate key update 字段=新值;
例子:insert into my_class
values('python1907','B407')
-- 沖突處理
on duplicatekey update
-- 更新教室
room='B407';
替換:
replace insert into 表名 [(字段列表)] values(值列表);
-- 主鍵沖突:替換
例子:replaceinto my_classvalues(
'python1903','B406');
replaceinto my_classvalues(
'python1910','B409');-- 沒有沖突的情況下,它執(zhí)行的是插入
表創(chuàng)建的高級(jí)操作
從已有表創(chuàng)建新表(復(fù)制表結(jié)構(gòu)):
create table 表名 like 數(shù)據(jù)庫(kù).表名;
-- 復(fù)制創(chuàng)建表(把my_class里的數(shù)據(jù)復(fù)制到my_copy里)
create table my_copy like
my_class;
-- 刪除主鍵(需要先刪除主鍵才能蠕蟲復(fù)制,原文件里的主鍵不刪)
alter table my_copydrop primary key;
蠕蟲復(fù)制:
先查出數(shù)據(jù),然后將查出的數(shù)據(jù)新增一遍
insert into 表名[(字段列表)] select 字段列表/* from 數(shù)據(jù)表名;
例子:-- 蠕蟲復(fù)制
insert into my_copyselect *from my_class;
insert into my_copyselect *from my_copy;
蠕蟲復(fù)制的意義
從已有表拷貝數(shù)據(jù)到新表中
可以迅速地讓表中的數(shù)據(jù)膨脹到一定的數(shù)量級(jí),
用來測(cè)試表的壓力以及效率
更新數(shù)據(jù)
基本語(yǔ)法:update 表名 set 字段=值 [where條件];
高級(jí)語(yǔ)法:update 表名 set 字段=值 [where條件] [limit 更新數(shù)量];
例子:-- 更新部分B406變成A406
update my_copyset room='A406'
where room='B406' limit3;
刪除數(shù)據(jù)
delete from 表名 [where條件] [limit 數(shù)量];
truncate 表名; -- 先刪除該表,后新增該表
例子:-- 刪除數(shù)據(jù),限制刪除五條
delete from my_copy
where room='B409' limit5;
-- 給學(xué)生表增加主鍵
alter table my_student modify id
int primary key auto_increment;
-- 清空表,重置自增長(zhǎng)
truncate my_student;
-- 以下三句的區(qū)別
-- delete from 加表名;--刪除數(shù)據(jù),不重置自增長(zhǎng)
-- truncate 表名 ; --刪除數(shù)據(jù),重置自增長(zhǎng)
-- drop table 表名;直接刪除表
select 選項(xiàng):
select對(duì)查出來的結(jié)果的處理方式
all:默認(rèn)的,保留所有的結(jié)果
例子:select *from my_copy;
select all *from my_copy;
distinct:去重,查出來的結(jié)果,將重復(fù)給去除
例子:-- 去掉重復(fù)(在做統(tǒng)計(jì)的時(shí)候會(huì)用到)
select distinct *from my_copy;
-- 向?qū)W生表插入數(shù)據(jù)
insert into my_student
values(null,'bc20190001','張三','男'),
(null,'bc20190002','李四','男'),
(null,'bc20190003','王五','男'),
(null,'bc20190004','趙六','男'),
(null,'bc20190005','周七','男');
字段別名
字段名 [as] 別名;
例子:select id,
numberas 學(xué)號(hào),
nameas 姓名,
sex 性別from my_student;
數(shù)據(jù)源:?jiǎn)伪頂?shù)據(jù)源、多表數(shù)據(jù)源、查詢語(yǔ)句
單表數(shù)據(jù)源:select * from 表名;
多表數(shù)據(jù)源:select * from 表名1,表名2, ...;
例子:-- 多表數(shù)據(jù)源
select *from my_student , my_class;
子查詢:select * from (select 語(yǔ)句) as 別名;
例子:-- 子查詢
select *from (select *from
my_student)as s;
where子句:返回結(jié)果0或1,0代表false,1代表true
判斷條件
比較運(yùn)算符:>、<、>=、<=、!=、<>、=、like、between、and、in/not in
邏輯運(yùn)算符:&&(and)、||(or)、!(not)
例子:-- 增加agg年齡和height身高字段
alter table my_studentadd age
tinyint unsigned;
alter table my_studentadd height
tinyint unsigned;
-- 增加字段的值: rand取得一個(gè)0—1之間的隨機(jī)數(shù),floor向下取整
update my_studentset age=floor(
rand()*20+20), height=floor(
rand()*20+170);
-- 找學(xué)生id為1,3,5的學(xué)生
select *from my_student
where id=1 || id=3 || id=5;-- 采用邏輯判斷
select *from my_student
where idin(1,3,5);-- in表示在集合中
-- 找出身高180到190之間的學(xué)生
select *from my_student
where height>=180 and height<=190;
select *from my_student
where heightbetween 180 and 190;
select *from my_student
where heightbetween 190 and 180;-- 不成立的,相當(dāng)于height>=190 and height<=180
select *from my_studentwhere 1;-- 所有條件都滿足
-- 根據(jù)性別分組
select *from my_student
group by sex;
-- 分組統(tǒng)計(jì):身高高矮、平均年齡、總年齡
select sex ,count(*) ,max(height),
min(height),avg(age),sum(age)
from my_studentgroup by sex;
-- 修改id為4的記錄,把年齡設(shè)置為NULL
update my_studentset age=null
where id=1;
select sex ,count(*) ,max(height),
min(height),avg(age),sum(age)
from my_studentgroup by sex;
-- 修改id為1的記錄,把性別設(shè)置為女
update my_studentset sex='女'
where id=1;
select sex ,count(*) ,max(height),
min(height),avg(age),sum(age)
from my_studentgroup by sexdesc;
-- 刪除班級(jí)表原主鍵
alter table my_classdrop primary key;
-- 給班級(jí)表增加主鍵
alter table my_classadd idint primary key auto_increment;
-- 給學(xué)生表增加班級(jí)id
alter table my_studentadd c_idint;
update my_studentset c_id=ceil(
rand()*3);
group by子句
基本語(yǔ)法:group by 字段名 [asc|desc];
統(tǒng)計(jì)函數(shù):
count():統(tǒng)計(jì)分組后的記錄數(shù),每一組有多少記錄
max():統(tǒng)計(jì)每組中最大的值
min():統(tǒng)計(jì)最小值
avg():統(tǒng)計(jì)平均值
sum():統(tǒng)計(jì)和
統(tǒng)計(jì)函數(shù):
count():統(tǒng)計(jì)分組后的記錄數(shù),每一組有多少記錄
例子:-- 分組統(tǒng)計(jì):身高高矮、平均年齡、總年齡
select sex ,count(*) ,max(height),
min(height),avg(age),sum(age)
from my_studentgroup by sex;
max():統(tǒng)計(jì)每組中最大的值
例子:-- 修改id為4的記錄,把年齡設(shè)置為NULL
update my_studentset age=null
where id=1;
select sex ,count(*) ,max(height),
min(height),avg(age),sum(age)
from my_studentgroup by sex;
-- 多字段分組:先班組、后男女
select c_id,sex,count(*)from my_studentgroup by c_id,sex;--排多字段序
select c_id,sex,count(*),group_concat(name)from my_studentgroup by c_id,sex;--排多字段序
-- 統(tǒng)計(jì)
select c_id,count(*)from
my_studentgroup by c_id;
回溯統(tǒng)計(jì)
with rollup;
例子:-- 回溯統(tǒng)計(jì)
select c_id,count(*)from
my_studentgroup by c_idwith rollup;
多字段排序
group_concat(字段);
例子:-- 多字段排序
select c_id,sex,count(*),
group_concat(name)from
my_studentgroup by c_id,sex
with rollup;
-- 多字段分組回溯統(tǒng)計(jì)
select c_id,count(*),
group_concat(name)from
my_studentgroup by c_id,sex;
having子句
與where子句一樣,是進(jìn)行條件判斷的
having能夠使用字段別名
order by子句排序基本語(yǔ)法:order by 字段名 [asc|desc]
例子:-- 求出所有班級(jí)人數(shù)大于等于2的學(xué)生人數(shù)
select c_id,count(*)from
my_studentgroup by c_idhaving
count(*)>=2;
-- having子句進(jìn)行條件查詢
select nameas 名字,numberas
學(xué)號(hào)from my_studenthaving 名字
like '張%';
limit子句
方案一:只用來限制長(zhǎng)度,即數(shù)據(jù)量:limit 數(shù)據(jù)量;
方案二:限制起始位置,限制數(shù)量:limit 起始位置,長(zhǎng)度;
limit offset,length;
length:每頁(yè)顯示的數(shù)據(jù)量,基本不變
offset = (頁(yè)碼-1)*每頁(yè)顯示量
-- 排序
select *from my_studentgroup by
c_id;-- 分組,為了進(jìn)行統(tǒng)計(jì)
select *from my_studentorder by
c_id;-- 排序
-- 多字段排序:先班級(jí)排序,后性別排序
select *from my_studentorder by
c_id,sexdesc;
-- 查詢學(xué)生,前兩個(gè)
select *from my_student limit2;
select *from my_student limit0,
2;--記錄數(shù)是從01開始可以編號(hào)
select *from my_student limit2,
2;
select *from my_student limit4,
2;
-- 更改ID為班級(jí)表第一列
alter table my_class change id id
int first;
連接查詢(join)分類:內(nèi)連接、外連接、自然連接、交叉連接
使用方式:左表 join 右表
交叉連接(cross join)
基本語(yǔ)法:左表 cross join 右表; -- 等價(jià)于:from 左表,右表;
例子:-- 交叉連接
select *from my_studentcross
join my_class;--? my_student cross join my_class;是數(shù)據(jù)源
內(nèi)連接([inner] join)
基本語(yǔ)法:左表 [inner] join 右表 on 左表.字段=右表.字段;
on表示連接條件
例子:-- 內(nèi)連接
select *from my_studentinner
join my_classon my_student.c_id=
my_class.id;
(2)例子:select *from my_studentinner
join my_classon c_id= my_class.id;
-- 字段和表別名
select s.*,c.nameas c_name,c.
room-- 字段別名
from my_studentas s-- 表別名
inner join my_classas c
on s.c_id=c.id;
-- 把學(xué)生表id為5的記錄的c_id設(shè)置為NULL
update my_studentset c_id=null
where id=5;
-- where 代替 on
select s.*,c.nameas c_name,c.
room-- 字段別名
from my_studentas s-- 表別名
inner join my_classas c
where s.c_id=c.id;
外連接(outer join)
left join:左外連接(左連接),以左表為主表
right join:右外連接(右連接),以右表為主表
基本語(yǔ)法:左表 left/right join 右表 on 左表.字段=右表.字段;
例子:-- 左連接
select s.*,c.nameas c_name,c.
room-- 字段別名
from my_studentas s
left join my_classas c-- 左表為主表:最終記錄數(shù)至少不少于左表已有的記錄數(shù)
on s.c_id=c.id;
-- 右連接
select s.*,c.nameas c_name,c.
room-- 字段別名
from my_studentas s
right join my_classas c-- 右表為主表:最終記錄數(shù)至少不少于右表已有的記錄數(shù)
on s.c_id=c.id;
select s.*,c.nameas c_name,c.
room-- 字段別名
from my_classas s
right join my_studentas c
on s.c_id=c.id;
自然連接(natural join)
自然內(nèi)連接:左表 natural join 右表;
自然外連接:左表 natural left/right join 右表;
模擬自然連接:左表 left/right/inner join 右表 using(字段名);
例子:-- 自然內(nèi)連接
select *from my_studentnatural
join my_class;
-- 修改班級(jí)表的name字段名為c_name
alter table my_class change name
c_namevarchar(20)not null;
-- 自然左外連接
select *from my_studentnatural
left join my_class;
-- 外連接模擬自然外連接:using
select *from my_studentleft
join my_classusing(id);