Day30

Day30
复习:
创建数据库:create database xx;
查看数据库:use xx;
创建表:create table xx( 列名 列类型 not null auto_increment , 列名 列类型, 列名 列类型 )
删除表:drop table xx;
删除数据库:drop database xx;
新内容:
一、数据库基本操作
1、自增约束:
关键字auto_increment
自增 如何使用:通常和主键约束(primary key(id主键列))搭配使用
目的:不重复并且给每行数据一个唯一标识。
2、插入数据insert into pet values( 1,‘旺财’,‘公’,‘牧羊犬’,2 );
insert into pet(pname,psex,type,age ) values( ‘旺财’,‘公’,‘牧羊犬’,2 );
3、更新操作
标准格式:update 表名 列名=更改的数据 where 条件。
例如:(1)update pet set pname=‘旺财2’ where pid=2
(2)update pet set pname=‘旺财3’ where pid=3
(3)所有的宠物 年龄加1
update pet set age=age+1
4、删除操作
标准格式:delete from 表名 where 条件
例如:删除 id 是4 的数据
delete from pet where pid=4
5、查询操作
1)查询满足某一条件的指定列
标准格式:select 列 ,列… from 表
例如:(1)select * from pet;
2)查询满足某一条件的指定列
标准格式:select 列 ,列… from 表 where 条件
(1)select pid,pname ,psex ,age from pet where age>=5 ;(性能较好)
3)查询满足多种条件的指定列
标准格式:select 列 ,列… from 表 where 条件 and 条件
例如:(1)查询性别为公的并且年龄大于5岁的宠物
select * from pet where psex=‘公’ and age>5
4)查询多种条件满足一种的的指定列
标准格式:select 列 ,列… from 表 where 条件 or 条件
例如:(1)查询 公的 或者 年龄大于5岁的宠物
select * from pet where psex=‘公’ or age>5
5)查询满足一种的的指定列
类型:> < >= <= !=(不等于)<>(不等于)
标准格式:select 列 ,列… from 表 where 条件
例如: (1)select * from pet where psex !=‘母’
(2)select * from pet where psex <> ‘母’
6)查询是否为空
标准格式:select 列,列… from 表名where 列 is null;
例如:
查询:所有没有生日的宠物 (
1)select * from pet where birthday is null;
7)查询列的值在括号中的记录
关键字:in
标准形式:select * from 表名 where 列 in (值1,值2,值3…)
例如: 查询宠物年龄是3 5 7 的记录
(1) select * from pet where age in (3,5,7)
(2)select * from pet where age =3 or age=5 or age=7
常适用于不连续的,有限个数的值
8)查找列的值是值1到值2 的范围的值(包含端点)
关键词:between …and…
标准格式:select * from 表名 where 列 between 值1 and 值2
例如:查询 年龄段 是 3~7 的 宠物记录
(1)select * from pet where age BETWEEN 3 and 7(高效一些)
(2)select * from pet where age>=3 and age<=7
9)对表中数据进行模糊查询
关键字: like
类型:
(1)%:代表任意个任意字符
(2):代表一个任意字符
标准格式:select * from 表名 where 列 like ‘%字%’
select * from 表名 where 列 like ‘字%’
例如:
查询宠物名为两个字的宠物信息
select * from pet where pname like '
_’
6、起别名
1)给列起别名
标准格式:列 as ‘别名’ ,其中as 可以省略
例如:(1)select pid ,pname ‘姓名’ ,pname from pet
(2)select pid ,pname as “姓名” ,pname from pet
2)给表起别名
标准格式:select * from 表名 as ‘别名’ ,其中as 可以省略
例如:select stu.sno from s_student as stu
7、排序操作:
用法:通常 放在查询的最后面 默认是 asc排序
标准格式:
升序:select * from 表名(where 条件)order by 列 asc ;
降序:select * from 表名(where 条件 )order by 列desc ;
例如:(1)select * from pet where psex=‘公’ order by age desc ;
(2)先按照 性别排序 然后相同性别的情况下 再根据年龄 排序
select * from pet order by psex DESC , age asc ;
8、行级去重
关键字:distinct
标准格式:SELECT DISTINCT 列from 表名
作用:将结果级中的行数据比较,重复的去掉
例如:查询pet表中所有的性别和类型
SELECT DISTINCT psex, type from pet;
9、多行函数(聚合函数)
作用:会将 结果集进行统计 汇总变成一行
类型: avg() :平均值
max():最大值
min():最小值
sum():求和
count():计数
标准格式:SELECT 聚合函数(列)from 表名
例如:(1)查询Pet表中年龄的个数、最大值、最小值、平均值、所有年龄和
select count(),max(age),min(age),avg(age),sum(age) from pet ;
(2)查询Pet表中性别为公的宠物个数
select count(
) from pet where psex=‘公’
注意 :(1)count(列名 或 ) 如果是列名统计的是非null的 记录数
(2) count (
) 就是统计 记录数量
(3) 使用聚合函数 不建议加其他的非聚合函数的列
(4)聚合函数不能直接用在where 后面,若要使用需要在where后面加上查询语句
例如: 查询年龄大于平均年龄的宠物
select * from pet where age> (select avg(age) from pet)
10、分组查询
关键字:group by
标准格式:select 列 from 表名 group by 列
例如:(1)将宠物按照性别分组
select psex from pet group by psex;
(2) 统计各班级的人数信息
select classes ,count() from s_student group by classes ;
(3) 统计各班男生的人数并且班级按降序排列
select classes,count(
) ‘人数’ from s_student where ssex=‘男’ group by classes order by classes desc;
若统计过后进行数据筛选,格式为group by 列 having 条件即可
例如: 统计 男生人数 大于2 的 班级 及其人数
select classes,count() ‘人数’ from s_student where ssex=‘男’ group by classes having count()>2;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值