复习
数据库
分类: 关系型 非关系型
数据库管理控制
优缺点:
特点 开源 中型 跨平台
数据库-- 数据表 --- 数据元素-- 字段 --记录
数据库的安装 和状态查看
sudo service mysql status/stop/start/restart
2.数据库的创建
show datatbases;
create database [db] charset=utf8;
use [db];
select database();
drop database [db];
3.数据表
创建
create table [tb] (字段1 类型 ...);
primary key
auto_increment
unsigned
not null
default XX
3.
show tables; 查看表
# 查询 select
select * from [tb] where 运算 条件;
select 字段1,字段2 from [tb] ;
insert into [tb] values (); 全部的字段
insert into [tb] 字段1, values (val1,);
作业:
1.请把今天的代码重敲一遍
2.聚合练习
1. 统计每位作家出版图书的平均价格
2. 统计每个出版社出版图书数量
3. 统计同一时间出版图书的最高价格和最低价格
4. 筛选出那些出版过超过50元图书的出版社,
并按照其出版图书的平均价格降序排序
```sql
练习 使用book表
1. 将呐喊的价格修改为45元
update books set price =45 where bname='呐喊';
2. 增加一个字段出版时间 类型为 date 放在价格后面
alter table books add p_time date after price;
3. 修改所有老舍的作品出版时间为 2018-10-1
update books set p_time = '2018-10-1' where
author = '老舍';
4. 修改所有中国文学出版社出版的但是不是
老舍的作品出版时间为 2020-1-1
update books set p_time = '2020-1-1' where
press = '中国文学出版社' and author !='老舍';
5. 修改所有出版时间为Null的图书
出版时间为 2019-10-1
update books set p_time = '2019-10-1'
where p_time is null;
6. 所有鲁迅的图书价格增加5元
update books set price = price+5 where
author = '鲁迅';
7. 删除所有价格超过70元或者不到40元的图书
delete from books where price not between 40 and 70;
```sql
# 更改某个字段
update class set score = score+1
where id = 7;
update class set score = 98,age =18
where id = 7;
update class set age = age+1;
# 删除
delete from class
where score=0 and sex='w';
#修改字段 alter
alter table hobby add phone
char(12) after price;
alter table hobby add address varchar(56);
alter table hobby change phone tel char(50);
alter table hobby modify level char(2);
alter table hobby drop level;
#时间类型
create table marathon(id int primary key
auto_increment,athlete varchar(32) not null,
birthday date,r_time datetime,
performance time);
insert into marathon values
(1,"曹操","1998-2-16","2021/5/6 10:10:27","2:38:49"),
(2,"关羽","2000-7-19","2021/4/30 16:22:09","2:27:18"),
(3,'孙策',"1995-10-23","2021/5/2 20:1:2","2:44:00");
select * from marathon where birthday >'2000-5-2';
select * from marathon where birthday >'2000-5-2' and performance <='3:0:0';
update marathon set athlete ='博 尔特' where id =2;
alter table marathon add level char(2);
alter table marathon
change level lel char(1);
alter table marathon
modify lel char(3);
alter table marathon drop lel;
# 表名重命名
alter table marathon rename mar;
# 模糊查询
select * from class where name like "J%"; #查询以J开头的名字
# _ 代表一个字符
select * from class where name like "___";
select name,hobby from hobby
where hobby like '%sing%';
#名字里有a的
select * from hobby where name like '%a%';
# as 重命名
# 将字段名字改为 姓名 分数,只修改当前这句话的查询结果
select name as 姓名,score as 分数 from class where score >90;
# 原来的写法 显示结果 的字段名字是 name,score
select name,score from class where score >90;
# 修改表名
select cls.name,cls.score from class as cls where cls.sex ='w';
# 排序 order by
select * from class order by score;
select * from class
where sex='m' order by score desc;
select * from class order by age,score desc;
# 限制 limit[num] [offset num]
select name,score from class limit 3;
select name,score from class
where sex='m' order by score
desc limit 1;
select name,score from class
where sex='m' order by score desc
limit 1 offset 2; # 求第三名
# union 联合查询
select * from class where age>18 union
select * from class where sex = 'm';
select name,age,score from class union
select name,hobby,price from hobby;
# 注意: 字段个数必须一致
select name,age,score from class
where id>7 union
select name,hobby,price from hobby
where id >3;
select name,age,score from class
where id>7 union
select name,hobby from hobby
where id >3; #这个不可以,字段个数不一致
#联合查询 跨表 跨表排序
#两句话的结果 进行排序
#排序的字段必须是第一表格的字段排序
select name,age,score from class
where id>7 union select
name,hobby,price from hobby where
sex ='m' order by score desc;
select name,age,score from class
where id>7 union select
name,age,score from class where
sex ='m' order by score desc;
select * from (select * from class where sex='m')
as s where s.score >80;
# 子查询的时候 作为一个结果/值
select * from class where age =
(select age from class
where name = 'Tom');
# class中有几个报兴趣班的
# in 返回多个结果
select * from class where name
in(select name from hobby);
create table sanguo
(id int primary key
auto_increment,zero varchar(30),
sex enum ('男','女','其他'),
country enum('魏','蜀','吴'),
attack smallint,
defence tinyint);
insert into sanguo values
(1, '曹操', '男', '魏', 256, 63),
(2, '张辽', '男', '魏', 328, 69),
(3, '甄姬', '女', '魏', 168, 34),
(4, '夏侯渊', '男', '魏', 366, 83),
(5, '刘备', '男', '蜀', 220, 59),
(6, '诸葛亮', '男', '蜀', 170, 54),
(7, '赵云', '男', '蜀', 377, 66),
(8, '张飞', '男', '蜀', 370, 80),
(9, '孙尚香', '女', '蜀', 249, 62),
(10, '大乔', '女', '吴', 190, 44),
(11, '小乔', '女', '吴', 188, 39),
(12, '周瑜', '男', '吴', 303, 60),
(13, '吕蒙', '男', '吴', 330, 71);
1. 查找所有蜀国人信息,按照攻击力排名
select * from sanguo where
country = '蜀' order by attack desc;
2. 吴国英雄攻击力超过300的改为300,
最多改2个
update sanguo set attack =300
where country ='吴' and
attack>300 limit 2;
3. 查找攻击力超过200的魏国英雄名字和
攻击力并显示为姓名, 攻击力
select hero as 姓名,attack as 攻击力
from sanguo where country='魏' and
attack >200;
4. 所有英雄按照攻击力降序排序,
如果相同则按照防御升序排序
select * from sanguo order by attack
desc,denfence;
5. 查找名字为3字的
select * from sanguo
where name like "___";
6. 找到魏国防御力排名2-3名的英雄
select * from sanguo where country
='魏' order by defence desc
limit 2 offset 1;
7. 查找所有女性角色中攻击力大于180的
和男性中攻击力小于250的
select * from sanguo where sex='女'
and attack >180 union select * from
sanguo where sex ='男' and attack<250;
8. 查找攻击力比魏国最高攻击力的人还要
高的蜀国英雄
select * from sanguo
where country='蜀' and attack >
(select attack from sanguo
where country = '魏' order by
attack desc limit 1);
# 聚合函数
select max(attack) from sanguo;
select count(*) from sanguo;
select avg(attack) from sanguo
where country ='蜀';
select count(*) as num from sanguo;
select country,avg(attack) from sanguo where country ='蜀' group by country;
所有国家的男英雄中 英雄数量最多的前2名的
国家名称及英雄数量