一、新增數據
基本語法: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 字段名 屬性類型;