MySQL基础速成2——DML、DQL

http://t.csdnimg.cn/pArgXicon-default.png?t=N7T8http://t.csdnimg.cn/pArgX接上一篇 MySQL基础速成——DDL

目录

一.数据库操作语言DML

1.1.插入数据【增】

1.2更新数据【改】

 1.3.删除数据【删】

二.数据查询语言DQL

2.1.简单查询

2.2.比较查询

2.3.范围查询

2.4.逻辑查询

2.5.模糊查询

2.6.非空查询

2.7.排序查询

2.8.聚合查询


一.数据库操作语言DML

数据操作命令有:

1: 插入数据; insert
2: 删除数据; delete
3: 修改数据. update

在数据库操作语言中,DDL是对表和库进行操作,而DML是对表内数据进行操作。

 DML语法结构如下。

(1)插入(insert)数据,常见语法:

insert into 表名(字段名1,字段名2,字段名3,...) values(值1,值2,值3,...);

(2)修改(update)数据,常见语法:

update 表名 set 字段名1=值,字段名2=值,字段名3=值,... where 条件;

(3)删除(delete)数据,常见语法:

delete from 表名 where 条件;

通俗理解:对数据的操作,就类似于在现实中操作Excel表格中的数据。

1.1.插入数据【增】

插入数据有两种方式:

(1)插入一条数据

(2)插入多条数据

给数据表中插入一条数据,语法:

insert into 表名[(字段名1,字段名2,字段名3,...)] values(值1,值2,值3,...);

# 说明:

(1)当要给字符串类型的字段插入值时,要使用单引号把值引起来,否则显示出错;

(2)表名后字段个数与类型等,要与values后的值对应。

给数据表中插入多条数据,语法:

insert into 表名[(字段名1,字段名2,字段名3,...)] values (值1,值2,值3,...),(值1,值2,值3,...),...;

# 说明:

多条数据之间使用,(逗号)分隔。

例如,使用命令完成:

(1)创建一个班级db_shopping库,并在该库下新建一个用户表;

(2)用户表的字段信息有编号、人物名、性别、住址等;[不添加任何约束条件]

(3)使用插入数据的语法分别插入一条数据;

(4)思考1:给所有字段名一次性插入2条数据信息,该怎么做?

(5)思考2:给用户表一次性插入仅包含人物名、年龄、性别字段的3条数据,该怎么做?

(6)若发现在性别字段后,还缺少了年龄字段,该怎么处理呢?请在DataGrip软件中查看结果。

#####################插入数据###############################
# 1.新建库
create database if not exists db_shopping charset utf8;
# 2.使用库
use db_shopping;
show tables;
# 3.创建表
create table if not exists user(
    id int,
    name varchar(20),
    sex varchar(10),
    address varchar(255)
);
show tables;
# 4.不加()与加()依次插入数据
insert into user values(1,'李自豪','男','山东青岛');
insert into user(id,name,sex,address) values(2,'吕春伟','男','山东济南');
# 注意:加了()可以适当调整字段的顺序

# 5.一次性插入多条数据
insert into user values(3,'周媛','女','山西太原'),(4,'周钰哲','女','广东湛江');
insert into user(id,name,sex,address) values(5,'周媛2','女','山西太原'),(6,'周钰哲2','女','广东湛江');

# 6.一次插入仅包含部分字段的数据
desc user;
insert into user(name,sex,address) values('刘志远','男','河南新乡'),('刘志远2','男','河南新乡'),('刘志远3','男','河南新乡');

# 7.缺少年龄字段,该怎么修改?
alter table user add age int after sex;

1.2更新数据【改】

更新,也称为修改。

更新数据有两种方式:

(1)更新所有数据

(2)按条件修改数据

更新数据表内所有数据的语法:

update 表名 set 字段名1=值1, 字段名2=值2, 字段名3=值3,...;

# 说明:

如果不指定条件,更新数据时会把所有数据记录全部更新

按条件修改数据的语法:

update 表名 set 字段名1=值1, 字段名2=值2, 字段名3=值3,…… where 条件;

# 说明:

当条件的结果为True时,数据会被修改。

例如,使用命令完成:

(1)将用户表内所有用户性别一次性设置为男;

(2)将用户表内所有用户的性别修改为女、年龄修改为18;

(3)将用户表内编号为2的用户性别设置为男;

(4)将用户表内编号为3的用户年龄更新为30,且住址更新为湖北省武汉市;

(5)思考:去年存储的用户数据到今年了,那么存储在user数据表里的用户年龄都要增加1岁,该怎么处理呢?######################更新数据################################

