找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
20、数据库增删改查
1)增加  insert into +表名
单条数据插入
insert into tab11(id,score,name,phone,time)values(1,66.88,'xiaoduan',13988886666,'2022-5-31');
insert into tab11 values(1,88.66,'xiaoliu',13466668888,'2022-5-31');

多条数据插入
insert into tab11(id,score,name,phone,time)values(3,66.88,'xiaoduan',13988886666,'2022-5-31'),
(4,66.33,'xiaoyi',13456789876,'2022-5-31'),(5,66.66,'xiaduan',13499998888,'2022-1-3');

insert into tab11 values(6,66.34,'xiaoming',13666668888,'2022-5-12'),(7,66.99,'xiaohe',13866667777,'2022-5-30'),
(8,66.68,'xiaohu',13734567890,'2022-5-31');

单个字段插入
insert into tab11(name)values('xiaotao');==》只给name 插入字段时,id字段自动加一,
可以为空的字段以null填充,score字段以0.00填充

insert into tab11(score)values(66.7899);==》只给score 插入字段时

0 不等于null,0时一个值,null是一个属性,空

数据库怎么去测试?
数据库类型
约束类型
存储的大小  边界值

2)删  delete from 表名

delete from tab11 where id =10;==》把id等于10 的数据删除
delete from tab11 where id > 6;==》把id大于6的数据删除
truncate tab11;==》快速删除全表数据

注:TRUNCATE,DELETE,DROP放在一起比较:
TRUNCATE TABLE:快速删除表数据。
DELETE TABLE: 按条件删除表数据 和where 条件使用
DROP TABLE:删除表结构和表数据

3)改 update 表名 +set 字段= ?
update +表名 set 字段名=值 where 条件
update tab11 set name = 'xiaoduan' where id = 4;==》把id等于4的数据姓名改为xiaoduan
update tab11 set name = 'xiaoduan';   ==》把全表的数据姓名字段改为xiaoduan
update tab11 set score = 100 where id = 1;==》把id等于1的score分数改为100分



4)查

1.查询表中所有数据
select * from +表名
”*“ 代表所有
select * from tab11;  ==》查询tab11这个表的全部数据

2. 查询某个字段的数据
select 字段 from +表名
select id from tab11; ---》查询tab11表中id 字段的值


3. 查询多个字段的数据
select 字段1,字段2 from +表名
select id,name,score from tab11;---》查询tab11表中  id name score 字段值


创建stu
/*create table stu(id int(4) primary key,age int(8),sex int(4),name varchar(20),class int(4),math int(4));

insert into stu values(1,25,1,"zhangsan",1833,90),
(2,25,1,"lisi",1833,67),(3,28,0,"xiaowang",1835,79),(4,35,1,"xiaoliu",1835,96),(5,27,0,"xiaoli",1833,86),
(6,32,1,"xiaochen",1835,48),(7,22,1,"wangwu",1834,70),(8,31,0,"xiaoqi",1825,88),
(9,27,0,"xiaoqi",1833,74),(10,27,1,"niuqi",NULL,80);
*/




4.查询满足某个条件的所有数据
select * from +表名 where 字段=值
where 后面接满足的条件
select * from  stu where id = 1;  ==》查询id为1的数据


5.查询不满足某个条件的所有数据
select * from +表名 where 字段!=value
“!=
” 代表不等于 ,也可以用符号 ”<>“ 代表不等于
#查询sex 字段不等于1的数据
#select * from  stu where sex <>1;(select * from student where sex != 1;)



6.查询同时满足多个条件数据
select * from +表名 where 条件1 and 条件2
and 关键字左右的两个条件必须同时满足

select * from stu where class = 1833 and sex = 1;   ==》查询class为1833并且sex为1的数据



7.查询满足至少1个条件的数据
select * from +表名 where 条件1 or 条件2
or 关键字左右的两个条件至少满足1个,否则返回空

#查询class为1833或者class为1835的数据
select *from stu where class = 1833 or class = 1835;
select * from stu where class in (1833,1835);


8.查询一个条件范围内的数据
select * from +表名 where 字段 between m and n
between...and ... 指定一个范围

select * from stu where age between 25 and 31;   #查询年龄在25和31 之间的数据


9查询字段满足在指定的集合中的数据
select * from +表名 where 字段 in(值1,值2,值3)
select * from stu where class in (1833,1835);    ==》#查询class为1833或者class为1835的数据


10查询字段不满足在指定集合中的数据
select * from +表名 where 字段 not in (值1,值2,值3)
select * from stu where id not in (1,3);     ==》查询id 不在1和3里面的数据


11.查询字段值为空的数据
select * from +表名 where 字段 is null
注意:字段是空不能写成 字段=null

