

Also, we want to provide appropriate names to the columns. We want to show only the first name, last name, and gender of employees. Example of MySQL CREATE View or MySQL REPLACE View Statements If you do not specify the database, you might end up creating the View in a different database. Write the View name in the ‘database_name’.’view_name’ format. Note: Create or Replace view keywords create a new view or change the existing View definition. The query is specified after the AS keyword. The select * from is the query that is used to generate the View using base_table.If the View does not exist, it creates a new view using the query specified after the AS keyword. If the View exists, it changes the View definition using the query specified after the AS keyword. When we run the create or the replace view statement, MySQL checks whether it exists in the database. Create or replace View: These keywords serve to create or replace the existing View.The syntax is following: Create or replace View view_name The CREATE VIEW and REPLACE VIEW keywords are used together. The REPLACE VIEW is used to change the definition of a view. The query is as follows: mysql> Select first_name, last_name, case when gender='M' then 'Male' when gender ='F' then 'Female' end as 'Gender', hire_date from vwEmployees Similarly, instead of the printing F, the query must return Female. Instead of printing M, the query must return Male. The output of the gender column returned by the View is either M or F. We print the value of the gender in some meaningful format.

We can use the CASE or IF function on the output of the View. Query output: Use the Control-Flow Function on the View The query is as follows: mysql> select * from vwemployees where Year(hire_date)>=1990 Let’s retrieve the list of the employees whose joining year is greater or equal to 1990. We can use the date-time function on the View output. Query Output: Use the Date-Time Function on the View The query is as follows: mysql> select * from vwemployees where emp_no select gender, count(emp_no)as 'Total Employees' from vwEmployees group by gender We want to retrieve the list of the employees whose employee number is less than 10005. The query is as follows: mysql> select * from vwEmployees where gender='F' In our case, we want to retrieve the list of female employees from the vwEmployees. We can filter the View output using the WHERE clause. Output: Filter the Data of the View Using the WHERE Clause To view the data of vwEmployees, run the following query: mysql> select * from vwEmployees To access the data of the View, we can use the SELECT query. The definition of the View is following: Create view vwEmployees Suppose we want to create a view that populates the list of employees.
#Mysql create view designer code#
You use this query in the application code frequently. Suppose you have a complex SQL query using that uses multiple joins and complex business logic. When we run the SELECT query to populate the data, it executes a query that creates the view (View definition).

Unlike physical tables, views do not store data in a database. An SQL view is a virtual table or a result-set generated by the SELECT query.
