HTML基礎知識(29)主鍵沖突、蠕蟲復制、數據應用、select、字段別名、數據源、五大子句、回溯統計、連接

一、新增數據

基本語法:insert into 表名 [(字段列表)] values(值列表);

二、主鍵沖突(Duplicate key)

當主鍵存在沖突的時候,可以選擇性地進行處理,進行更新和替換

更新操作:insert into 表名 [(字段列表)] values(值列表) on duplicate key update 字段=新值;

替換:replace insert into 表名 [(字段列表)] values(值列表);

三、表創建的高級操作

從已有表創建新表(復制表結構):create table 表名 like 數據庫.表名;

四、蠕蟲復制:

1、蠕蟲復制:先查出數據,然后將查出的數據新增一遍

insert into 表名[(字段列表)] select 字段列表/* from 數據表名;

2、蠕蟲復制的意義

從已有表拷貝數據到新表中

可以迅速地讓表中的數據膨脹到一定的數量級,用來測試表的壓力以及效率

五、數據的應用

1、更新數據

基本語法:update 表名 set 字段=值 [where條件];

高級語法:update 表名 set 字段=值 [where條件] [limit 更新數量];

2、刪除數據

delete from 表名 [where條件] [limit 數量];

truncate 表名; -- 先刪除該表,后新增該表

3、查詢數據

基本語法:select 字段列表/* from 表名 [where條件];

完整語法:select [select 選項] 字段列表[字段別名]/* from 數據源 [where條件子句] [group by子句] [having 子句] [order by子句] [limit 子句];

六、select

select 選項:select對查出來的結果的處理方式

all:默認的,保留所有的結果

distinct:去重,查出來的結果,將重復給去除

七、字段別名

字段名 [as] 別名;

八、數據源

數據源:單表數據源、多表數據源、查詢語句

單表數據源:select * from 表名;

多表數據源:select * from 表名1,表名2, ...;

子查詢:select * from (select 語句) as 別名;

九、五大子句

1、where子句

where子句:返回結果0或1,0代表false,1代表true

判斷條件

比較運算符:>、<、>=、<=、!=、<>、=、like、between、and、in/not in

邏輯運算符:&&(and)、||(or)、!(not)

2、group by子句

group by子句

基本語法:group by 字段名 [asc|desc];

統計函數:

count():統計分組后的記錄數,每一組有多少記錄

max():統計每組中最大的值

min():統計最小值

avg():統計平均值

sum():統計和

多字段排序

group_concat(字段);

回溯統計

with rollup;

3、having子句

having子句

與where子句一樣,是進行條件判斷的

having能夠使用字段別名

4、order by子句

order by子句

基本語法:order by 字段名 [asc|desc]

5、limit子句

limit子句

方案一:只用來限制長度,即數據量:limit 數據量;

方案二:限制起始位置,限制數量:limit 起始位置,長度;

limit offset,length;

length:每頁顯示的數據量,基本不變

offset = (頁碼-1)*每頁顯示量

十、連接

1、連接查詢

連接查詢(join)分類:內連接、外連接、自然連接、交叉連接

使用方式:左表 join 右表

2、交叉連接

交叉連接(cross join)

基本語法:左表 cross join 右表; -- 等價于:from 左表,右表;

3、內連接

內連接([inner] join)

基本語法:左表 [inner] join 右表 on 左表.字段=右表.字段;

on表示連接條件

4、外連接

外連接(outer join)

left join:左外連接(左連接),以左表為主表

right join:右外連接(右連接),以右表為主表

基本語法:左表 left/right join 右表 on 左表.字段=右表.字段;

5、自然連接

自然連接(natural join)

自然內連接:左表 natural join 右表;

自然外連接:左表 natural left/right join 右表;

模擬自然連接:左表 left/right/inner join 右表 using(字段名);

————————————————————————————

-- 給班級表增加主鍵

alter table my_classadd primary key(name);

-- 插入數據

insert into my_classvalues(

'python1907','B408');

insert into my_classvalues(

'python1907','B407');-- 錯誤:主鍵沖突

insert into my_classvalues(

'python1907','B407')

-- 沖突處理

on duplicatekey update

-- 更新教室

room='B407';

insert into my_classvalues(

'python1903','B408');

-- 主鍵沖突:替換

replaceinto my_classvalues(

'python1903','B406');

replaceinto my_classvalues(

'python1910','B409');

-- 復制創建表

create table my_copylike my_class;

-- 蠕蟲復制

insert into my_copyselect *from my_class;

insert into my_copyselect *from my_copy;

-- 刪除主鍵

alter table my_copydrop primary key;

-- 更新部分B406變成A406

update my_copyset room='A406'

where room='B406' limit3;

-- 刪除數據:限制記錄數為5

delete? from my_copywhere room='B409' limit5;

select *from my_copywhere room='B409';

-- 給學生表增加主鍵

alter table my_student modify idint primary key auto_increment;

-- 清空表,重置自增長

truncate my_student;

-- 一下幾項的區別?

-- delete from 表名 -- 刪除數據,不重置自增長

-- truncate 表名 -- 刪除數據,重置自增長

-- drop table 表名? -- 刪除表(全沒)

-- select 選項

select *from my_copy;

select all *from my_copy;

-- 去重

select distinct *from my_copy;

-- 向學生表插入數據

insert into my_student

values(null,'bc20190001','張三','男'),

values(null,'bc20190002','李四','男'),

values(null,'bc20190003','王五','男'),

insert into my_student

values(null,'bc20190004','趙六','男')

;

-- 字段別名

select id,

numberas 學號,

nameas 姓名,

sex 性別from my_student;

-- 多表數據源

select *from my_student,my_class;

-- 子查詢

select *from (select *from my_student)as s;

-- 增加age年齡和height身高字段

alter table my_studentadd age

tinyint unsigned;

alter table my_studentadd height

tinyint unsigned;

-- 增加字段值:rand取得一個0-1之間的隨機數,floor向下取整

update my_studentset age=floor(rand()*20+20), height=floor(rand()*20+170);

-- where子句--------------------------------------------------

-- 找學生ID為1、3、5的學生

select *from my_studentwhere id=1 || id=3 || id=5;-- 邏輯判斷

select *from my_studentwhere idin(1,3,5);-- in表示在集合中

-- 找出身高在180-190之間的學生

select *from my_studentwhere

height>=180 and height<=190;

select *from my_studentwhere

heightbetween 180 and 190;

select *from my_studentwhere

heightbetween 190 and 180;-- 不成立的,相當于大于等于190小于等于180

select *from my_studentwhere 1;-- 所有條件都滿足

-- group by 子句—————————————————————

-- 根據性別分組

select *from my_studentgroup by sex;

-- 分組統計:身高高矮、平均年齡、總年齡

select sex,count(*),max(height),min(height),avg(age),sum(age)

from my_studentgroup by sex;

-- 修改id為4的記錄,把年齡置為NULL

update my_studentset age=null

where id=4;

select sex,count(*),count(age),max(height),min(height),avg(age),sum(age)

from my_studentgroup by sex;

-- 修改id為1的記錄,把性別置為女

update my_studentset 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_classdrop primary key;

-- 給班級表添加主鍵

alter table my_classadd idint primary key auto_increment;

-- 給學生表增加班級ID

alter table my_studentadd c_idint;

update my_studentset c_id=ceil(rand()*3);

-- 多字段分組:先班組、后男女

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;

-- 統計

select c_id,count(*)from my_studentgroup by c_idwith rollup;

-- 多字段分組回溯統計

select c_id,sex,count(*),group_concat(name)from my_studentgroup by c_id,sex;

-- 多字段排序

select c_id,sex,count(*),group_concat(name)

from my_studentgroup by c_id,sexwith rollup;

-- having子句——————————————————————

-- 求出所有班級人數大于等于2的學生人數

select c_id,count(*)from my_studentgroup by c_idhaving count(*)>=2;

select c_id,count(*)from my_studentwhere count(*)>=2 group by c_id;-- 錯誤

select c_id,count(*)as totalfrom my_studentgroup by c_idhaving total>=2;

select c_id,count(*)as totalfrom my_studentwhere total>=2 group by c_id;-- 錯誤

-- having子句進行條件查詢

select nameas 名字,numberas 學號from my_studenthaving 名字like '張%';

-- order by子句——————————————————————

-- 排序

select *from my_studentgroup by c_id;-- 分組,為了進行統計

select *from my_studentorder by c_id;-- 排序

-- 多字段排序:先班級排序,后性別排序

select *from my_studentorder by c_id,sexdesc;

--limit子句————————————————————————

-- 查詢學生:前兩個

select *from my_student limit2;

select *from my_student limit0,2;-- 記錄數從0開始編號

select *from my_student limit2,2;-- 記錄數從3開始編號

select *from my_student limit4,2;-- 記錄數從5開始編號

-- 更改id為班級表的第一列

alter table my_class change id idint first;

-- 交叉連接

select *from my_studentcross join my_class;-- my_student cross join my_class是數據源

-- 內連接

select *from my_studentinner join my_classon my_student.c_id=my_class.id;

select *from my_studentinner join my_classon c_id=my_class.id;

select *from my_studentinner join my_classon c_id=id;-- 錯誤寫法,兩張表都有id字段

-- 字段和表的別名

select s.*,c.nameas c_name,c.room-- 給字段起別名

from my_studentas s-- 給表起別名

inner join my_classas con s.c_id=c.id;

-- 把學生表的id為5的記錄的c_id設置為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 cwhere s.c_id=c.id;

-- 左連接

select s.*,c.nameas c_name,c.room-- 給字段起別名

from my_studentas s-- 給表起別名

left join my_classas c-- 左表為主表:最終記錄數至少不少于左表已有的記錄數

on s.c_id=c.id;

-- 右連接

select s.*,c.nameas c_name,c.room-- 給字段起別名

from my_studentas s-- 給表起別名

right join my_classas c-- 右表為主表:最終記錄數至少不少于y右表已有的記錄數

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;

-- 自然內連接

select *from my_studentnatural join my_class;

-- 修改班級表的name字段名為c_name

alter table my_class change name c_namevarchar(20)not null;

-- 自然左外連接

select *from my_studentnatural left join my_class;

-- 外連接模擬自然外連接

select *from my_studentleft join my_classusing(id);

-- 修改字段類型

alter table 表名 modifycolumn 字段名 屬性類型;

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