select * from stu where class is null;  ==》查询班级为空的数据



注:0不等于null,0是一个值,  null 是一个空属性,所以用is  


12.查询字段不为空的数据
select * from +表名 where 字段 is not null
select * from stu where class is not null;   ==》查询class 字段不为空的数据

13.查询某个字段模糊匹配成功的数据
select * from +表名 where 字段 like “%值%”
%用于匹配字段开头和结尾

select *from stu where name like 'ci%';  ==》查询姓名以“ci”开头的数据


select * from stu where name like '%qi';  ==》查询姓名以‘qi’结尾的数据

select * from stu where name like '%qi%';  ==》查询姓名含有‘qi’的数据

14.查询限定的数量的数据
select * from +表名 where 字段 limit m,n
m 指下标,n指限定的数量,下标为m的开始的n条数据

前5行  ==》1到5行   ==》limit 0,5   就是前面这个数1-1=0, 然后减完后+?后面这个数5  ==》0+5=5

查看2到6行的数据  ==》limit 1,5    ==>2-1 =1   1+?=6  ==》1+5=6
查看4,7行的数据  ==》limit 3,4

#查询前5行的数据
#select * from stu limit 0,5; (select * from student limit 5;)  --》一般数据库从0开始算起的



15.查询的数据根据某个字段从小到大排序
select * from +表名 order by 字段 asc
order by ...asc 从小到大排序
select * from stu order by id asc;  (ascend)  ==》#查询表数据按id从小到大排序


16.查询的数据根据某个字段从大到小排序
select * from +表名 order by 字段 desc
order by ... desc 从大到小排序

select * from student ORDER BY  id desc; (descend)  ==》#查询表数据按id从大到小排序

17.查询的数据根据某个字段进行分组
select * from +表名 group by 字段
group by ... 根据条件分组

select class,count(*)  from stu group by class; ==》#根据班级class字段分组、然后求出每个班级的人数

帮class和count(*)分别取别名a和b
select class as a,count(*) as b from  stu group by class;

帮class和count(*)分别取别名"班级"和"每个班级总人数"
elect class as 班级,count(*) as 每个班级总人数 from stu group by class;


18.查询的数据根据某个字段进行分组再条件过滤
select * from +表名 group by 字段 having 条件
having跟在group by 后面,作用相当于where

求出每个班的数学总成绩大于200的班级和成绩信息
select class,sum(math) from stu group by class having sum(math)>200;


mysql聚合函数
19. 统计查询数据的数量
select count(*) from +表名
20. 查询某个字段求和
select sum(字段) from +表名
21. 查询某个字段进行平均值
select avg(字段) from +表名
22. 查询某个字段最大值
select max(字段) from +表名
23. 查询某个字段最小值
select min(字段) from +表名
24 对某个字段进行去重
select distinct(字段) from +表名   ==》select distinct(sex) from stu;  ==》查询按性别去重


题目:
求每个班的数学总分
select class,sum(math) from stu group by class;

求每个班的数学平均分
select class,avg(math) from stu group by class;

求每个班的数学最低分
select class,min(math) from stu group by class;

求每个班的数最高分
select class,max(math) from stu group by class;


题目:
求出每个班级的数学成绩大于80的人数
select class,count(*) from stu where math >80 group by class;

求出每个班中性别为1的数学总成绩
select class,sum(math) from stu where sex = 1 group by class;

求出每个班的数学总成绩大于200的班级和成绩信息
select class,sum(math) from stu group by class having sum(math)>200;


【单表查询总结】
1、分组函数group by 只能和聚合函数一起使用,要加只能加分组字段

2、where 后面可以接group by 分组函数,但是group by 后面不能跟where

3、group  by 前面加where是先过滤在分组

4、having 可以接在group  by 后面,类似于where ,筛选分组后满足条件的


123456

21、[数据库的备份]
1)备份表
create table tab12 like tab11;  ==》创建一个tab12表,和tab11表一样的结构(只是备份结构)
insert into tab12 select * from tab11;  ==》往tab12表中插入和tab11表一样的数据(全部数据)
insert into tab12(id,name,phone)select id,name,phone from tab11;
==》往tab12 表中插入和tab11 表中id name phone 一样的数据
注意点
1. 插入的表必须存在
2. 插入的表是新表,没有数据。


2)备份库
mysqldump -uroot -p123456 dcs11 >/dcs11/dcs11_bak  ==》把dcs7这个库备份到根目录下dcs11下名为dcs11_bak

mysql -uroot -p123456 dcs11 </dcs11/dcs11_bak  ==》把根目录下dcs11下名为dcs11_bak 还原到dcs11这个库里面


分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册