Views

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:
  1. 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.
  2. 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.
  3. Code Manageability : Views help in managing the code in structured and consistent way.
Syntax:
CREATE [OR REPLACE] VIEW <view_name> AS
<sql_select_statement>;
    Here:
     <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