-- 主鍵沖突,替換表創(chuàng)建的高級(jí)操作,蠕蟲復(fù)制,更新數(shù)據(jù),刪除數(shù)據(jù),以下三句的區(qū)別,select 選項(xiàng),distinct:去重,字段別名,數(shù)據(jù)源,where子句,group by子句,回溯統(tǒng)計(jì),...

高級(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);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,563評(píng)論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,694評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,672評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,965評(píng)論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,690評(píng)論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,019評(píng)論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,013評(píng)論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,188評(píng)論 0 290
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,718評(píng)論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,438評(píng)論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,667評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,149評(píng)論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,845評(píng)論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,252評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,590評(píng)論 1 295
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,384評(píng)論 3 400
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,635評(píng)論 2 380