详解MySQL第三篇—DCL语句
DCL(Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许可和访问级别的语句。这些语句定义了数据库、表、字段、用户的访问权限和安全级别。主要的语句关键字包括 grant、revoke 等。
DCL 语句主要是 DBA 用来管理系统中的对象权限时所使用,一般的开发人员很少使用。下面通过一个例子来简单说明一下。
创建一个数据库用户 z1,具有对 sakila 数据库中所有表的 SELECT/INSERT 权限:
1
2
3
4
5
6
7
8
9
10
11
12
|
mysql> grant select,insert on sakila.* to 'z1'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[mysql@db3 ~]$ mysql -uz1 -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21671 to server version: 5.1.9-beta-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use sakila
Database changed
mysql> insert into emp values('bzshen','2005-04-01',3000,'3');
Query OK, 1 row affected (0.04 sec)
|
由于权限变更,需要将 z1 的权限变更,收回 INSERT,只能对数据进行 SELECT 操作:
1
2
3
4
5
6
7
8
|
[mysql@db3 ~]$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21757 to server version: 5.1.9-beta-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> revoke insert on sakila.* from 'z1'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
|
用户 z1 重新登录后执行前面语句:
1
2
3
4
5
6
7
8
9
10
|
[mysql@db3 ~]$ mysql -uz1 -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21763 to server version: 5.1.9-beta-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> insert into emp values('bzshen','2005-04-01',3000,'3');
ERROR 1046 (3D000): No database selected
mysql> use sakila
Database changed
mysql> insert into emp values('bzshen','2005-04-01',3000,'3');
ERROR 1142 (42000): INSERT command denied to user 'z1'@'localhost' for table 'emp'
|
以上例子中的 grant 和 revoke 分别授出和收回了用户 z1 的部分权限,达到了我们的目的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2015-03-31 HDU1838:Chessboard(线性dp)
2015-03-31 HDU4223:Dynamic Programming?(简单dp)
2015-03-31 HDU1712:ACboy needs your help(分组背包)
2015-03-31 HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)
2015-03-31 HDU1165: Eddy's research II(递推)
2015-03-31 HDU1158:Employment Planning(线性dp)
2015-03-31 HDU1081:To The Max(最大子矩阵,线性DP)