mysql.exe -h localhost -P 3306 -u root -p
use mydb;?????——???? 進(jìn)入數(shù)據(jù)庫?
show tables;? ? ——? ? 查看所有表
查看:show index from 表名\G
desc:查看表結(jié)構(gòu)
select * from 表名:查詢所有數(shù)據(jù)
表創(chuàng)建的高級(jí)操作
從已有表創(chuàng)建新表(復(fù)制表結(jié)構(gòu)):create table 表名 like 數(shù)據(jù)庫.表名;
-- 復(fù)制創(chuàng)建表(只是復(fù)制了表結(jié)構(gòu))
create table my_copy like my_class;
蠕蟲復(fù)制的意義
? ??????從已有表拷貝數(shù)據(jù)到新表中
? ??????可以迅速地讓表中的數(shù)據(jù)膨脹到一定的數(shù)量級(jí),用來測(cè)試表的壓力以及效率
蠕蟲復(fù)制
先查出數(shù)據(jù)是整,然后將查出的數(shù)據(jù)新增一遍
????????insert into 表名[(字段列表)] select 字段列表/* from 數(shù)據(jù)表名;
-- 蠕蟲復(fù)制
insert into my_copy select * from my_class;
insert into my_copy select * from my_copy;?
-- 刪除主鍵
alter table my_copy drop primary key;
limit-truncate-select
更新數(shù)據(jù)
基本語法:update 表名 set 字段=值 [where條件];
高級(jí)語法:update 表名 set 字段=值 [where條件] [limit 更新數(shù)量];
-- 部分更新(前3個(gè)1811變成1911肖揣,其余不變)
update my_copy set name='Python1911' where name='Python1811' limit 3;
刪除數(shù)據(jù)
delete from 表名 [where條件] [limit 數(shù)量];
truncate 表名; -- 先刪除該表,后新增該表
-- 刪除數(shù)據(jù):限制記錄數(shù)為5(前5個(gè)1903)
delete from my_copy where name='Python1903' limit 5;
如果表中存在主鍵自增長浮入,那么當(dāng)刪除后將不會(huì)還原龙优,將按原來的數(shù)自動(dòng)往下增長
查看增長到幾,用建表語句——show create table my_student;
數(shù)據(jù)的刪除不會(huì)改變表結(jié)構(gòu)
-- 給my_student表增加主鍵
alter table my_student modify id int primary key auto_increment;
-- 清空表:重置自增長(建議盡量不要隨便使用)
truncate my_student;
查詢數(shù)據(jù)
基本語法:select 字段列表/* from 表名 [where條件];
完整語法:select [select 選項(xiàng)] 字段列表[字段別名]/* from 數(shù)據(jù)源 [where條件子句] [group by子句] [having 子句] [order by子句] [limit 子句];
select 選項(xiàng):select對(duì)查出來的結(jié)果的處理方式
????????all:默認(rèn)的事秀,保留所有的結(jié)果
????????distinct:去重彤断,查出來的結(jié)果,將重復(fù)給去除
-- select 選項(xiàng)
select * from my_copy;
select all * from my_copy;
-- 去重
select distinct * from my_copy;
字段別名
字段名 [as] 別名;
-- 向?qū)W生表插入數(shù)據(jù)
insert into my_student
values (null,'bc20200001','張三','男'),
(null,'bc20200002','李四','男'),
(null,'bc20200003','王五','女'),
(null,'bc20200004','趙六','男'),
(null,'bc20200005','小明','男');
-- 字段別名
select id, number as 學(xué)號(hào), name as 姓名,
sex 性別 from my_student;
別名-數(shù)據(jù)源
數(shù)據(jù)源:?jiǎn)伪頂?shù)據(jù)源秽晚、多表數(shù)據(jù)源瓦糟、查詢語句
????????單表數(shù)據(jù)源:select * from 表名;
-- 單表數(shù)據(jù)源
select * from my_student;
????????多表數(shù)據(jù)源:select * from 表名1,表名2, ...;
-- 多表數(shù)據(jù)源
select * from my_student,my_class;
???子查詢:select * from (select 語句) as 別名;
-- 子查詢
select * from (select * from my_student) as s;
where子句
where原理:唯一一個(gè)直接從磁盤獲取數(shù)據(jù)的時(shí)候就開始判斷條件筒愚,也就是說where之后的都不是操作磁盤上的數(shù)據(jù)赴蝇,而是讀到內(nèi)存,都是在操作內(nèi)存上的數(shù)據(jù)(保證放到內(nèi)存里的數(shù)據(jù)都是合理的)
where子句:返回結(jié)果0或1巢掺,0代表false句伶,1代表true
判斷條件
? ??????比較運(yùn)算符:>劲蜻、<、>=考余、<=先嬉、!=、<>楚堤、=疫蔓、like、between身冬、and衅胀、in/not in
? ??????邏輯運(yùn)算符:&&(and)、||(or)酥筝、!(not)
-- 增加age年齡和height身高字段
alter table my_student add age tinyint unsigned;
alter table my_student add height tinyint unsigned;
-- 增加字段值:rend取得一個(gè)0~1之間的隨機(jī)數(shù)滚躯,floor向下取整
update my_student set age=floor(rand() *20+20),height=floor(rand() *20+170);
-- 找學(xué)生id為1、3嘿歌、5的學(xué)生(以下兩種方法查出結(jié)果一樣)
select * from my_student where id=1 || id=3 || id=5;-- 邏輯判斷
select * from my_student where id in(1,3,5);-- 落在集合當(dāng)中
-- 找身高在185~190之間的學(xué)生(以下兩種方法查出結(jié)果一樣)(左邊必須<=右邊的值)
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 between 190 and 185;-- 系統(tǒng)識(shí)別不出來
select * from my_student where 1;? -- 所有條件都滿足
group by子句
基本語法:group by 字段名 [asc|desc];
統(tǒng)計(jì)函數(shù):(重點(diǎn)掸掏,必會(huì))
count():統(tǒng)計(jì)分組后的記錄數(shù),每一組有多少記錄
max():統(tǒng)計(jì)每組中最大的值
min():統(tǒng)計(jì)最小值
avg():統(tǒng)計(jì)平均值
sum():統(tǒng)計(jì)和
-- 根據(jù)性別分組
select * from my_student group by sex;
-- 分組統(tǒng)計(jì):身高高矮宙帝、平均年齡丧凤、總年齡
select sex,count(*),max(height),min(height),avg(age),sum(age) from my_student group by sex;
-- 修改id為4的記錄,把年齡置位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;
將count(*)與count(age)進(jìn)行比較步脓,count(age)里沒有記錄為null的那條
-- 修改id為1的記錄息裸,把性別置為女
update my_student set sex='女' where id=1;
-- nan nv? 按拼音排序,默認(rèn)升序
-- 倒序(desc)
select sex,count(*), count(age),max(height),min(height),avg(age),sum(age) from my_student group by sex desc;
-- 刪除班級(jí)表主鍵
alter table my_class drop primary key;
-- 給班級(jí)表增加主鍵id
alter table my_class add id int primary key auto_increment;
-- 給學(xué)生表增加一個(gè)班級(jí)表的id
alter table my_student add c_id int;
update my_student set c_id=ceil(rand() *4);-- ceil天花板沪编,向上取整
-- 中途插一條
insert into my_student values(6,'bc20200006','小芳','女',18,160,2);
-- 多字段分組呼盆,先班級(jí),后男女(用在group by后面蚁廓,只能用having)
select c_id,sex,count(*) from my_student group by c_id,sex; -- 多字段排序
多字段排序
group_concat(字段);
-- 分組的結(jié)果中的某個(gè)字段進(jìn)行字符串的連接(保留這個(gè)組的所有字段)
select c_id,sex,count(*),group_concat(name) from my_student group by c_id,sex;