SQL语言之数据库增删改查

SQL语言之数据库增删改查

简单查询数据库表

select * from 表名

创建表

create table 表名(

​ 字段名 类型 约束,

​ 字段名 类型 约束,

)

例如:创建学生表,字段要求如下

姓名(长度为10)

create table student(

​ name varchar(10) ,

​ age int unsigned

)

注:unsigned是无符号的意思

– 例:创建学生表,字段要求如下:
– ——
– ——姓名(长度为10),年龄,身高(保留小数点2位)

create table student2(
id int unsigned primary key auto_increment,
-- 创建id组件为无符号,自动排序
name varchar(10),
age int unsigned,
-- unsigned为无符号
height decimal(5,2)
-- decimal为小数的类型,(5,2)代表整数为3位,小数为2位

)

ps:动态添加字段:alter table 表名 add 字段名 字段类型

删除表

格式一:drop table 表名

格式二:drop table if exists 表名

例如:删除学生表

drop table students

drop table if exists students

添加数据

添加一行数据

格式一:所有字段设置值,值的顺序与表中字段的顺序对应

说明:主键列是自动增长,插入时需要占位,通常使用0或者default或者null来占位,插入成功后以实际数据为准

insert into 表名 values(...)

例如:插入一个学生,设置所有字段的信息

insert into student value(0,'张三',18,177.56)

格式二:部分字段设值,值的顺序与给出的字段顺序对应

insert into 表名(字段1,...) values(值1,...)

例如:插入一个学生,只设置姓名

insert into student(name) values('李四')

同一个字段插入多个值

格式:insert into student(name) values('王五'),('老六'),('涂七')

修改数据

格式:update 表名 set 列1=值1,列2=值2… where 条件

例如:修改id为5的学生书局,姓名改为狄仁杰,年龄改为18岁

update student set name=狄仁杰,age=18 where id=5

删除数据

格式:delete from 表名 where 条件

例如:删除id为6的学生数据

delete from student where id=6

逻辑删除

设计表,给表添加一个字段isdelete,1代表删除,0代表没有删除

update student set isdelete=0

update student set isdelete=1 where id=1

查询

简单查询

​ 查询指定列,如果没有写where会把指定列的所有数据都显示出来

select name,sex,hometown from student

​ 给查询出来的字段起别名

​ select name as 姓名,sex as 性别,hometown as 家乡 from student

​ 给表起别名

select s.name,s.sex,s.hometown from student as s

​ 查询学生的性别有哪几种

​ 去掉重复数据,如果有重复的数据只显示一条

select distinct sex from student

​ 去掉重复数据,当查询多个字段时,只有一行记录会跟另一行记录完全一样时才是重复数据

select distinct sex,class from student

select distinct * from student

比较运算符

等于:=

大于:>

大于等于:>=

大于:<

小于等于:<=

不等于:!=或者<>

例1:查询小乔的年龄

select age from student where name='小乔'

例2:查询20岁以下的学生

select * from student where age<20

例3:查询家乡不在北京的学生

select * from student where hometown!='北京'

练习:

1、查询学号是’007’的学生的身份证号

select * from student where studentno='007'
select card from student where studentno='007'

2、查询’1班’以外的学生信息

select * from student where class!='1班'

3、查询年龄大于20的学生的姓名和性别

select name,age from student where age>20

逻辑运算符

and

or

not

例1:查询年龄小于20的女同学

select * from student where age<20 and sex='女'

例2:查询女学生或’1班’的学生

select * from student where sex='女' or class='1班'

例3:查询非天津的学生

select * from student where not hometown='天津'

练习:

1、查询河南或者河北的学生

select * from student where hometown='河南' or hometown='河北'

2、查询’1班’的’上海’的学生

select * from student where class='1班' and hometown='上海'

3、查询非20岁的学生

select * from student where not age=20

模糊查询

like

%表示任意多个任意字符

_表示一个任意字符

例1:查询姓孙的学生

select * from student where name like '孙%'

例2:查询姓孙且名字是一个字的学生

select * from student where name like'孙_'

例3:查询叫乔的学生

select * from student where name like'%乔'

例3:查询姓名含白的学生

select * from student where name like '%白%'

练习:

1、查询姓名为两个字的学生

select * from student where name like '__'

2、查询姓百且年龄大于20的学生

select * from student where age>20 and name like '百%'

3、查询学号以1结尾的学生

select * from student where studentno like '%1'

范围查询

in 表示在一个非连续的范围之内

