是否可以在MySQL中创建具有只读选项的视图?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了是否可以在MySQL中创建具有只读选项的视图?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2219字,纯文字阅读大概需要4分钟。
内容图文
我目前正在使用最新版本的MySQL(版本8.0.2),我正在尝试创建一个只读视图.
这是我的查询的样子:
CREATE VIEW Emp_Salary3 AS
SELECT Empid, Ename, Date_Joined, Salary, Dcode
FROM Employees
WHERE Salary < 35000
WITH READ ONLY;
但后来我得到的回应是:
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near ‘READ ONLY’ at line 5
因此我也检查了manual,它没有任何READ-ONLY选项.有没有解决这个问题?
任何帮助都可以得到赞赏.
解决方法:
MySQL不支持WITH READ ONLY for CREATE VIEW.
可以间接方式创建只读视图:创建对所有表具有SELECT权限的用户和CREATE VIEW权限然后使用此用户创建视图并在CREATE VIEW语句中指定SQL SECURITY DEFINER:
CREATE
DEFINER = CURRENT_USER
SQL SECURITY DEFINER
VIEW Emp_Salary3 AS
SELECT Empid, Ename, Date_Joined, Salary, Dcode
FROM Employees
WHERE Salary < 35000
如果您使用受限用户创建视图,则不需要DEFINER = CURRENT_USER行.
或者您可以使用管理员用户创建视图,在这种情况下,行DEFINER =必须包含将拥有该视图的用户的名称.
文档说明:
MySQL checks view privileges like this:
At view definition time, the view creator must have the privileges needed to use the top-level objects accessed by the view. For example, if the view definition refers to table columns, the creator must have some privilege for each column in the select list of the definition, and the
SELECT
privilege for each column used elsewhere in the definition. If the definition refers to a stored function, only the privileges needed to invoke the function can be checked. The privileges required at function invocation time can be checked only as it executes: For different invocations, different execution paths within the function might be taken.The user who references a view must have appropriate privileges to access it (
SELECT
to select from it,INSERT
to insert into it, and so forth.)When a view has been referenced, privileges for objects accessed by the view are checked against the privileges held by the view
DEFINER
account or invoker, depending on whether theSQL SECURITY
characteristic isDEFINER
orINVOKER
, respectively.
这意味着视图的用户必须至少具有该视图的SELECT权限.然后,如果SQL SECURITY是DEFINER,那么DEFINER用户的权限将应用于视图定义中使用的表和视图.
内容总结
以上是互联网集市为您收集整理的是否可以在MySQL中创建具有只读选项的视图?全部内容,希望文章能够帮你解决是否可以在MySQL中创建具有只读选项的视图?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。