目录
一、SQL分类:
二、DDL:
三、DML:
四、DQL:
4.1 基本查询:
4.2 条件查询:
4.3 聚合函数:
4.4 分组查询:
4.5 排序查询:
4.6 分页查询:
4.7 DQL执行顺序:
五、DCL:
5.1 用户管理:
5.2 权限控制:
一、SQL分类:
二、DDL:
- alter table tb_user deng;(改表名)
- alter table tb_user changem_name name varchar(30) comment '用户名';
- alter table tb_user modify sex char(1);(改数据)
三、DML:
# DML : 对数据库中表的数据记录进行 增删改 操作
# 插入 数据 【要一一对应】
insert into test01 (name, id, age) values ('deng',105,20);
insert into test01 (name, id, age) values ('jmsq',2,18);
# 修改 数据
update test01 set id = 1 where name = 'deng'; #在姓名为deng处,修改id为1
update test01 set age = 20; #年龄修改为 20
# 删除 数据
delete from test01 where id = 105; #删除所有 id=105 的数据
delete from test01; #删除表中的所有数据
四、DQL:
4.1 基本查询:
#----------------------基本查询-----------------
# 1、查询 指定字段
select name,age from emp; #[查询指定字段]
# 2、查询 所有字符
select * from emp; #[查询所有的字段]
# 3、 查询 【起别名】
select name as '姓名' from emp;
select name from emp;
# 4、 查询 姓名 【不要重复的】
select distinct name as '姓名' from emp;
4.2 条件查询:
#----------------------条件查询-----------------
# 1、 查询 年龄等于20岁的员工信息
select * from emp where age = 20; #[同理:可以改为: <=, >=,!=,<,>]
# 1、 查询 年龄 大于15岁 小于20岁的员工信息
select * from emp where age>=15 && age <=20;
select * from emp where age>=15 and age <=20; # [建议使用and]
select * from emp where age between 15 and 20; # [15-20岁,闭区间]
select * from emp where age=18 or age=20; # [or]
select * from emp where age in (15,18) # [和or一样]
# 2、 查询没有 age 的员工
select * from emp where age is null ;
# 3、 查询有 age 的员工
select * from emp where age is not null ;
# 4、 查询 姓名 为4个字的员工信息
select * from emp where name like '____';
# 5、 查询 姓名 最后一位为 q 的员工信息
select * from emp where name like '%q';
select * from emp where name like '__y_'; # [这个感觉 有点万能] [对于字符串而言]
select * from emp where name like '___1
';
4.3 聚合函数:
#----------------------聚合函数-----------------
-- 1.统计 员工表的 数量
select count(*) from emp;
select count(name) from emp;
-- 2.统计 员工的 平均年龄
select avg(age) from emp;
-- 3.统计 员工的 最大(最小)年龄
select min(age) from emp;
select max(age) from emp;
-- 4.统计年龄之和
select sum(age) from emp where age = 18;
4.4 分组查询:
#----------------------分组查询-----------------
-- 1.根据年龄分组 并统计员工的 数量
select age, count(*) from emp group by age;
-- 2.根据年龄分组 并统计员工的 平均年龄
select age, avg(age) from emp group by age;
-- 3.根据年龄分组【小于25才可以】,并获取 数量大于等于2的 员工年龄
select age as '年龄', count(*) as '该年龄的总人数' from emp where age<25 group by age having count(*)>=2;
-- 总结: where是 表后面, having是 group by 后面
4.5 排序查询:
#----------------------排序查询-----------------
-- 1.按照年龄 进行升序排序 [从小到大]
select * from emp order by age asc;
select * from emp order by age ; # [asc是默认值,是可以省略的]
-- 2.按照年龄 进行降序排序 [从大到小]
select * from emp order by age desc;
-- 3.按照年龄对员工进行升序排序, 如果年龄相同,则按照id进行降序排序
select * from emp order by age asc, id desc ;
4.6 分页查询:
#----------------------分页查询-----------------
-- 1. 查询第一页员工数据,每页展示5条记录
select * from emp limit 0,5;
select * from emp limit 5; #[如果是从0开始,则可简写]
-- 1. 查询第二页员工数据,每页展示5条记录------》[页码-1]*页展示数
select * from emp limit 5,5;
4.7 DQL执行顺序:
五、DCL:
5.1 用户管理:
-- 创建用户 【只能在当前主机localhost访问,密码为:123456】
create user 'it'@'localhost' identified by '123456';
-- 创建用户 【可以在任意主机访问数据库,密码为123456】
create user 'its'@'%' identified by '123456';
-- 修改用户 its 的密码
alter user 'its'@'%' identified with mysql_native_password by '123';
-- 删除用户 it
drop user 'it'@'localhost';
5.2 权限控制:
#-------------------权限控制:
-- 1.查询权限 [its 用户 只能登陆mysql]
show grants for 'its'@'%';
-- 2.授予权限 [its 用户 可以查看 test数据库]
grant all on test.* to 'its'@'%';
-- 3.撤销权限 []
revoke all on test.* from 'its'@'%';