mysql学习笔记二--数据库的增删改查

这篇博客详细介绍了MySQL数据库的增删改查(CRUD)操作,包括insert语句的注意事项、update语句的使用细节、delete语句的执行要求以及select语句的基础语法和排序查询。内容涵盖数据类型的匹配、默认值的设定、where子句的应用和order by子句的排序方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 数据库 C【create】R【retrieve】U【update】D【delete】 语句(CRUD-增删改查)

  1. insert 语句 (添加数据)
  2. update 语句 (更新数据)
  3. delete 语句 (删除数据)
  4. select 语句 (查找数据)

2. insert

2.1 insert 语句

create table `goods` (
	`id` int,
    `goods_name` varchar(10),
    `price` double
);

SELECT * FROM jsr_bd03.goods;

-- 往goods表中添加两条记录 
insert into `goods` (id, goods_name, price) 
	values(1,'小金', 88);
    
insert into `goods` (id, goods_name, price) 
	values(2,'小林', 888);

2.2 insert 语句注意事项

  1. 插入的数据应与字段的数据类型相同。比如 把 ‘abc’添加到 int 类型会错误
  2. 数据的长度应在列的规定范围内,例如:不能将一个长度为80的字符串加入到长度为40的列中。
  3. 在 values 中列出的数据位置必须与被加入的列的排列位置相对应。
  4. 字符和日期型数据应包含在单引号中。
  5. 列可以插入空值【前提是该字段允许为空】,insert into table value(null)
  6. insert into tab_name (列名…) values(),(),() 形式添加多条记录
  7. 如果是给表中的所有字段添加数据,可以不写前面的字段名称
  8. 默认值的使用,当不给某个字段值时,如果有默认值就会添加默认值,否则报错。【如果某个列 没有指定 not null,那么当添加数据时,没有给定值,则会默认给null】【如果我们希望指定某个列的默认值,可以在创建表时指定】在这里插入图片描述

3. update

3.1 update 语句

-- MySql运行在safe-updates模式下,该模式会导致非主键条件下无法执行update或者delete命令
show variables like 'SQL_SAFE_UPDATES';
-- 关闭safe-updates模式
set sql_safe_updates = 0;

-- 显示 employee 表的所有字段
desc employee;

-- 显示表中的数据
select * from employee;

-- 要求: 将所有员工工资修改为5000
update employee
	set Salary = 5000;
    
-- 要求: 将 user_name 为 jsr的员工工资修改为5000
update employee
	set Salary = 8000
    where user_name = 'jsr';
    
-- 要求: 将 user_name 为 金角小金的员工工资 + 1000
update employee
	set Salary = Salary + 1000
    where user_name = '金角小金';
    
-- 打开safe-updates模式
set sql_safe_updates = 1;

3.2 update 语句使用细节

  1. update 语法可以用新值更新原有表行中的各列。
  2. set子句指示要修改哪些列和要给予哪些值。
  3. where子句指定应更新哪些行。如没有 where 子句,则更新所有的行(记录),所以要小心使用
  4. 如果需要修改多个字段,可以通过 set 字段1 = 值1,字段2 = 值2.。。。
//要求:在上面创建的employee表中修改表中的记录
//1.将所有员工的薪水修改为 5000 元
update employee set salary = 5000
//2、将姓名为 小妖怪的员工的薪水修改为 3000 元
update employee 
	set salary = 3000
	where user_name = '小妖怪'
//3、 将 老妖怪 的薪水在原有基础上增加 1000 元
update employee 
	set salary = salary + 1000
	where user_name = '老妖怪'

4. delete

4.1 delete语句

-- MySql运行在safe-updates模式下,该模式会导致非主键条件下无法执行update或者delete命令
show variables like 'SQL_SAFE_UPDATES';
-- 关闭safe-updates模式
set sql_safe_updates = 0;

select  * from employee;

-- 删除表中user_name = jsr 的记录
delete from employee
	where user_name = 'jsr';
    
-- 删除表中所有记录
delete from employee; 

4.2 delete语句使用细节

  1. 如果不使用 where 子句,将删除表中所有数据。
  2. Delete 语句不能删除某一列的值(可使用update 设为 null 或者 ‘’)
  3. 使用 delete 语句仅删除记录,不删除表本身。如要删除表,使用 drop table 语句。 drop table 表名

5. select

5.1 select 语句 基本语法

在这里插入图片描述

  1. select 指定查询哪些列的数据
  2. column指定列名
  3. *号代表查询所有列
  4. from指定查询哪张表
  5. distinc 可选,指显示结果时,是否去掉重复数据
-- 查询表中所有学生的信息。 
select * from student;

-- 查询表中所有的学生的姓名和对应的英语成绩
select `name`,english from student;

-- 过滤表中重复数据 distinct
select distinct english from student;

-- 要查询的记录,每个字段都相同,才会去重
select distinct `name`, english from student;  

使用表达式对查询的列进行运算

在这里插入图片描述

在 select 语句中可使用 as 语句

在这里插入图片描述

-- 统计每个学生的总分
 select `name`,(chinese + english + math) from student;
 
 -- 在所有学生的总分上加10分
select `name`,(chinese + english + math + 10) from student; 

 -- 使用别名表示学生分数 
select `name`, (chinese + english + math + 10)  as total_score from student; 

5.2 select 语句—在 where 子句中经常使用的运算符

between…and… 是闭区间,两个边界都包括
在这里插入图片描述

-- 查询姓名为 jsr1 的学生成绩
select  * from student 
	where `name` = 'jsr1';
	
-- 查询英语成绩大于 90 分的学生成绩
select  * from student 
	where `english` > 90;
	
-- 查询总分大于 200 分的所有同学 
select * from student
	where (chinese + english + math) > 200;
	
-- 查询 math 大于 60 并且 id 大于 3 的学生成绩
select * from student
	where  math > 60 and id > 3;
	
-- 查询英语成绩大于语文成绩的同学
select * from student
	where  english  > chinese;
	
-- 查询总分大于 200 分 并且 数学成绩小于语文成绩的名字以3结尾的同学。【模糊查询】    
select * from student
	where (chinese + english + math) > 200 and math < chinese and `name` like '%3';
	
-- 查询总分大于 200 分 并且 数学成绩小于语文成绩的名字以j开头的同学。    
select * from student
	where (chinese + english + math) > 20 and math < chinese and `name` like 'j%';

5.3 select 语句 – 使用 order by 子句排序查询结果

在这里插入图片描述

  1. Order by 指定排序的列,排序的列既可以是表中的列名,也可以是 select 语句后指定的列名。
  2. Asc 升序【默认】、Desc 降序
  3. order by 子句应位于 select 语句的结尾。
-- 演示 order by的使用 
-- 对数学成绩排序后输出【升序】 select * from student order by english asc;
-- 对总分按从高到低的顺序输出 select * from student order by (chinese + english + math) asc; select `name` , (chinese + english + math) as total_score
from student 	order by total_score desc;
-- 对名字以3结尾的学生成绩排序输出(升序)    select * from student where `name` like '%3' order by (chinese + english + math) asc; select `name` , (chinese
+ english + math) as total_score from student  	where `name` like '%3' 	order by total_score; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值