数据库之数据的管理:增删改查

数据的插入

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臭屁范

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值