二、MySQL基础语法重点!(增删查改)

目录

一、SQL的增删改(DML)

1、添加数据 

2、修改数据 

3、删除数据 

 二、SQL查询(DQL)

(一)基本查询

 (二)条件查询

 (三)聚合函数

(四)分组查询 

 (五)排序查询

(六)分页查询 

 (七)总结

 三、SQL的数据控制语言(DCL)


一、SQL的增删改(DML)

DML介绍,对于数据库进行增删改的操作。

DML英文全称是Data Manipulation Language(数据操作语言),用来对数据库中表的数据记录进行增删改操作。

添加数据 (INSERT)
修改数据 (UPDATE)
删除数据(DELETE)

1、添加数据 

注意:
插入数据时,指定的字段顺序需要与值的顺序是一一对应的;

字符串和日期型数据应该包含在引号中;

插入的数据大小,应该在字段的规定范围内。 

insert into emp values (3,'03','张凯','男',31,'223456789011111111','2002-02-02'),(4,'04','李德志','男',25,'323456789011111111','2005-02-02');
select * from emp;

2、修改数据 

update emp set name='IT黑马' where id=1;

3、删除数据 

delete from emp where gender='女';

 

 delete from emp;        #删除表的所有数据

 二、SQL查询(DQL)

DQL-介绍
DOL英文全称是Data QueryLanquage(数据查询语言),数据查询语言,用来查询数据库中表的记录

(一)基本查询


查询关键字:SELECT

1、创建emp表 

create table emp(
    id int comment '编号',
    workno varchar(10) comment '工号',
    name varchar(10) comment '姓名',
    gender char comment '性别',
    age tinyint unsigned comment '年龄',
    idcard char(18) comment '身份证号码',
    workaddress varchar(50) comment '工作地址',
    entrydate date comment '入职时间'

)comment '员工表';

 2、向emp表中插入数据

insert into emp (id,workno,name,gender,age,idcard,workaddress,entrydate)
values  (1,'1','柳岩','女',20,'123456789012345678','北京','2000-01-01'),
        (2,'2','张无忌','男',18,'123456789012345670','深圳','2005-09-01'),
        (3,'3','韦一笑','男',38,'123456789712345670','上海','2005-08-01'),
        (4,'4','柳啦','女',20,'223456789012345678','南昌','2000-01-01'),
        (5,'5','张无的','男',18,'223456789012345670','九江','2005-09-01'),
        (6,'6','韦一撒','男',38,'223456789712345670','赣州','2005-08-01'),
        (7,'7','张凯','女',20,'323456789012345678','新余','2000-01-01'),
        (8,'8','张无','男',18,'323456789012345670','赣州','2005-09-01'),
        (9,'9','韦一','男',38,'323456789712345670','广州','2005-08-01'),
        (10,'10','李坤','女',20,'423456789012345678','武汉','2000-01-01'),
        (11,'11','张飞','男',18,'423456789012345670','长沙','2005-09-01'),
        (12,'12','吴大','男',38,'423456789712345670','杭州','2005-08-01'),
        (13,'13','邓琦','女',20,'523456789012345678','成都','2000-01-01'),
        (14,'14','张然','男',18,'523456789012345670','重庆','2005-09-01'),
        (15,'15','刘亮','男',38,null,'南京','2005-08-01');

 3、对emp表进行查询

select * from emp;        #查询emp表中所有数据
select workno,name,age from emp;        #查询出emp表中字段workno,name,age中的数据
select id,workno,name,gender,age,idcard,workaddress,entrydate from emp;        #查询表中所有字段数据
select workaddress from emp;        #查询表中当个字段数据
select workaddress as '工作地址' from emp;        对于单个字段选取别名
select distinct emp.workaddress as '工作地址' from emp;        #去除重复字段名

 (二)条件查询

 

