Home » SQL

Implement CREATE VIEW in SQL

In this article, we are going to use CREATE VIEW, basically view can be created from a single, multiple tables or we can use another view.
Submitted by Manu Jemini, on March 04, 2018

Whenever you need to have a different view from your original table all you need to do is to create view with your desired fields. In the example below we have used this operation on our dummy data.

Now, the first thing we need to do is to, find what we will need in our view. After that, execute a SQL query telling database to create a similar view.

In our dummy data we have name and age as our fields, so we tell database to a view having these two fields. We can do this by using the creat_view keyword and then use select keyword keyword to select the records form the database.

This view will be available for us to use select query from. Now yes we can directly get those records from the database without creating any view but then every time we need to a complex query we fetch these records but here all we will do is do a fetch query on the view, just like we would do for the tables.

Dummy data:

Create view Example

Syntax:

CREATE VIEW view_name AS SELECT c1, c2,c3,c4,c5...  
FROM table_name  WHERE  [condition];

Example 1:

CREATE VIEW EMPL_VIEW AS SELECT name, age 
FROM  'includehelp'.'employee';

SELECT * FROM EMPL_VIEW;
Create view Example Ouput 1

Example 2:

CREATE VIEW EMPLOYE_VIEW AS SELECT name, age 
FROM  'includehelp'.'employee' 
WHERE age IS NULL WITH CHECK OPTION;

SELECT * FROM EMPLOYE_VIEW;
Create view Example Ouput 2

Example 3:

DELETE FROM EMPL_VIEW WHERE age = 23;

SELECT * FROM EMPL_VIEW;


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.