1. 数据库 C【create】R【retrieve】U【update】D【delete】 语句(CRUD-增删改查)
- insert 语句 (添加数据)
- update 语句 (更新数据)
- delete 语句 (删除数据)
- 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 语句注意事项
- 插入的数据应与字段的数据类型相同。比如 把 ‘abc’添加到 int 类型会错误
- 数据的长度应在列的规定范围内,例如:不能将一个长度为80的字符串加入到长度为40的列中。
- 在 values 中列出的数据位置必须与被加入的列的排列位置相对应。
- 字符和日期型数据应包含在单引号中。
- 列可以插入空值【前提是该字段允许为空】,insert into table value(null)
- insert into tab_name (列名…) values(),(),() 形式添加多条记录
- 如果是给表中的所有字段添加数据,可以不写前面的字段名称
- 默认值的使用,当不给某个字段值时,如果有默认值就会添加默认值,否则报错。【如果某个列 没有指定 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 语句使用细节
- update 语法可以用新值更新原有表行中的各列。
- set子句指示要修改哪些列和要给予哪些值。
- where子句指定应更新哪些行。如没有 where 子句,则更新所有的行(记录),所以要小心使用
- 如果需要修改多个字段,可以通过 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语句使用细节
- 如果不使用 where 子句,将删除表中所有数据。
- Delete 语句不能删除某一列的值(可使用update 设为 null 或者 ‘’)
- 使用 delete 语句仅删除记录,不删除表本身。如要删除表,使用 drop table 语句。 drop table 表名。
5. select
5.1 select 语句 基本语法
- select 指定查询哪些列的数据
- column指定列名
- *号代表查询所有列
- from指定查询哪张表
- 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 子句排序查询结果
- Order by 指定排序的列,排序的列既可以是表中的列名,也可以是 select 语句后指定的列名。
- Asc 升序【默认】、Desc 降序
- 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; ```