SQL语句(数据更新、查询操作)

数据库表操作

  • 创建数据库语法格式

    create table 表名(
    	字段名1 类型 约束,
        字段名2 类型 约束,
        ..... ..... 
    )
    
  • 创建学生表,字段要求如下:

    • 姓名(长度为10)、年龄、身高(保留2位小数)
    create table students(
        id int unsigned primary key auto_increment, -- id字段为主键,且值自增
        name varchar(20), -- name字段类型为字符串,长度为20
        age int unsigned, -- age 字段类型为无符号的整数
        height decimal(5,2) -- height字段类型为小数,小数位数为2    
    );
    

image-20240901202453172

  • 删除数据库表

    drop table students;
    drop table if exists students;
    
    -- 注释说明,不是sql语句,不会被执行
    -- 对语句的解释说明
    -- 创建students 表
    create table students(
        id int unsigned primary key auto_increment, 
        name varchar(20), 
        age int unsigned, 
        height decimal(5,2)
    );
    -- 删除数据集
    -- 注释的快捷键 ctrl 加 /
    drop table students;
    drop table if exists students;
    

数据操作

简单查询

select * from 表名;
select * from students;

添加一行数据

-- 主键自增长,可以用0或null代替
insert into 表名 values(...)
insert into students values(0,'亚瑟',22,177.56);

insert into 表名(字段1,字段2...)values(1,值2...)
insert into students(name) values('老夫子');
  • 练习
-- 姓名(长度为10)、班级(长度为8)、科目(长度为56)、成绩(保留一位小数)
create table scores(
name varchar(10),
class varchar(8),
subject varchar(56),
score decimal(4,1)
);

-- 将刚才创建的成绩表删除
drop table scores;

-- 全部
insert into scores values('mike','24期','软测','59');

-- 部分
insert into scores(name) values("tom");

--查询
select * from scores;

数据更新操作

添加多行数据

-- 方式一:写多条insert语句,多条语句之间用英文分号分隔
insert into students(id,name)values(0,'张三');
insert into students(id,name)values(0,'李四');
insert into students values(0,'李四',20,180);

-- 方式二:通过一条insert 语句插入多条数据,数据间用逗号分隔
-- insert into 表名 values(...),(...)...
insert into students values(0,'亚瑟3',23,167.56),(0,'亚瑟4',23,167.56);

-- insert into 表名(字段名1,字段名2...) values(字段名1,字段名2...),(字段名1,字段名2...)...
insert into students(id,name) values(0,'小黑'),(0,'小蓝'),(null,'大白');

修改数据

-- update 表名 set 字段名1=值1,字段名2=值2,...where 条件
-- 修改id为5的数据,将名称设为狄仁杰,年龄设为22
UPDATE students set name='狄仁杰',age=22 where id =5;

注意:修改数据时一定要加条件,否则修改的是所有记录的值

删除数据

-- delete from 表名 where条件(物理删除对应的数据)
delete from students where id=6;
  • 注意

    • 此方法为物理删除,工作中大部分使用逻辑删除
    • 逻辑删除是指通过设定一个字段来标识当前记录已经删除
    • is_delete字段来标识,1代表删除,0表示未删除

    image-20240901210114770

其他删除数据的方式:

-- truncate table 表名(清除表里面的数据,但是表结构会保留,自增长字段的值会从1开始)
truncate table students;

--drop table 表名(删除数据表,包括数据和表结构)
drop table students;

image-20240901210836384

数据库查询操作

数据准备

drop table if EXISTS students;

create table students(
studentNo VARCHAR(10) PRIMARY KEY,
name VARCHAR(10),
sex VARCHAR(1),
hometown VARCHAR(20),
age tinyint(4),
class VARCHAR(10),
card VARCHAR(20)
);

insert into students VALUES
('001','王昭君','女','北京','20','1班','340322199001247654'),
('002','诸葛亮','男','上海','18','2班','340322199102247125'),
('003','张飞','男','南京','24','3班','340322199201237547'),
('004','白起','男','安徽','22','4班','340322199301247584'),
('005','大乔','女','天津','19','3班','340322199401257659'),
('006','孙尚香','女','河北','18','1班','340322199501267344'),
('007','百里玄策','男','山西','20','2班','340322199607247884'),
('008','小乔','女','河南','15','3班',null),
('009','百里守约','男','湖南','21','1班','');

