Pages

Tuesday 21 January 2020

Views

  •  A virtual table whose contents are obtained from an existing table or tables called base tables.
  • The view itself does not contain any real data; the data is physically stored in the base table.
  • The view simply shows the data contained in the base table.
  • Acts as a dynamic table because it instantly reflects data and structural changes in the base table.
  • Don’t forget they are not real, physical data sets, meaning we cannot insert or update the information that has already been extracted. They should be seen as temporary virtual data tables retrieving information from base tables
Syntax to create a view in MySQL
CREATE OR REPLACE VIEW view_name AS
SELECT
column_1, column_2,… column_n
FROM
table_name;

For example: 
CREATE OR REPLACE VIEW v_dept_emp_latest_date AS
    SELECT
        emp_no, MAX(from_date) AS from_date, MAX(to_date) AS to_date
    FROM
        dept_emp
    GROUP BY emp_no;

A view acts as a shortcut for writing the same SELECT statement every time a new
the request has been made

How to call  the view in MySQL
SELECT * FROM employees.dept_emp_latest_date;
Note: In the above query employees is the database name and dept_emp_latest_date is the view name.

Benefits of Views
Saves a lot of coding time.
Occupies no extra memory

Next Topic: MySQL INDEX