#查询表中所有数据
select * from emp;
#查询表中年龄=18
select * from emp where age=18;
#查询表中年龄小于等于20
select * from emp where age<=20;
#查询表中身份证号为空
select * from emp where idcard is null;
#查询身份证不为空
select * from emp where idcard is not null;
#查询年龄不等于18
select * from emp where age<>18;
#查询年龄大于等于15小于等于18
select * from emp where age>=15 and age<=18;
select * from emp where age between 15 and 20;        #between...and..之间只能由小到大
select * from emp where gender='女' and age < 30;
select * from emp where age=18 or age=38 or age=30;
select * from emp where age in(18,20,30);   #在in中满足条件
select * from emp where name like '__';     #模糊查询两个字的员工姓名
select * from emp where name like '___';
select * from emp where idcard like '%0';       #查询身份证号最后一位为0

 

 (三)聚合函数

 注意:null值不参与所有聚合行数运算;

select count(*) from emp;
select count(name) from emp;    #统计字段数量
select count(emp.idcard) from emp;  
select avg(emp.age) from emp;   #查询年龄平均值
select max(emp.age) from emp;   #查询年龄最大值
select min(emp.age) from emp;   #查询年龄最小值
select sum(age) from emp where workaddress='北京';        #查询北京地区年龄之和

(四)分组查询 

案例语句: 

select gender,count(*) from emp group by gender;    
#根据性别分组,统计男性员工和女性员工人数
select gender,avg(age) from emp group by gender;    
#根据性别分组,统计男性员工和女性员工平均年龄
select workaddress,count(*) from emp where age<45 group by workaddress having count(*)>=2;
#查询年龄小于45,并根据工作地址来分组,获取员工数量大于等于2的工作地址
select workaddress,count(*) as address_count from emp where age<45 group by workaddress having address_count>=2;

 (五)排序查询

 案例语句:

select * from emp order by age asc;
#根据年龄升序
select * from emp order by age desc ;
select * from emp order by entrydate desc ;
#根据工作时间降序
select * from emp order by entrydate asc ;
#根据年龄进行升序,年龄相同按工作日期降序排
select * from emp order by age asc ,entrydate desc ;

(六)分页查询 

 注意:
起始索引从0开始,起始索引= (查询页码 - 1) * 每页显示记录数;

分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT;

如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 10。

 案例语句:

select * from emp limit 0,5;
#查询第一页员工数据,每页展示5条记录
select * from emp limit 10,5;
#查询员工表数据第二页,展示5条数据 ------》注意:(页码数-1)*页面展示数量

案例练习: 

案例实现:

select * from emp where gender='女' and age in(18,20,30);
#统计表中性别为女,年龄在18,20,30的人

select * from emp where gender='男' and (age between 10 and 30) and name like '__';
#统计表中性别为男,年龄在10到30之间以及姓名两个字

select gender,count(*) from emp where age<60 group by gender;
#统计表中年龄小于60,男性和女性员工人数

select name,age from emp where age<=30 order by age asc ,entrydate desc ;
#查询数据表中所有年龄小于等于30的人员性别和年龄,并对查询年龄按升序排序,年龄相同按入职时间降序排序

select * from emp where gender='男' and (age between 20 and 40) order by emp.age ,entrydate desc limit 0,5;
#查询员工性别为男,且年龄在20-40以内的前52位员工信息,对查询结果按年龄升序排序,年龄相同按入职时间降序排

 (七)总结

 三、SQL的数据控制语言(DCL)

实战案例:

可以到自己数据库中查看用户数 

#创建用户itcast只能够在当前主机localhost访问,密码为123456
create user 'itcast'@'localhost' identified by '123456';
#创建用户zch,可以在任何主机上访问数据库,密码为123456
create user 'zch'@'%' identified by '123456';
#修改用户zch访问密码为123456789
alter user 'zch'@'%' identified with mysql_native_password by '123456789';  #语句有误
set password for 'zch'@'%' = password ('123456789');
set password for 'zch'@'%' = password ('123456');

