View is a representation of collection of data from one or more tables. It usually consist of SQL select statement to select data from combination of related tables but can also be used to update , insert and delete data from the tables.
Now the question is when we are using the SQL statements only why we need Views? Answer is:
CREATE [OR REPLACE] VIEW <view_name> AS
<sql_select_statement>;
<view_name>: is the name of the view
<sql_select_statement>: SQL SELECT statement against one or more tables in the database.
For Ex:
Create or replace view employee_department
AS
SELECT emp.name,
emp.empID,
dept.name
FROM EMPLOYEE emp,
DEPARTMENT dept
Where emp.deptid = dept.id
Now the question is when we are using the SQL statements only why we need Views? Answer is:
- Security : With the help of views you can restrict and manage what data and fields from a single or multiple tables the users should be able to see.
- Repetitive code: If some code needs to be used mutiple times its better to save as views so that every time the same need not to written again.
- Code Manageability : Views help in managing the code in structured and consistent way.
CREATE [OR REPLACE] VIEW <view_name> AS
<sql_select_statement>;
<view_name>: is the name of the view
<sql_select_statement>: SQL SELECT statement against one or more tables in the database.
For Ex:
Create or replace view employee_department
AS
SELECT emp.name,
emp.empID,
dept.name
FROM EMPLOYEE emp,
DEPARTMENT dept
Where emp.deptid = dept.id
No comments:
Post a Comment