How can we create MySQL views?

MySQLMySQLi Database

MySQL views are created using the CREATE VIEW statement. Views can be created from a single table, multiple tables, or another view. As we know that to create a view, a user must have the appropriate system privilege according to the specific implementation.

The basic CREATE VIEW syntax is as follows −

Syntax

CREATE
   [OR REPLACE]
   [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
   [DEFINER = { user | CURRENT_USER }]
   [SQL SECURITY { DEFINER | INVOKER }]
   VIEW view_name [(column_list)]
   AS select_statement
   [WITH [CASCADED | LOCAL] CHECK OPTION]

Here,

  • CREATE VIEW − This statement creates a new view.

  •  View_name − view_name is the name of the view. A view always belongs to a database. By default, a new view is created in the currently used database.

  •  Select_statement − The select_statement is a SELECT statement and provides the definition of the view. Select_statement can select data from base tables or other views.

  • Column_list − The column_list part is optional. It provides a list of names for the view's columns right after the view name where the names must be unique. The number of names in column_list must be the same as the number of columns retrieved by the SELECT statement. If we want to give our view column’s a different name, we can do so by adding an [AS name] clause in the select list.

  •  OR REPLACE − If the optional OR REPLACE clause is added with CREATE VIEW statement, the CREATE VIEW statement replaces an existing view and create a new one. If the view does not exist, CREATE VIEW is the same as CREATE OR REPLACE VIEW.

  •  ALGORITHM − The ALGORITHM clause is optional, it affects how MySQL processes the view. ALGORITHM takes three values: MERGE, TEMPTABLE, or UNDEFINED. The default algorithm is UNDEFINED.

  • [DEFINER = { user | CURRENT_USER }] [SQL SECURITY { DEFINER | INVOKER }] − The DEFINER and SQL SECURITY clauses specify the security context to be used when checking access privileges at view invocation time. If you specify the DEFINER clause, the following rules determine the legal DEFINER user values −

    • If you do not have the SUPER privilege, the only legal user value is your own account and you cannot set the definer to some other account.

    • If you have the SUPER privilege, you can specify any syntactically legal account name.

Within a stored routine that is defined with the SQL SECURITY DEFINER characteristic, CURRENT_USER returns the routine's DEFINER value. This also affects a view defined within such a routine, if the view definition contains a DEFINER value of CURRENT_USER.

  • 8. [WITH [CASCADED | LOCAL] CHECK OPTION] − The WITH CHECK OPTION clause can be given for an updatable view to preventing inserts or updates to rows except those for which the WHERE clause in the select_statement is true. In a WITH CHECK OPTION clause for an updatable view, the LOCAL and CASCADED keywords determine the scope of check testing when the view is defined in terms of another view. The LOCAL keyword restricts the CHECK OPTION only to the view being defined. CASCADED causes the checks for underlying views to be evaluated as well. When neither keyword is given, the default is CASCADED.

Example

In this example, we are creating a view named First_View on table ‘Customers’ having the data as follows −

mysql> Select * from Customers;
+-------------+----------+
| Customer_Id | Name     |
+-------------+----------+
| 1           | Rahul    |
| 2           | Yashpal  |
| 3           | Gaurav   |
| 4           | Virender |
+-------------+----------+
4 rows in set (1.30 sec)

mysql> Create view first_view AS SELECT * FROM Customers;
Query OK, 0 rows affected (0.36 sec)

Now, if we will run the query using the name of the view then we will get the details from the table on which it is created.

mysql> Select * from first_view;
+-------------+----------+
| Customer_Id | Name     |
+-------------+----------+
| 1           | Rahul    |
| 2           | Yashpal  |
| 3           | Gaurav   |
| 4           | Virender |
+-------------+----------+
4 rows in set (0.13 sec)
raja
Published on 22-Feb-2018 09:40:22
Advertisements