# 使用库
use db_shopping;
show tables;
# 1
update user set sex='男';
# 2
update user set sex='女',age=18;
# 3
update user set sex='男' where id=2;
# 4
update user set age=30,address='湖北省武汉市' where id=3;
# 5
update user set age=age+1;

 1.3.删除数据【删】

删除数据有两种方式:

(1)删除所有数据

(2)按条件删除数据

删除所有数据的语法:

delete from 表名;

# 说明:

不加条件时,会删除所有数据内容

按条件删除数据的语法:

delete from 表名 where 条件;

# 说明:

当条件结果为True时,满足条件的数据会被删除

例如,使用命令完成:

(1)在数据库中班级db_shopping创建一个用户表2,表字段有编号、用户名、密码,再增加3条数据;

(2)删除表内编号为1的这条数据,再删除用户名为Andy的这条数据;

(3)使用命令删除表内所有数据,观察效果。

###################删除数据########################
# 使用库
use db_shopping;
# 创建表
create table if not exists user2(
    id int,
    username varchar(20),
    password varchar(20)
);
show tables;
# 新增数据
insert into
    user2(id,username,password)
values
    (1,'Andy','123456'),
    (2,'Amy','666777'),
    (3,'Marry','888999');

# 删除一条数据
delete from user2 where id=1;
delete from user2 where username='Andy';
# 清空所有数据
delete from user2;  # 方案1
# 扩展 -truncate清空数据
truncate table user2;  # 方案2
# 问题: 思考delete和truncate删除数据的区别? 查阅资料, 验证。

二.数据查询语言DQL

2.1.简单查询

简单查询有两种方式:

(1)查询所有数据

(2)按不同字段名来查询数据

简单查询数据,语法:

# 查询表内所有数据
select * from 表名;

# 查询表内数据,以指定的列来显示结果
select [distinct] 字段名1,字段名2,... from 表名;

例如,使用命令完成:

(1)在数据库班级db_product1中,查询商品表的所有数据信息;

(2)查询商品表的所有名称信息;

(3)查询商品名称、价格、分类的所有结果;

(4)将所有的商品名称、价格+20进行显示出结果;

(5)思考:如何去除重复的价格值并显示所有价格信息?

#########################简单查询#################################
# 使用库
use db_product1;
desc product;
# 1
select * from product;
# 2
# select * from product;
select pname from product;
# 3
# select * from product;
select pname,price,category_id from product;
# 4
# select * from product;
select pname,price from product;
select pname,price+20 from product;
# 取外号/别名
# ① 字段名 as 别名
select pname,price+20 as price2 from product;
# ② 字段名 别名
select pname,price+20 price2 from product;
# 5
# select * from product;
select price from product;
select distinct price from product;  # 推荐
# 扩展
select distinct(price) from product;

2.2.比较查询

对数据进行条件筛选处理,通用语法:

select [*|字段名1, 字段名2, ...] from 表名 where 条件;

where 条件中,使用比较运算符来查询结果。

比较运算符有:

例如,使用命令完成:

(1)在操作商品表时,查询商品表中的所有商品信息;

(2)查询商品名称为"花花公子"的商品所有信息;

(3)查询价格为800的商品信息;

(4)查询价格不是800的所有商品信息;

(5)查询商品价格大于60元的所有商品信息;

(6)查询商品价格小于等于800元的所有商品信息。

##########################比较查询############################
# 使用库
use db_product1;
# 1
select * from product;
# 2
# select * from product;
select * from product where pname = '花花公子';
# 3
select *
from
    product
where
    price = 800;
# 4
select * from product where price != 800;  # 推荐
select * from product where price <> 800;
# 5
select * from product where price > 60;
# 6
select * from product where price <= 800;  # 推荐     变量名 运算符 值
select * from product where 800 >= price;
# 结论: 使用where可以根据不同的条件来进行筛选数据结果。

2.3.范围查询

范围查询是指在某个范围内进行查询,分别有in和between...and...。

(A)in是用于非连续值的范围查询,语法:

select * from 表名 where 字段名 in (范围值1,范围值2,...);

(B)between and 是用于值在连续范围的查询,语法:

select * from 表名 where 字段名 between 范围值1 and 范围值2;

例如,使用命令完成:

(1)在操作商品表时,查询商品价格是200或800的所有商品信息;

(2)查询商品价格在200-1000之间所有商品信息;

(3)思考:有其他方法来完成查询商品价格在200-1000之间所有商品信息吗?

