MySQL基础之DCL

一、基础回顾:

举例如下:

--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'@'%';

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值