image-20240901212408281

查询的基本操作

  • 查询部分字段的值
-- select 字段1,字段2...from 表名(查询的为一部分的字段的信息)
-- 查询学生表中的姓名、性别、年龄的数据
select name,sex,age from students;
  • 取别名
-- 给表取别名
-- select 别名.字段名1,别名.字段名2...from 表名 as 别名
-- 这个语法为后面关联查询做准备,现在只需要知道这个语法即可
select s.name,s.sex,s.age from students as s;

-- 给字段取别名
-- select 字段名1 as别名1,字段名2 as别名2...from 表名;
select name as 姓名,sex as 性别,age as 年龄 from students;
  • 去重
-- select distinct 字段1,字段2...from 表名;
-- 根据学生的性别去重
select distinct sex from students;

条件查询

  • 语法格式
select 字段1,字段2...from 表名 where 条件
select * from students where id=5

条件查询:比较运算符

  • 比较运算符:大于(>)、等于(=)、小于(<)、大于等于(>=)、小于等于(<=)、不等于(<>或者!=)
-- 例1:查询小乔的年龄
select age from students where  name='小乔';
-- 例2:查询20岁以下的学生
select * from students where age<20;
-- 例3:查询家乡不在北京的学生 !=之间不能有空格
select * from students where hometown !='北京';

-- 1、查询学号是'007'的学生身份证号
select card from students where studentNo='007';
-- 2、查询1班以外的学生信息
select * from students where class <> '1班';
-- 3、查询年龄大于20的学生姓名和性别
select name,sex from students where age>20;

条件查询:逻辑运算符

  • 逻辑运算符:and(且,同时符合对应的条件),or(或,符合其中的一个条件),not(非,不符合该条件)
-- 查询年龄等于18的女生的记录(and)
select * from students where age=18 and sex='女'; 
-- 查询出女学生或者是1班的学生
select * from students where sex='女' or class='1班';
-- 查询非天津学生的记录
select * from students where hometown !='天津';
select * from students where not hometown ='天津';

-- 1、查询河南或河北的学生
select * from students where   hometown ='河南'or  hometown ='河北';
-- 2、查询2班的上海学生
select * from students where class='2班' and hometown ='上海'
-- 3、查询非20岁的学生
select * from students where not age =20;
select * from students where  age !=20;

条件查询:模糊查询

  • 模糊查询:like关键字

    • %:匹配任意个字符
    • _:匹配任意单个字符
    • 一般like关键字只用来匹配字段类型为字符串的
    -- 例1、查询姓'孙'的学生
    select * from students where name like '孙%'
    -- 例2、查询姓'孙'且名字是一个字的学生
    select * from students where name like '孙_';
    -- 例3、查询名字以'乔'结尾的学生
    select * from students where name like '%乔';
    -- 例4、查询姓名中包含'白'的学生
    select * from students where name like '%白%';
    
    -- 1、查询姓名为两个字的学生
    select * from students where name like '__';
    -- 2、查询姓'百'且年龄大于20的学生
    select * from students where name like '百%' and age>20;
    -- 3、查询学号以1结尾的学生
    select * from students where studentNo like '%1';
    

条件查询:范围查询

  • in :查询非连续范围内的数据
-- 查询家乡为北京或上海或广东的学生
select * from students where hometown in ('北京','上海','广东');

-- 相同实现方式
select * from students where hometown='北京' or hometown='上海' or  hometown='广东';
  • between…and:查询连续范围内的数据(用在数值型字段中)
-- 查询年龄在18到20之间的学生
select * from  students where age between 18 and 20;
-- 相同实现方式
select * from  students where age >=18 and age<=20;

-- 1、查询年龄为18或19或22的女生
select * from  students where age in (18,19,20) and sex='女'
-- 2、查询年龄在20到25以外的学生
select * from  students where age not between 18 and 25;

条件查询:为空判断

  • null和 ’ '字符串不一样
  • 空判断 is null
