MySQL ——增删改查数据
2025-06-24 12:11:53
来源:新华网
一、DML语言
1.1.1 Insert插入数据
语法:insert [into] 表名 [字段名] values(值列表);
插入一行数据
第一个:insert into file1(id,name,age) values (1,'aa',11);
第二种:insert into file1 values(1,'aa',11);
插入多行数据。
insert into file1(id,name,age) values(1,'aa',11), (2,'bb',12、(3,'cc',13);
注意:为了避免表面结构发生变化而导致错误,建议在插入数据时注明具体的字段名!
1.2 update 更新数据
语法:UPDATE 表名 SET 字段1=值1,字段2=值2,...字段n=值n [WHERE 条件]; 。
-- 示例:将id = age改为14update file1 set age = 14 where id =1;
注意:1、更新多列数据,使用逗号分开;2、限制添加条件。
1.3 delete 删除数据
语法格式:delete from 表名 [where条件];
-- 示例:删除file1中的数据[表仍在]delete from file1;
-- 示例:删除id=1数据deletetetetet的数据 from file1 where id =1;
1.4 truncate 删除数据
语法:truncate table 表名;
-- 示例:删除学生表中的数据[清空数据,表还在】truncate table students;
-- 示例:删除id是1的学生数据[错误报告,不能添加条件]truncate table students where id = 1;
二、DQL语言
DQL是Data Qurey Language英文缩写#xff0c;数据查询语言
常用语句:
基本查询句子、条件查询、多条件查询、模糊查询、分组查询、连接查询、子查询。
关键字:
AS、运算符、IN、BETWEEN AND、DISTINCT、ORDER BY、LIMIT。
条件:
模糊查询。 | like “字符”。 |
关键字查询[具体值]。 | in。 |
查询关键字[范围]。 | between 值1 and 值2. |
关键字查询【去重】。 | distinct。 |
查询关键字[顺序]。 | order by 排列列名 [asc升序,desc降序。 |
根据行数查询。 | limit。 |
2.1 select基本查询句子
语法:select 列名 from 表名;
示例:查询所有学生信息。
select *from students;
例题:查询所有学生的姓名,性别。
select name,sexfrom students;
2.2 运算符。
算术运算符。 | + - * / %。 |
关系运算符。 | > < = <> >= <= != |
赋值运算符 | = |
逻辑操作符。 | and or not (&& || !)。 |
2.3 where条件查询。
语法:select 列名 from 表名 where 条件。
示例:查询id为1学生姓名,性别。
select name,sexfrom studentswhere id =1;
例题:查询学生'邓超'的信息。
select *from studentswhere name = '邓超';
2.4 多条件查询where。
语法:select 列名 from 表名 where 条件 运算符 条件。
示例:查询id=或id=学生姓名,性别、。
select name,sexfrom studentswhere id = 1 or id = 3;
例题:查询2班学生性别为女性的信息。
select *from studentswhere sex = '女' and cls_id = 2;
2.5 like 模糊查询。
使用SQL模糊查询 通配符取代一个或多个字符的条件查询。
语法:select 列名 from 表名 where 字段。 like。'值'
例题:查询名称包含'小'学生信息。
select * from students where name like '小%';
2.6 in关键字查询 是什么?
语法:select 列名 from 表名 where 字段。in。(值1,值2,……);
示例:学生信息为1、5、6、10,查询id。
select *from studentswhere id in (1,5,6,10);
not in 不是什么。
示例:查询id不是1、5、6、10学生的信息。
select *from studentswhere id not in(1,5,6,10);
2.7 查询between关键字查询 在什么内部。
语法:select 列名 from 表名 where 字段。between。值1。 and。值2;
示例:查询8-10名学生的ID信息。
select *from studentswhere id between 8 and 10;
2.8 distinct 查询关键字[去重]。
语法: select。 distinct。字段名1字段名2,…… from 手表名称;
示例:查询性别有几种分类。
select distinct sex from students;
示例:查询几个班级。
select distinct cls_id from students;
2.9 order by关键字查询。
语法:select 字段名列表 from 表名 [where 查询条件] [。order by。排序列名][asc(升序) 或 desc(降序)。
示例:按升序排列学生的身高。
select * from studentsorder by height; -- 默认为升序。
示例:按降序排列学生的身高。
select *from studentsorder by height desc;
2.10 limit关键字查询。
语法:
select 字段名列表。
from 表名。
[where 查询条件]。
[order by 排序的列名 asc(升序) 或 desc(降序)] 。
[。LIMIT。<开始行数需要检查的行数> ];
若开始行数不写默认为0。
示例:只看前两条学生信息。
select *from studentslimit 2;
示例:查看从第四行到第七行的学生信息。
select *from studentslimit 4,3;
三、连接查询。
连接查询是根据指定条件连接多张表中记录的查询方法。
注意:连接查询涉及两个以上表格,查询时至少要有必要的连接条件,这个必要条件是两个表共有的字段相等,而且这个字段必须在一个表中,主键,外健在另一个表里。
3.1 内连接。
内部连接是返回连接表中符合连接条件记录的连接查询。
包括:显式内连接,隐式内连接。
3.1.1 显示连接。
语法格式-xff1a;
select 字段 from 表1。 inner join。表2。 on。连接条件[where 条件]。
示例:检查学生的班级。
select s.name as '学生姓名',c.name as '班名' -- 可以省略frommas students s inner join classes con s.cls_id = c.id;
提问:检查学生所在的班级,班级为1。
select s.name '学生姓名',c.name '教室名'from students s inner join classes con s.cls_id = c.id and c.id = 1;
3.1.2 隐式内连接查询。
select 字段 from 表1,表2 where 表1.条件 = 表2.条件。
示例:检查学生的班级。
select s.name, c.idfrom students s, classes cwhere s.cls_id = c.id;
3.2 外连接。
3.2.1 左外连接查询。
左外连接基于左表,返回左表中所有记录和连接表中合格记录的外部连接。
select 字段 from 表1。left join。表2。on。连接条件 [where 条件]。
示例:检查老师的班级。
select t.name '教师姓名' , c.id '班级名'from teachers t left join classes con t.id = c.teacher_id;
3.2.1 查询右外连接。
右外连接基于右表,返回右表中所有记录和连接表中合格记录的外部连接。
语法:select 字段 from 表1 right join 表2 on 连接条件 where 条件。
select t.name '教室名称',c.id '班名'from teachers t right join classes con t.id = c.teacher_id;
3.3 聚合函数。
如何查看班级学生的平均身高?——使用聚合函数。
聚合函数可以计算一组值,并返回单个值的函数。
语法:select 聚合函数<字段> from 表名 [where 条件][group by 聚合函数] 。
count()。 | 计数。 |
sum()。 | 求和。 |
max()。 | 最大值。 |
min()。 | 最小值。 |
avg()。 | 平均值。 |
示例:询问班上学生的平均身高。
select avg(height)from students;
示例:询问班上有多少学生。
select count(*) -- idfromm也可以在这里使用 students。
3.4 子查询[查询嵌套]。
如何只查询同一班比刘德华高的学生信息?f;——子查询。
定义:子查询是包括另一种查询方法在一个查询内部的查询方法。
3.4.1 简单子查询。
示例:查看刘德华同学所在班的所有同学。
select namefrom studentswhere cls_id = (select cls_id from students where name ='刘德华');
3.4.2 any/some子查询。
示例:查看赵老师带来的学生信息。
select *from studentswhere cls_id = any(select id from classes where teacher = select id from teachers where name = '赵老师'));
3.5 alll查询。
ALL。
子查询的关键在于比较所有子查询的返回值。例如,当使用。> ALL。
时,主查询中的表达式必须大于子查询返回的所有值,只有这样才能满足条件。
示例:检查学生的班级。
select *from studentswhere cls_id >=all(select id from classes where teacher_id = (select id from teachers where name = '赵老师'));
3.6 exists子查询。
示例:删除表。
drop table if exists file1;
示例:查看王老师的班级表。
select *from classes where exists (select * from teachers where name='王老师');
3.7 not exists子查询。
示例:创建教师表。
create table IF NOT EXISTS teachers( id int primary key, name varchar(20));
避免重复创建。