Summary -
In this topic, we described about the below sections -
Why to create view?
Let us assume if other users have access to run query on table, they can get all data from the table that includes sensitive information too. To avoid this situation, view came into picture as a solution. By creating view, we can give access to the selected columns on table. End users can work on view without having the idea about the table what it contains. By using view, we can combine data from various tables and make it as one table to use.
Views are created for the purpose of hiding some secured information from end users. Views are created with all or some specific columns from table as per requirement and hides the sensitive information from end users.
The tables that are used to create the view are called as base tables.
View is nothing but select statement output which is stored in database. It just looks like a normal table, end users can do all operations like select, create, update statements on views. View can create from existing one or more tables in database.
How to create view?
CREATE VIEW statement is used to create the view from one or more tables with all or selected columns from the base tables.
Syntax -
FOR CREATE view -
CREATE VIEW view_name AS SELECT column1,column2
FROM table_name [ WHERE condition ];
CREATE VIEW by using multiple tables -
CREATE VIEW view_name AS SELECT table_name1.column1,
table_name1.column2, table_name2.column1, table_name2.column2, ….,
table_nameN.columnN
FROM table_name1, table_name2, …, table_nameN
WHERE condition;
CREATE VIEW WITH CHECK OPTION -
CREATE VIEW view_name AS
SELECT column1,column2
FROM table_name WHERE condition WITH CHECK OPTION;
CREATE VIEW with aggregate function -
CREATE VIEW view_name AS
SELECT aggregate function (column1),column2
FROM table_name WHERE condition;
- view_name - Represents the name of the view which is going to crate.
- column1,column2 - Represents the included columns on view.
- WITH CHECK Option - Specifies to the SQL server that all modification on the view should satify the definition of the view.
Example -
Let us consider below table(s) as an example table(s) to frame the SQL query for getting the desired results.
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
department_details –
dept_id | dept_name | dept_location |
---|---|---|
1000 | Office | Location1 |
2000 | Sales | Location2 |
Scenario – Creating view for one table.
Requirement – Creating view employee_basic_info on employee_details table with three columns like emp_id, emp_name, designation. The query was as follows -
CREATE VIEW employee_basic_info AS
SELECT emp_id, emp_name, designation
FROM employee_details;
By executing above query, we can create view. The output was shown as below –
employee_basic_info -
emp_id | emp_name | designation |
---|---|---|
001 | Employee1 | Director |
002 | Employee2 | Director |
003 | Employee3 | Manager |
004 | Employee4 | Manager |
005 | Employee5 | Analyst |
006 | Employee6 | Analyst |
007 | Employee7 | Clerk |
008 | Employee8 | Salesman |
009 | Employee9 | Salesman |
Scenario – Creating view from multiple tables.
Requirement – Creating view employee_full_info on employee_details and department_details table. The query was as follows –
CREATE VIEW department AS
SELECT employee_details.emp_id,
employee_details.emp_name,
department_details.dept_name,
department_details.dept_location
FROM employee_details, department_details
WHERE employee_details.dept_id = department.dept_id;
By executing above query, we can create view from multiple tables. The output was shown as below –
emp_id | emp_name | dept_name | dept_location |
---|---|---|---|
001 | Employee1 | Office | Location1 |
002 | Employee2 | Sales | Location2 |
003 | Employee3 | Office | Location1 |
004 | Employee4 | Sales | Location2 |
005 | Employee5 | Office | Location1 |
006 | Employee6 | Office | Location1 |
007 | Employee7 | Office | Location1 |
008 | Employee8 | Sales | Location2 |
009 | Employee9 | Sales | Location2 |
Once view was created, we can run the query like normal table.
Scenario – Getting data from view.
Requirement – Fetch the data from employee_full_info view. The query is as shown below -
SELECT * FROM employee_full_info;
By executing above query, we can get all details from view. The output was shown as below -
emp_id | emp_name | dept_name | dept_location |
---|---|---|---|
001 | Employee1 | Office | Location1 |
002 | Employee2 | Sales | Location2 |
003 | Employee3 | Office | Location1 |
004 | Employee4 | Sales | Location2 |
005 | Employee5 | Office | Location1 |
006 | Employee6 | Office | Location1 |
007 | Employee7 | Office | Location1 |
008 | Employee8 | Sales | Location2 |
009 | Employee9 | Sales | Location2 |
Scenario – Creating view by using aggregate functions.
Requirement – Create view employee_aggr by using aggregate functions. The query was as follows -
CREATE VIEW employee (total employees, designation) AS
SELECT COUNT(emp_id), designation
FROM employee_details
GROUP BY designation;
By executing above query, we can create view with aggregate function. The output was shown as follows –
total_employees | designation |
---|---|
2 | Analyst |
1 | Clerk |
2 | Director |
2 | Manager |
2 | Salesman |