01
—
什么是DML
DML是Data Manipulation Language的缩写。是指对数据进行操作的语言。我们通常操作数据使用的指令集合就叫DML语法。一般包含:select、insert、update、delete 这些语法。在工作中如果能够灵活并且熟练的使用这些语法。也可以算作一个Sql大神了。不要小看这些语法,工作中通常会有组合的使用情况。Sql是进入大数据的必备技能。
02
—
DML操作实战
1、select语法:查询表数据使用,可以同时关联多种表进行查询。但是需要弄清楚表中数据存储的关系。如:1对1、1对多、多对多的数据关系。
举例说明:一张客户信息表、一张订单信息表、一张产品表、一张实名表
1对1的情况: 客户与实名表,是一对一的关系
1对多的情况:客户与订单,是一对多的关系
多对多的情况:产品与订单,是多对多的关系。理解这些关系,便于我们可以灵活的利用select查找出需要的数据。
-- 为select准备测试表和数据
-- 创建客户表(测试表)
create table customer_info
(
cut_id int comment '客户id',
cut_name varchar(2) comment '客户姓名'
);
-- 插入数据(测试数据)
insert into customer_info(cut_id,cut_name) values(1,'张三');
insert into customer_info(cut_id,cut_name) values(2,'李四');
-- 创建客户订单表(测试表)
create table order_info
(
cut_id int comment '客户ID',
order_number varchar(20) comment '订单编号',
order_time datetime comment '订单时间'
);
-- 插入数据(测试数据)
insert into order_info(cut_id,order_number,order_time) values(1,'NO001','2020-09-29');
insert into order_info(cut_id,order_number,order_time) values(1,'NO001','2020-10-01');
复制
select * from customer_info ; -- 查询所有客户数据
复制
-- and表示必须同时满足条件
select * from customer_info where cut_id = 1 and cut_name ='张三';
复制
-- or表示满足其中任意一个条件
select * from customer_info where cut_id = 1 or cut_name ='李四';
复制
-- in表示包含在集合中的所有数据
select * from customer_info where cut_id in (1,2);
复制
-- not in表示不做集合中的数据。这里查询不包含“1”的数据
select * from customer_info where cut_id not in (1);
复制
-- like表示模糊配置数据,"%"百分号表示任意值。这里表示“张”前面和后面可以是任意值。
select * from customer_info where cut_name like '%张%';
复制
-- innr join 关联查询,c 和 o 表示给表取个简短的别名, on 后面是相互匹配的字段。
-- inner join简单点可以理解为:表1 inner join 表2 on 表1.字段 = 表2.字段。
-- 如果字段值可以相互匹配上就可以查询出来。
select
c.*,
o.*
from
customer_info c
inner join
order_info o on c.cut_id = o.cut_id
复制
-- insert table values: 向客户表添加一个用户(前提是表是存在的)
insert into customer_info(cut_id,cut_name) values(3,'王五');
复制
-- insert table select: 查询结果添加到客户表(前提字段个数能匹配上)
insert into customer_info(cut_id,cut_name)
select
4 as cut_id,
'赵六' as cut_name;-- 可以单独执行下select,就知道添加的内容了
复制
-- update 表 set 字段='修改的值'。查询出id=4的修改名称为'测试'
update customer_info set cut_name='测试' where cut_id = 4;
复制
-- 语法:delete from 表 where。如果不写where,将删除所有数据(很危险)
delete from customer_info where cut_id = 4;
复制
03
—
小结
图片截自www.runoob.com
如果大家喜欢可关注公众号,感谢!
文章转载自数据在此,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
508次阅读
2025-04-25 18:53:11
墨天轮个人数说知识点合集
JiekeXu
446次阅读
2025-04-01 15:56:03
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
426次阅读
2025-04-25 15:30:58
MySQL数据库当前和历史事务分析
听见风的声音
426次阅读
2025-04-01 08:47:17
MySQL 生产实践-Update 二级索引导致的性能问题排查
chengang
390次阅读
2025-03-28 16:28:31
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
365次阅读
2025-04-17 17:02:24
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
357次阅读
2025-04-15 23:49:58
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
332次阅读
2025-04-15 14:48:05
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
304次阅读
2025-04-07 12:14:29
记录MySQL数据库的一些奇怪的迁移需求!
陈举超
201次阅读
2025-04-15 15:27:53