例1:查询家乡是北京或上海或广东的学生

select * from student where hometown='北京' or hometown='上海' or hometown='广东'  or语法
select * from student where hometown in('北京','上海','广东')		in语法

between … and …表示在一个连续的范围之内

例2:查询年龄为18至20的学生

select * from student where age between 18 and 20

练习:

1、查询年龄在18或19或22的女生

select * from student where age in('18','19','20') and sex='女'

2、查询年龄在20到25以外的学生

select * from student where not age between 20 and 25

空判断

注意:null与’'是不同的

判断is null

例1:查询没有填写身份证的学生

select * from student where card is null

判断非空is not null

例2:查询填写了身份证的学生

select * from student where card is not null

排序

为了方便查看数据,可以对数据进行排序

语法:

select * from 表名 order by 列1 asc|desc,列2 asc|desc,...

将行数据按照列1进行排序,如果某些行列1的值相同时,则按照2排序,以此类推

默认按照列值从小到大排序

asc从小到大排列,既升序

desc从大到小排序,既降序

例1:查询所有学生信息,按年龄从小到大排序

select * from student order by age asc

例2:查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序

select * from student order by age desc,studentno desc

练习:

1、查询所有学生信息,按班级从小到大排序,班级相同时,再按学号从从小到大排序

对中文数据进行排序
select * from student order by convert(name using gbk)

聚合函数

为了快速得到统计数据,经常会用到如下5个聚合函数

count(*)表示计算总行数,括号中写星与列名,结果是相同的

聚合函数不能在where中使用

例1:查询学生总数

select count(*) from student;

max(列)表示求此列的最大值

例2:查询女生的最大年龄

select max(age) from student where sex='女';

min(列)表示求此列的最小值

例3:查询1班的最小年龄

select min(age) from student;

sum(列)表示求此列的和

例4:查询北京学生的年龄总和

select sum(age) from student where hometown='北京';

avg(列)表示求此列的平均值

例5:查询女生的平均年龄

select avg(age) from student where sex='女';

练习:

1、查询所有学生的最大年龄、最小年龄、平均年龄、

select max(age) as 最大年龄,min(age) as 最小年龄,avg(age) as 平均年龄 from student;

2、一班共有多少个学生

select sum(name) from student where class='一班'
3、查询3班年龄小于18的同学有几个
select sum(age) from student where age<18;

分组

按照字段分组,表示此字段相同的数据会被放到一个组里

分组后,分组的依据会显示在结果集中,其他列不会显示在结果集中

可以对分组的数据进行统计,做聚合运算

语法:

select1,列2,聚合... from 表名 group by1,列2...

例1:查询各种性别的人数

select sex,count(*) from student group by sex

例2:查询各种年龄的人数

select age,count(*) from student group by age

练习:

查询各个班级学生的平均年龄,最大年龄,最小年龄

select class,avg(age) as 平均年龄,max(age)最大年龄,min(age) as 最小年龄 from student groub by class;

分组后的数据筛选

语法:

select1,2,聚合...from 表名 group by1,2,3... having1,...聚合...

having后面的条件运算符与where的相同

例1:查询男生总人数

方案1select count(*) from student where sex='男'
___________________________________________
方案二:
select sex,count(*) from students group by sex having sex='男'

练习:

查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄
select class,avg(age),max(age),min(age) from student where class!='1班' group by class

select class,avg(age),max(age),min(age) from student group by class having class!='1班'

获取部分行

当数据量过大时,在一页中查看数据是一件非常麻烦的事情

语法:

select * from 表名 limit start,count

从start开始,获取count条数据

start索引从0开始

例1:查询前3行学生信息

select * from student limit 0,3

练习

查询第四到第六行学生信息

select * from student limit 3,3

分页

已知:每页显示m条数据,求:显示第n页的数据

select * from student limit (n-1)*m,m

求总页数

​ 查询总条数p1

​ 使用p1除以m得到p2

​ 如果整除则p2为总页数

​ 如果不整除则p2+1为总页数

连接查询

方法一,等值连接

select * from1,2 where1.字段=2.字段
此方式会产生笛卡尔积,生成的记录总数=1的总数*2的总数会产生临时表

——例1:查询学生信息及学生的成绩

select * from student,scores where student.studentno=scores.studentno

方式二,内连接

不会产生笛卡尔积,不会产生临时表,性能高

select * from student 
inner join scores on student.studentno=scores.studentno

猜你喜欢

转载自blog.csdn.net/qq_50377269/article/details/132304198
今日推荐