-- 查询出身份证号为空的信息
select * from  students where card is null;
  • 非空判断:is not null
-- 查询出身份证号不为空的信息
select * from  students where card is not  null;

排序

  • 语法格式:
select * from 表名 order by 字段1 asc|desc,字段2 asc|desc...
  • 字段的排序规则默认为升序排序(从小到大)
  • asc:表示从小到大排序(升序)
  • desc:表示从大到小排序(降序)
-- 例1:查询所有学生信息,按年龄从小到大排序
select * from students ORDER BY age;
-- 例2:查询所有学生信息,年龄按从大到小排序,年龄相同时,再按学号从小到大排序
select * from students ORDER BY age desc,studentNo;
-- 例3:查询所有学生信息,班级按从大到小排序,班级相同时,再按学号从小到大排序
select * from students ORDER BY class desc,studentNo;

分组和聚合

聚合函数介绍

  • 使用聚合函数方便进行数据统计
  • 聚合函数不能在where中使用

image-20240902091756708

聚合函数案例

  • count(*):求表的总的记录数
-- 查询学生表的总记录数
select count(*) from students;
  • max(字段名):查询对应字段的最大的值
-- 查询女生的最大年龄
select max(age) from students where sex='女';
  • min(字段名):查询对应字段的最小的值
-- 查询1班的最小年龄
select min(age) from students where class='1班';
  • sum(字段名):查询对应字段的值的总和
-- 查询北京学生的年龄总和
select sum(age) from students where hometown='北京';
  • avg(字段名):查询对应字段的值的平均数
-- 查询女生的平均年龄
select avg(age) from students where sex='女';
-- 1、查询所有学生的最大年龄、最小年龄、平均年龄
select max(age),min(age),avg(age) from students;
-- 2、一班共有多少个学生
select count(*) from students where class='1班';
-- 3、查询3班年龄小于18岁的同学有几个
select count(*) from students where class='3班' and age<18;

分组查询

  • 分组的目的是对每一组的数据进行统计(使用聚合函数)

    • 语法格式

      select 字段1,字段2,聚合函数...from 表名 group by 字段1,字段2...
      
    • 例子

      -- 查询各种性别的人数
      select sex,count(*) from students group by sex;
      -- 查询各种性别年龄最大的
      select sex,max(age) from students group by sex;
      -- 查询各个班级的人数
      select class,count(*) from students group by class;
      -- 查询各个班级中不同性别的人数
      select class,sex,count(*)  from students group by class,sex;
      
  • 分组后的数据筛选:

    • 将分组之后的数据当成是一个表数据,然后再通过having条件来对当前的表数据进行筛选。

    • 语法格式

      select 字段1,字段2,聚合函数...from 表名 group by 字段1,字段2...
      having 条件
      
      • having条件 运算符跟where条件运算符是一样的
      • having后面可以使用聚合函数
    • 例子

      -- 查询男生总人数
      select count(*) from students where sex='男'
      select sex,count(*) from students GROUP BY sex HAVING sex='男'
      
      -- 查询每个班级男生的总记录数 
      select class,sex,count(*) from students GROUP BY class,sex having sex='男'
      -- 查询所有班级中不同性别的记录数大于1的信息
      select class,sex,count(*) from students GROUP BY class,sex HAVING count(*)>1
      

      1.where 是对from后面指定的表进行数据筛选,属于对原始数据的筛选

      2.having 是对group by的结果进行筛选

      3.having后面的条件中可以用聚合函数,where后面不可以

分页查询

  • 语法格式

    select * from limit start,count
    
    • start表示的是开始的记录,索引是从0开始。0表示第一条记录
    • count 表示的是从start开始,查询多少条记录
  • 例子

    -- 查询前3行的学生信息
    select * from students limit 0,3;
    select * from students limit 3;
    
  • 分页查询实现

    select * from students limit (n-1)*m,m;
    
    • n表示的是页数
      • n=1,2,3,4
    • m表示的是每页显示的记录数
      • m=3
    • (n-1)*m,m是公式,并不是语法格式,不能直接写在SQL中,需要替换为具体的数字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值