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.字段。-- 如果字段值可以相互匹配上就可以查询出来。selectc.*,o.*fromcustomer_info cinner joinorder_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)select4 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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




