1.字段的约束
(1)常用约束介绍
主键(primary key):值不能重复, auto_increment代表值自动增长;
非空(not null):此字段不允许填写空值;
唯一(unique):此字段的值不允许重复;
默认值(default):当不填写此值时会使用默认值,如果填写时以填写为准。
(2)创建带约束字段的语法格式
create table 表名(字段名 数据类型 约束,字段名 数据类型 约束,...)
例1:创建表d,字段要求如下:id:数据类型为int unsigned(无符号整数)),primary key(主键),auto_increment(自增长);name姓名:数据类型为varchar(字符串)长度为10 非空;age年龄:数据类型为int(整数);
create table d ( id int unsigned primary key auto_increment, name varchar(10) not null, age int);
(3)如果不指定字段,主键自增长字段的值可以用占位符,0或者nu11
insert into d values (0, '康熙',30);
insert into d values (NULL,'薄仪',5O);
2.字段的别名
语法: 字段名 as 别名
别名可以是中文,as可以省略
例1:通过as给字段起一个别名
select card as 身份证,name as 姓名,sex as 性别 from students;
例2:别名的as可以省略
select card 身份证,name姓名,sex性别from students;
3.表的别名
语法:表名 as 别名
as可以省略
例1:通过as给表students起一个别名
select * from students as stu;
-可以省略as
select * from students stu;
4.distinct 过滤重复记录
语法:select distinct 字段名,字段名 from 表名
过滤select查询结果中的重复记录
例1:查询学生表中性别和班级的不重复的记录
select distinct sex,class from students;
5.where子句
where后面跟一个条件,实现有选择的查询
语法:select * from 表名 where 条件
例1:查询 students 表中学号studentNo等于'001’的记录
select * from students where studentNo ='001';
例2:查询students表中年龄 age等于30的姓名name,班级class
select name,class from students where age= 30;
6.比较运算符
- 大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: !=和<>
例1:查询students表中name(姓名)等于’小乔'学生的age(年龄)
select age from students where name ='小乔';
例2:查询students表中30岁以下的学生记录
select * from students where age<30;
例3:查询students表中30岁和30岁以下的学生记录
select * from students where age <= 30;
例4:查询家乡不在北京的学生记录
select * from students where hometown !='北京';
select * from student swhere hometown <>'北京';
7.逻辑运算符
(1)and与
- 条件1 and 条件2
- 两个条件必须都满足
(2)or或
- 条件1 or 条件2
- 两个条件只要有一个满足即可
(3)not
- not 条件
- 条件成立,not以后就不成立,条件不成立,not以后就成立
例1:查询age 年龄小于 30,并且sex性别为'女’的同学记录
select * from students where age<30 and sex='女';
例2:查询sex性别为'女或者c1ass班级为'1班’的学生记录
select * from students where sex='女' or class='1班';
例3:查询hometown老家非'天津'的学生记录
select * from students where not hometown='天津';
例4:查询hometown老家'天津'的学生记录
select * from students where not hometown!=天津';
练习题:
练习1:查询hometown老家是'河南'或'河北'的学生记录
练习2:查询class班级为'1班’,并且hometown老家为北京的学生记录
练习3:查询age年龄非30岁的学生记录;
8.模糊查询
- like 实现模糊查询
- % 代表任意多个字符
- _ 代表任意一个字符
语法:字段名 like '字符%'
例1:查询name姓名中以'孙’开头的学生记录
select * from students where name 1ike '孙%';
例2:查询name姓名以'孙'开头,且名只有一个字的学生记录
select * from students where name like '孙_';
练习题
练习1:查询Jname姓名为两个字的学生记录; 练习2:查询name姓'白'且年龄大于30的学生记录; 练习3:查询studentNo学号以1结尾的学生记录;
9.范围查找
(1) in(值,值,值)
非连续范围查找
(2) between开始值and结束值
连续范围查找,包含开始值包含结束值
例1:查询hometown家乡是北京或上海或广东的学生记录
select * from students where hometown='北京'or hometown =上海'or hometown ='广东';
select * from students where hometown in('北京','上海','广东');
例2:查询age年龄为25至30的学生记录
select * from students where age>=25 and age<=30;
select * from students where age between 25 and 30;
练习题
练习1:查询age年龄在20或25或30的女生记录
练习2:查询age年龄25到30以外的学生记录;
10.空判断
- null不是0,也不是'',nul在SQL里面代表空,什么也没有
- null不能用比较运算符的判断
- is null---是否为null
- is not null---是否不为null
- 不能用 字段名=null 字段名!=null 这些都是错误的
例1:查询card身份证为nul1的学生记录
select * from students where card is null;
例2:查询card身份证非nu11的学生记录
select * from students where card is not null;
11.where子句在update与delete语句中同样有效
例1:修改age为25,并且name为孙尚香'学生的class为'2班
update students set class='2班' where age=25 and name='孙尚香';
例2:删除class为'1班,并且age大于30的学生记录
delete from student where class='1班' and age>30;
12.排序
语法:select * from 表名 order by 字段1 asc|desc,字段2 asc|desc
将行数据按照字段1进行排序,如果某些行字段1的值相同时,则按照字段2排序,以此类推;
- 默认按照字段值从小到大排序;
- asc从小到大排序,即升序(可省略);
- desc从大到小排序,即降序(不可省略);
例1:查询所有学生记录,按age年龄从小到大排序
select * from student order by age asc;
例2:查询所有学生记录,按age年龄从大到小排序,年龄相同时,再按studentNo学号从小到大排序
select * from student order by age desc,studentNo;
where 结合order by
当一条select语句出现了where和order by
语法:select * from 表名 where 条件 order by字段1,字段2;
一定要把where写在orderby前面
例1:查询所有男学生记录,按class 班级从小到大排序,班级相同时,再按studentNo学号再按学号从大到小排序
select * from students where sex =男' order by class,studentNo desc;
聚合函数
聚合函数不能用到where后面的条件里
练习题:
练习1:查询所有学生的最大年龄、最小年龄、平均年龄;
练习2:查询1班共有多少个学生;
练习3:查询3班中年龄小于30岁的同学有几个;
1.count总记录数
语法:count(字段名)
例1:查询学生总数(查询stuents表有多少记录)
select count(*) from students;
select count(name) from students;
select count(DISTINCT class) from students;
例2:查询女同学数量
select count(name) from students where sex='女';
select count(*) from students where sex=女';
2.max最大值
语法:max(字段名)
查询指定字段里的最大值
例1:查询students中的最大年龄
select max(age) from students;
例2:查询students中的女生最大年龄
select max(age) from students where sex='女';
例3:查询students中的'1班最大年龄
select max(age) from students where class ='1班';
3.min最小值
语法:min(字段名)
查询指定字段里的最小值
例1:查询students中的最小年龄
select max(age) from students;
例2:查询students中的女生最小年龄
select min(age) fr不作为students where sex='女';
例3:查询students中的'1班最小年龄
select min(age) from students where class ='1班';
4.sum求和
语法:sum(字段名)
查询指定字段的值求和
例1:查询students中的年龄之和
select sum(age) from students;
例2:查询students中的女生年龄之和
select sum(age) from students where sex='女';
例3:查询students中的'1班年龄之和
select sum(age) from students where class ='1班';
5.avg平均值
语法:avg(字段名)
查询指定字段的平均值
avg的字段中如果有null,null不作为分母计算平均
例1:查询学生平均年龄
select avg(age) from students;
例2:查询students中的女生平均年龄
select avg(age) from students where sex='女';
例3:查询students中的'1班平均年龄
select avg(age) from students where class ='1班';