一、基础回顾:
举例如下:
--1、查询年龄是20,23,25岁的男性员工信息--
select *
from table_employee
where gender = '男' and age in (20,23,25);
--2、查询性别为女,且年龄在20~30岁(含)以内的三个字名字的员工--
select *
from table_employee
where gender = '女' and (age between 20 and 30) and name like '___';
-3、统计年龄小于60岁的男性及女性员工人数--
select gender, count(*)
from table_employee
where age < 60
group by gender;
--4、查询所有年龄小于等于40对员工姓名和年龄,并按年龄升序排序,年龄相同时按入职时间降序排列--
select name, age
from table_employee
where age <= 40
order by age asc, entrydate desc;
--或者--
select name, age
from table_employee t
where t.age <= 40
order by age asc, entrydate desc;
--或者--
select t.name, t.age
from table_employee t
where t.age <= 40
order by age asc, entrydate desc;
/*5、查询性别为女,且年龄在20-40岁(含)以内的前5名员工信息,对结果按年龄升序排序,年龄相同时按入职时间升序排列*/
select *
from table_employee
where gender = '女' and age between 20 and 40
order by age asc, entrydate asc
limit 5;
二、DCL
数据控制语言:Data Control Language。用来授权或回收访问数据库的某种特权,并控制数据库操纵事务发生的时间及效果,能够对数据库进行监视,即管理数据库用户,控制数据库的访问权限。比如常见的授权、取消授权、回滚、提交等等操作。注意是数据库管理员DBA(Database Administrator)使用
DCL管理用户
1、查询用户
USE mysql;
SELECT * FROM user;
--创建用户‘itcast'只能在当前主机localhost访问,密码‘123456’--
create user 'itcast'@'localhost' identified by '123456';
2、创建用户
CREATE USER '用户名'@'主机名'
IDENTIFIED BY ‘密码’;
--创建用户‘nana‘,可以在任意主机访问该数据库,密码’123456‘--
create user 'nana'@'%' identified by '123456';
3、修改用户
主机名可以使用%通配。
ALTER USER '用户名'@'主机名'
IDENTIFIED WITH mysql_native_password BY '新密码';
--修改用户‘nana’的访问密码为888888--
create user 'nana'@'%'
identified with mysql_native_password by '888888';
4、删除用户
DROP USER '用户名'@'主机名';
--删除itcast@localhost用户--
drop user 'itcast'@'localhost';
DCL权限控制
权限 | 说明 |
ALL , ALL PRIVILEGES | 所有权限 |
SELECT | 查询数据 |
INSERT | 插入数据 |
UPDATE | 修改数据 |
DELETE | 删除数据 |
ALIER | 修改表 |
DROP | 删除数据库/表/视图 |
CREATE | 创建数据库/表 |
1,查询权限
SHOW GRANTS FOR '用户名'@'主机名';
show grants for 'host'@'%';
2,授予权限
GRANT 权限列表 CN 数据库名.表名 TO '用户名'@'主机名';
grant all on it cast.* to 'host'@'%';
3,撤销权限
REVOKE 权限列表 ON 数据库名.列表 FROM '用户名'@'主机名';
revoke all on it cast.* from 'host'@'%';