✨:MYSQL入门阶段二
前言
提供给正在学习的伙伴们
背景:随着大数据时代的到来,很多时候都需要我们对数据有更多的收集和处理方法。而MYSQL能够满足我们大多数时候针对海量数据的处理,打破了仅会excel来处理数据的局限性
提示:以下是本篇文章正文内容,健康“食品”,放心食用
一、DML操作表数据
主要是对表中的数据进行增删改查
1.1 插入数据
insert into 表名 (字段)values (字段值)
#方式一
insert into test1(id, name) values(03, '张三');
#方式二
insert into test1 values(03,'张三','2022/05/19');
#方式三
insert into test1 (name) values('李四');
注意:
1、值与字段必须要对应,个数相同&数据类型相同
2、值的数据大小,必须在字段指定的长度范围内
3、varchar,char,date 类型的值必须使用单引号包裹。
4、如果要插入空值,可以忽略不写,或者插入null
5、如果插入指定字段的值,必须要上写列名
1.2 更改数据
update 表名 set 列名 = 值
update 表名 set 列名 = 值 [where 条件表达式:字段名 = 值 ]
# 把 test1 表的 name 字段全部更改为 王五
update test1 set name = '王五';
# 把 id 为 01 的 name 字段更改为 王五
update test1 set name = '王五' where id = 01;
# 把 id 为 02 的 name 字段更改为 王五弟,tdate 字段更改为 2022-01-03
update test1 set name = '王五弟', tdate = '2022-01-03' where id = 02;
1.3 删除数据
Delete 删除表:
delete from 表名
delete from 表名 [where 字段名 = 值]
delete from test2;
delete from test2 where id = 03;
Truncate 删除表:
truncate table 表名
truncate table test2;
注意:
- delete 删除表; 不推荐. 有多少条记录 就执行多少次删除操作. 效率低
- truncate删除表: 推荐. 先删除整张表, 然后再重新创建一张一模一样的表. 效率高
当一个表中条数非常多,又要做删除动作的时候,如果用delete会很慢,那我们可以使用truncate
二、DQL查询语言
2.1 简单查询
创建一个 test_emp表
create table test_emp
(
tid int,
tname varchar(32),
tdate date,
tsex varchar(32),
salary decimal(5,2)
);
查询MYSQL的执行数据
form > where > group by > having > select > order by
简单的查询:
1、查询表中所有数据
select * from test_emp;
2、查询部分数据:tid, tname
select tid, tname from test_emp;
3、查询后另起别名
select
tid as 'ID',
tname as '姓名'
from test_emp;
4、查询共有多少人(按照ID进行去重)
select distinct tid as 'ID' from test_emp;
5、查询平均薪资
select avg(tsalary) from test_emp;
2.1 条件查询
目的:在实际应用中,为了获取想要的数据,就需要对数据进行条件过滤
select 列名 from 表名 where 条件表达式
运算符:
运算符 | 说明 |
---|---|
>, < ,<=, >=, = | 大于、小于、小于(大于)等于、等于 |
<>, != | 不等于 |
BETWEEN … A … AND … B … | 显示在A与B之间的值 |
IN(集合) | in(1,2,3 …)在集合中的值 |
LIKE '%五% | 模糊查询,含有‘五’的数据 |
IS NULL | 查询为 null的值 |
And, && | 同时成立的条件 |
or ,I I | 成立一个条件即可 |
Not | 都不成立 |
1、查询 salary 小于9000的数据
2、查询 不含有 ‘张’ 的数据
3、查询 tid 为 null 的数据
4、查询时间在 2022-01-02 到 2022-03-15 的数据
1、
select * from test_enp where salary < 9000;
2、
select * from test_emp where tname not like'%张%';
3、
select * from test_emp where tid is null;
4、
select * from test_emp where tdate betweed '2022-01-02' and '2022-03-15';
总结
仅仅只是小白的一个学习过程,也望能够帮助大家。会不定期更新~~~