mysql dml平台_MySQL5.7之DML和DQL的应用演示

大家好,我是anyux。上一节简单介绍了SQL语句的DDL(数据定义语言)、DCL(数据控制语言)。本节是对于SQL语句DML(数据操作语言)、DQL(数据查询语言)进入演示操作。

DML的应用

数据操作语言DML主要有三种形式:

1.插入:INSERT

2.更新:UPDATE

3.删除:DELETE

以下操作基于已经创建的zabbix数据库及库下的stu表,请先准备。也可联系作者获取相应sql文件

#-----偷懒插入数据方法 insert  `zabbix`.`stu` values(1,'zs',18,'m',now());

#-----最规范插入数据方法

insert into `zabbix`.`stu`(id,name,age,sex,intime) values(2,'ls',19,'m',now());

#-----针对性录入数据

insert into `zabbix`.`stu`(name,age,sex) values('ww',11,'m');

#-----一次性录入多行数据

insert into `zabbix`.`stu`(name,age,sex) values('aa',11,'m'),('bb',12,'f'),('cc',13,'m');

update

#-----更新操作

update `zabbix`.`stu` set name='zhangfeifei' where id=10;

delete

#-----删除操作

insert into `zabbix`.`stu` (name,age,sex) values('dd',20,'f');

select * from `zabbix`.`stu`;

delete from `zabbix`.`stu` where name='dd' and age =20 and sex='f';

7796525f3f2b500fafb9a6433edf40a4.png

业务数据应用

delete 和truncate删除区别

delete:DML操作,是逻辑性质删除,逐行删除,速度慢

truncate:DDL操作,将表中的数据页进行清空,速度快

#----- 逻辑删除

delete from `zabbix`.`stu`;

#----- 数据清空

truncate table `zabbix`.`stu`;

数据伪删除

一般地,使用update替代delete,最终保证业务查找不到

#-----增加数据列

alter table `zabbix`.`stu` add state tinyint not null default 1;

#-----查看数据表状态

select * from `zabbix`.`stu` where state=1;

#-----使用update替换delete

update `zabbix`.`stu` set state=0 where id=6;

#-----查看数据表状态

select * from `zabbix`.`stu` where state=1;

63742872df69a2bcb172b6ad2c71f9fe.png

DQL的应用

select语法简单应用

查看当前数据库端口select @@port;+--------+| @@port |+--------+| 3306 |+--------+查看当前数据库应用程序目录select @@basedir;+---------------------+| @@basedir |+---------------------+| /application/mysql/ |查看当前数据库应用程序服务idselect @@server_id;+-------------+| @@server_id |+-------------+| 6 |+-------------+控制MySQL 磁盘写入策略select @@innodb_flush_log_at_trx_commit;+----------------------------------+| @@innodb_flush_log_at_trx_commit |+----------------------------------+| 1 |+----------------------------------+查看MySQL应该支持的sql语法,对数据的校验等等select @@sql_mode;+-------------------------------------------------------------------------------------------------------------------------------------------+| @@sql_mode |+-------------------------------------------------------------------------------------------------------------------------------------------+| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |+-------------------------------------------------------------------------------------------------------------------------------------------+

其实数据库中自带了很多的内置函数,比如查看当前所在数据库,相当于Linux的pwd命令

-----查看当前数据库

select database(); -----查看当前时间

select now();

以上select查询都简单的使用,可以被其他工具替代。下面是select语句主要的使用场景

select通用语法(单表)

所谓select通用语法是指,select语法通过配合其他查询子句。如select .... from

语法构成

select 列

from 表

where 条件

group by 条件

order by 条件

limit x,y

环境说明

此处需要一个world.sql文件,导入到数据库中,需要文件的同学,可以联系作者

select配合from子句应用

查询表中所有的信息(生产中几乎是没有这种需求的)

use world;

select id,name,countrycode,district,population from city;

ee6eba5b34b7bcbc9385a672fcd3a9cb.png

#-----简便写法

select * from city;

#-----查询表中 name 和 population的值

select name,population from city;

1948fae3cb8943c1279214fc4069959a.png

select配合where子句应用

查询中国所有的城市名称和人口数

#-----select 列,列,列 from 表 where 过滤条件

select name,population from city where countrycode='CHN';

4dda27a5cf6b8320a786e09c42ba541a.png

世界上小于100人的城市

#-----where配合比较判断查询(> < >= <=)

select name,population from city where population<100;

06b7f7e5e8f9b63c72e155fd88659216.png

查询中国人口数量大于10万的城市和人口

#-----where配合逻辑连接符(and or not)

select name,population from city where countrycode='CHN' and population>100000;

f86ba5a425fe6315a96ea2c9e8d2bedc.png

查询中国或美国的城市名和人口数

select name,population from city where countrycode='CHN' or countrycode="USA";

03bcf70f45fc57fd2e64ab407041cf40.png

查询人口数量在50万到60万之间的城市名和人品数

#-----查询人口数量在50万到60万之间的城市名和人品数

select name,population from city where population > 500000 and population

select name,population from city where population between 500000 and 600000;

b554e91317ca8d138477e3f371e398c7.png

where配合like语句,实现模糊查询

#-----where配合like语句,实现模糊查询

#-----查询contrycode中有C开头的城市信息

#-----注意,不要出现%C%这种情况,这样做不走索引,功能上可以实现,但对性能影响巨大。如果业务中有大量模糊查询的需求,可以使用ES来替代

select * from city where countrycode like 'C%';

查询中国或美国的城市信息

#-----where配合in语句

select name,population from city where countrycode='CHN' or countrycode='USA';

select name,population from city where countrycode in ('CHN',"USA")

本节主要是对于SQL语句的DML(数据操作语言)和DQL(数据查询语言)进行演示。其中如有不足还各位同学指出。下一节将对DQL的select操作进行更多深入的演示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值