数据的插入
insert into 表名(字段1,字段2,...)
values(值1,值2,...),
(值1,值2,...),
...
例:
insert student(sid,sname.score,semail)
values(01,'李四',88,'1234@qq.com')
(02,'王五',90,'23456@qq.com')
数据的查询
1.查询所有字段
select 所有字段名/* from 表名
例:
select 所有字段名/* from student
2.查询特定的字段
select 指定的字段 from 表名
例:
select sid,sname from student
3.条件查询
select 字段名 from 表名 where 查询条件
例:
select sname score from student where sid = 01;
关系运算
关系运算符:= != > >= < <=
支持多条件查询,多条件查询常用逻辑运算符
与 和的意思 and
或 或者的意思 or
非 取反的意思 not
例:
select * from student where sname='张三' and sanme='李四'
select * from student where sname=('张三' ,'李四')
模糊查询(like)
常用通配符 :
%:表示%处有0个,1个或多个字符
_:表示_处有1个字符
例:
select * from student where sname like 张%
空值查询(is)
is null:是空值
not is null:不是空值
例:
select * from student where score is null
注:
from 用来明确数据的来源
where 过滤出满足条件的行
select 过滤出满足条件的所有字段
4.排序显示
select 字段
from 表名
where 查询条件
order by 字段 asc|desc
asc:ascend 升序
desc:descend 降序
例:
select * from student order by sid asc
5.统计函数
统计函数一般用于分组统计
count() 统计个数
avg() 求平均值
sum() 求和
max() 求最大值
min() 求最小值
例:
select count(sid) from student
6.分组查询
select 字段
from 表名
where 查询条件
group by 分组字段
having 过滤条件
order by 字段 asc|desc
例:
select xh sum(cj) from cjb group by xh;
注:
group by 按某个字段做分组
having 对分组后的数据进一步过滤
where 对分组之前的数据做过滤,统计函数不能出现在where子句里
数据的更新
update 表名 set 修改的内容 [where 更新条件];
例:
update cjb set cj=cj-2 where xh = '001';
注:
不跟条件表里的所有记录都会更新
跟条件只有满足条件的记录才会更新
数据的删除
delete from 表名 [where 查询条件]
例:
delete from cjb where xh='001';
删除操作时,如果有外键关联,需要先删除子表例的信息,才能删除父表里的信息
注:
不跟条件会删除表里所有信息,保留表结构
跟条件只会删除满足条件的信息
总结
数据库查询语句执行顺序:
select *|字段 5
from 表名 1
where 查询条件 2
group by 分组字段 3
having 过滤条件 4
order by 字段 asc|desc 6