#########################范围查询###############################
# 使用库
use db_product1;
# 1
select * from product where price in (200,800);
# 2
select * from product where price between 200 and 1000;
select * from product where price between 200 and 800;   # 200 <= x <= 1000
# 3
# select * from product where 200 <= price <= 1000;   # 错误的  True   --Python
# 并且   逻辑运算符

2.4.逻辑查询

对数据进行条件筛选处理,通用语法:

select [*|字段名1, 字段名2, ...] from 表名 where 条件;

逻辑运算符有:

例如,使用命令完成:

(1)查询商品价格在200到1000之间所有商品信息;

(2)查询商品价格是200或800的所有商品信息;

(3)查询价格不是800的所有商品;

(4)思考:如果要查询不是200或800的所有商品信息,该怎么做呢?

####################逻辑查询##########################
# 使用库
use db_product1;
# 1
select * from product where price between 200 and 1000;
# 逻辑运算+比较运算
select * from product where price >= 200 and price <= 1000;
select * from product where 200 <= price and price <= 1000;   # 连贯/推荐
# 2
select * from product where price in (200,800);
select * from product where price = 200 or price = 800;
# 3
select * from product where price != 800;  # 正向
select * from product where price = 800;
select * from product where not (price = 800);   # 英文 + shift+9
# 4
select * from product where not (price = 200 or price = 800);   # 理解
# ?
select * from product where price != 200 and price != 800;
select * from product where not (price in (200,800));

2.5.模糊查询

模糊查询的语法:

select * from 表名 where 字段名 like '%某个字%'; 

select * from 表名 where 字段名 like '某个字_';

 例如,使用命令完成:

(1)查询商品名称含有"香"字的所有商品信息;

(2)查询商品名称为三个字的商品信息;

(3)查询商品名称以"斯"结尾,并且是三个字的商品信息;

(4)思考1:查询以"香"开头,且是三个字的商品信息;

(5)思考2:查询以"香"开头的所有商品信息。

######################模糊查询##################################
# 使用库
use db_product1;
# 1
select * from product where pname like '%香%';
# 2
select * from product where pname like '___';
# 3
select * from product where pname like '__斯';
# 4
select * from product where pname like '香__';
# 5
select * from product where pname like '香%';

2.6.非空查询

非空查询的语法:

select * from 表名 where 字段名 [条件]; 

非空运算符有:

例如,使用非空查询来完成:

(1)将商品名称为"香奈儿"的分类category_id修改为null;

(2)查询分类为空的商品信息;

(3)查询分类不为空的所有商品信息。

########################非空查询##############################
# 使用库
use db_product1;
# 1
update product set category_id=null where pname = '香奈儿';
select * from product;
# 2
# 错误
# select * from product where category_id = null;  # 错误
# 对
select * from product where category_id is null;
# 3
select * from product where category_id is not null;

2.7.排序查询

 SQL查询通用语法:

select [*|字段名1,字段名2,...|函数(...)] from 表名 where 条件 另外的要求;

 排序查询指的是对某字段进行升序或降序的形式来查询结果,语法:

select * from 表名 where 条件 order by 字段名 [asc|desc];

# 说明:

(1)asc从小到大排列,即升序;

(2)desc从大到小排序,即降序;

(3)默认按照列值从小到大进行排序(即asc升序)。

例如,使用排序查询来完成:

(1)按价格进行升序排序查询所有的商品信息;

(2)按价格进行降序排序查询所有的商品信息;

(3)按照价格升序排序查询名称中有"想"字的所有商品信息。

#########################排序查询################################
# 使用库
use db_product1;
# 升序
select * from product order by price asc;  # 建议
select * from product order by price;
# 降序
select * from product order by price desc;
# 想  升序
select * from product where pname like '%想%' order by price asc;
# select * from product order by price asc where pname like '%想%';  # 报错:程序报错后,就要改错.

2.8.聚合查询

聚合查询的语法:

select 函数(...) from 表名 [where 条件];

聚合函数有:

例如,使用命令完成:

(1)查询商品的总条数;

(2)查询商品价格的最大值;

(3)查询商品价格的最小值;

(4)加入where条件后,查询价格大于200的商品总条数;

(5)查询分类c001中所有商品的总和;

(6)查询分类为c002所有商品的平均价格。

############################聚合函数################################
# 使用库
use db_product1;
# 1
select count(*) from product;
# 2
select max(price) from product;
# 3
select min(price) from product;
# 4
select count(*) from product;
select * from product where price > 200;
select count(*) from product where price > 200;
# 5
select sum(price) from product where category_id='c001';
# 6
select avg(price) from product where category_id='c002';

  • 27
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值