一、數據的管理 :增、刪、改、查
#創建一個新的數據庫 mydb2
create database mydb2;
#查看下當前連接下所有數據庫
show databases;
#切換到新創建的數據庫下進行操作
use mydb2;
#創建表
create table student(
id int primary key auto_increment,
NAME varchar(20) not null,
grade int,
gender varchar(6)
);
desc student;
1.數據的“增”:插入 —— insert into
l 語法一:一次插入1行數據
insert into 表名(字段1,字段2,...字段n)
values(值1,值2,...值n);
例:
insert into student(name,grade,gender) values('zhangsan',100,'male');
select * from student;
insert into student(name,grade,gender) values('lisi',60,'male');
注意:
1、字段名和字段的值要一一對應。
2、id的值設置了 auto_increment自增約束,因此可以自動生成並增長(+1),不需要專門去輸入。
3、字段的值若是字符串類型,則需要加單引號''。
l 語法二:一次插入多行數據
insert into 表名(字段1,字段2,...字段n)
values(第一行 值1,值2,...值n),
(第二行 值1,值2,...值n),
............
(第n行 值1,值2,...值n),
例:
insert into student(name,grade,gender)
values('xiaohong',80,'female'),
('xiaoming',79,'male'),
('xiaowang',70,'male');
2.數據的“刪”:刪除 —— delete from
語法:
delete from 表名 [條件];
注意:條件為可選項,如果不加條件,則會將表里所有記錄都刪除(表結構被保留),如果加上條件,則只有滿足條件的記錄會被刪除。
例:
#刪除整張學生表中的所有記錄
delete from student;
#刪除姓名為tom的記錄
delete from student where name='tom';
3.數據的“改“:更新 —— update ... set...
語法:update 表名 set 修改的內容 [條件];
注意:條件是可選項,如果不加條件,則表中所有記錄都會更新,如果加上條件,則只有符合條件的記錄才會被更新。
例:
#將所有人的成績減去5分
update student set grade=grade-5;
#將豬八戒的成績加上10分
update student set grade=grade+10 where name='豬八戒';
#將豬八戒的成績加上5分,性別改為女生
update student set grade=grade+5,gender='女生' where name='豬八戒';
4.數據的“查”:查詢 —— select...from...(*****)
創建了三張表:
xsb——學生表
xh 學號 --主鍵
xm 姓名
xb 性別
jq 籍貫
nl 年齡
bj 班級
sfzh 身份證號
zcrq 注冊日期
kcb——課程表
kch 課程號 --主鍵
kcm 課程名
cjb——成績表
kch 課程號 --外鍵
xh 學號 --外鍵
cj 成績
2.1簡單查詢:—— select ... from ...
語法:select *|字段名1,字段名2 from 表名;
注意:從指定表內查出所有字段(*)或特定字段。
例:
#查詢學生表所有記錄
select * from xsb;
#查詢學生表所有記錄中的學號、姓名、性別、年齡、班級
select xh,xm,xb,nl,bj from xsb;
2.2.條件查詢:—— where
語法:
select *|字段名1,字段名2
from 表名
where 查詢條件;
注釋:
select子句過濾出滿足條件的列(字段);
from子句確定數據來源於哪張表;
where子句過濾出滿足條件的行(記錄);
例:
#查詢成績表中所有及格了的記錄
select *
from cjb
where cj>=60;
#查詢成績表中不及格的課程號和成績
select kch,cj
from cjb
where cj<60;
l 多條件查詢:—— and、or、not
邏輯運算符:與、或、非
與:並且的意思 —— and
或:或者的意思 —— or
非:取反的意思 —— not
例:
#查詢學生表姓名是張三或李四的信息
SELECT *
from xsb
where xm='張三' or xm='李四';
#查詢學生表中年齡是<=20歲的男生的信息
select *
from xsb
where nl<=20 and xb='男';
#查詢除了張三以外的其他學生的信息
select *
from xsb
where not xm='張三';
#查詢除了張三、李四以外的其他學生的信息
select *
from xsb
where not (xm='張三' or xm='李四');
(————與上面的sql等價————)
select *
from xsb
where not xm='張三' and not xm='李四';
l 模糊查詢:—— like
模糊查詢的常用的通配符:%,_
%:表示此處有0~n個字符。
_:表示此處有1個字符。
例:
#查詢張姓學員的信息
select *
from xsb
where xm like '張%';
l 集合查詢:—— in
例:
#查詢學生表姓名是張三或李四的信息
select *
from xsb
where xm in('張三','李四','王欣');
#查詢學生表中籍貫是北上廣的信息
select *
from xsb
where jg in('北京','上海','廣州');
#查詢學生表中籍貫不是北上廣的信息
select *
from xsb
where jg not in('北京','上海','廣州');
l 空值查詢: —— is null、is not null
例:
#查詢成績表中沒有成績的信息
select *
from cjb
where cj is null;
#查詢所有 有成績表的學生學號和課程號
select xh,kch
from cjb
where cj is not null;
l 范圍查詢: —— between...and...
注意:小值放前面,大值放后面。
例:
#查詢20(含)~24(含)之間的學生信息
select *
from xsb
where nl between 20 and 24;
(——與上面的sql等價——)
select *
from xsb
where nl>=20 and nl<=24;
2.3排序顯示:—— order by ... asc/desc
asc:升序(默認,不寫就是默認asc)
desc:降序
#查詢學生表內的記錄,按年齡 升序排列
select *
from xsb
order by nl;
(——與上面的sql等價)
select *
from xsb
order by nl asc;
#查詢學生表內的記錄,按年齡 降序排列
select *
from xsb
order by nl desc;
#查詢成績,按成績升序排序
select *
from cjb
order by cj;
#查詢成績不為空的,按成績降序排序
select *
from cjb -----1
where cj is not null -----2
order by cj desc; -----3
2.4聚合函數 —— count( )、sum( )、avg( )、max( )、min( )
count( ):統計個數
sum( ):求合
avg( ):求平均數
max( ):求最大值
min( ):求最小值
例:
#統計一共有多少學生
select count(*)
from xsb;
#統計學號為001號的學員,其平均成績、最大成績、最小成績、成績總分
SELECT avg(cj),max(cj),min(cj),sum(cj)
from cjb
where xh='001';
#統計學號為006號學員的平均成績
SELECT avg(cj)
from cjb
where xh='006';
#統計所有學生的平均年齡
2.5分組查詢: —— group by.. 、having
語法:
select 字段 -----5
from 表名 ------1
[where 查詢條件] -----2
[group by 分組字段] -----3
[having 分組后的過濾條件] ------4
[order by 字段 asc|desc]; -----6
group by:按某個字段做分組 (某個字段值相同的算作一組)每,各,統計。1、有聚合函數;2、查出多條記錄;
having:對分組后的數據做進一步過濾
where 和having的區別?
————where:對分組前的數據做過濾;having:對分組后的數據做過濾。
#查詢每個學員的平均分
SELECT xh,avg(cj)
from cjb
group by xh;
#查詢有兩門及以上不及格的學生的學號
#思考一:查詢出所有不及格的學生的學號
select xh
from cjb
where cj<60;
#思考二:如何加上限定查詢兩門以上(不及格的)?———— 按xh分組,統計個數,個數>=2的記錄
group by xh
having count(*)>=2;
#因此sql語句合起來為:
select xh
from cjb
where cj<60
group by xh
having count(*)>=2;
---------------------------------------------
#查每門課的平均成績
select kch,avg(cj)
from cjb
group by kch;
#查每門課的平均成績,其中平均成績不及格的課程及成績
select kch,avg(cj)
from cjb
group by kch
having avg(cj)<60;