表關系 主鍵沖突 蠕蟲復制 limit 數據源 where子句 group by子句

--給my_class表增加主鍵

alter table my_class add primary key(name);

--插入數據

insert into my_class

values('python1910','B409');

insert into my_class

balues('python1910','B510');

--主鍵沖突:更新

insert into my_class

values('python1910','B510')

--沖突處理

on duplicate key update

--更新教室

room='B510'

insert into my_class

values('python1907','B403');

--主鍵沖突:替換

replace into my_class

values('pythonb1907','A203');

replace into my_class

values('python1811','B407');

--復制創建表

create table my_copy like my_class;

--蠕蟲復制

insert into my_copy select * from my_class;

--刪除主鍵

alter table my_copy drop primary key;

insert into my_copy select * from my_copy;

--更新部分1811變成1911

update my_copy set name='python1911' wyere name='python1811' like 3;

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

delete from my_copy where name='python1903' limit 5;

給my_student表增加主鍵

alter table my_student modify id int primary key

auto_incremen;

--清空表:重置自增長

truncate my_student;

--select 選項

select * from my_copy

select all * from my_copy;

--去重

select distinct * from my_copy;

--向學生表插入數據

insert into my_student

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

(null,'bc2020002','李四','男'),

(null,'bc2020001','王五','男'),

(null,'bc2020001','趙六','女'),

(null,'bc2020001','小明','男');

--字段別名

select id, number as 學號,name as 姓名,

sex 性別 form my_student;

--多表數據源

select * from my_student,myclass;

--子查詢

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

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

alter table my_student add age tinyint unsigned;

alter table my_student add height tinyint unsigned;

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

update my_student set age=floor(rand()*20+20),height-floor(

rand()*20+170);

--找學生id為1,3,5的學生

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

select * from my_student where id in(1,3,5); -- 落在集合中

--找身高在185到190之間的學生

elect * from my_student where height>=185 and height<=190;

elect * from my_student where height between 185 and 190;

elect * from my_student where height between 190 and 185;

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

--根據性別分組

select * from my_student group by sex;

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

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;

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

update my_student set sex='女' where id=1;

--nan? nv

--倒序

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

age),sum(age) from my_student group by sex desc;

--刪除班級表主鍵

alter table my_class drop primary key

auto_incremrnt;.

--給學生表增加一個班級表的id

alter table my_student add c_id int;

update my_student set c_id=ceil(rand()*4);

insert into my_student values(6,'bc20200006','小芳',

'女',18,160,2);

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

select c_id, sex,count(*) from my_student group by

c_id,sex; -- 多字段排序

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

my_student group by c_id,sex;

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容