#删除用户itcast
drop user 'itcast'@'localhost';

 注意:

  • 主机名可以使用 % 通配。
  • 这类SQL开发人员操作的比较少,主要是DBA(DatabaseAdministrator数据库管理员)使用。

权限控制 

注意:

  • 多个权限之间,使用逗号分隔
  • 授权时,数据库名和表名可以使用*进行通配,代表所有。 

案例:

#查询权限
show grants for 'zch'@'%';
#授予权限
grant all on itcast.* to 'zch'@'%';
#撤销权限
revoke all on itcast.* from 'zch'@'%';

 查询权限的结果:

重点这是以上使用的MySQL代码:

select * from emp;
select workno,name,age from emp;
select id,workno,name,gender,age,idcard,workaddress,entrydate from emp;
select emp.workaddress from emp;
select emp.workaddress as '工作地址' from emp;
select distinct emp.workaddress as '工作地址' from emp;
select * from emp;
select * from emp where age=18;
select * from emp where age<=20;
select * from emp where idcard is null;
select * from emp where idcard is not null;
select * from emp where age<>18;
select * from emp where age>=15 and age<=18;
select * from emp where age between 15 and 20;
select * from emp where gender='女' and age < 30;
select * from emp where age=18 or age=38 or age=30;
select * from emp where age in(18,20,30);   #在in中满足条件
select * from emp where name like '__';     #模糊查询两个字的员工姓名
select * from emp where name like '___';
select * from emp where idcard like '%0';       #查询身份证号最后一位为0

select count(*) from emp;
select count(name) from emp;    #统计字段数量
select count(emp.idcard) from emp;
select avg(emp.age) from emp;   #查询年龄平均值
select max(emp.age) from emp;   #查询年龄最大值
select min(emp.age) from emp;   #查询年龄最小值
select sum(age) from emp where workaddress='北京';        #查询北京地区年龄之和
select emp.gender,count(*) from emp order by gender;
select * from emp;
select gender,count(*) from emp group by gender;    #根据性别分组,统计男性员工和女性员工人数
select gender,avg(age) from emp group by gender;    #根据性别分组,统计男性员工和女性员工平均年龄
select workaddress,count(*) from emp where age<45 group by workaddress having count(*)>=2;
#查询年龄小于45,并根据工作地址来分组,获取员工数量大于等于2的工作地址
select workaddress,count(*) as address_count from emp where age<45 group by workaddress having address_count>=2;
select * from emp order by age asc;
#根据年龄升序
select * from emp order by age desc ;
select * from emp order by entrydate desc ;
#根据工作时间降序
select * from emp order by entrydate asc ;
#根据年龄进行升序,年龄相同按工作日期降序排
select * from emp order by age asc ,entrydate desc ;
select * from emp limit 0,5;
#查询第一页员工数据,每页展示5条记录
select * from emp limit 10,5;
#查询员工表数据第二页,展示5条数据 ------》注意:(页码数-1)*页面展示数量


select * from emp where gender='女' and age in(18,20,30);
#统计表中性别为女,年龄在18,20,30的人
select * from emp where gender='男' and (age between 10 and 30) and name like '__';
#统计表中性别为男,年龄在10到30之间以及姓名两个字
select gender,count(*) from emp where age<60 group by gender;
#统计表中年龄小于60,男性和女性员工人数
select name,age from emp where age<=30 order by age asc ,entrydate desc ;
#查询数据表中所有年龄小于等于30的人员性别和年龄,并对查询年龄按升序排序,年龄相同按入职时间降序排序
select * from emp where gender='男' and (age between 20 and 40) order by emp.age ,entrydate desc limit 0,5;
#查询员工性别为男,且年龄在20-40以内的前52位员工信息,对查询结果按年龄升序排序,年龄相同按入职时间降序排

 

喜欢的欢迎一键三连。

下一节学习:三、MySQL基础语法(数据库函数详解看这一篇就够了!)-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值