I'm currently having the latest version of MySQL
(ver 8.0.2) and I'm trying to create a read-only
View.
This is how my query looks like:
CREATE VIEW Emp_Salary3 AS
SELECT Empid, Ename, Date_Joined, Salary, Dcode
FROM Employees
WHERE Salary < 35000
WITH READ ONLY;
But then the response I got was:
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
Hence I checked the manual as well, it doesn't have a READ-ONLY option whatsoever. Is there a work around for this?
Any help could be appreciated.
Best Answer
MySQL doesn't support
WITH READ ONLY
forCREATE VIEW
.It is possible to create a read-only view in an indirect way: create an user that has the
SELECT
privilege on all tables andCREATE VIEW
privilege then use this user to create the view and specifySQL SECURITY DEFINER
in theCREATE VIEW
statement:The line
DEFINER = CURRENT_USER
is not needed if you use the limited user to create the view.Or you can use an admin user to create the view and in this case the line
DEFINER =
must contain the name of the user that will own the view.The documentation explains:
This means the users of the view must have at least
SELECT
privilege for the view. Then, if theSQL SECURITY
isDEFINER
then the privileges of theDEFINER
user are applied to the tables and views used in the view definition.