Introduction to Views Module 5
Introduction to views Advantages of views To Create Views To Alter Views To Drop Views To Rename Views View with Check Option How to get the information on view Contents
Objectives At the end of this presentation you should be able to: Understand the concept of views Understand why views are required in a database Understand how to create and work with the views  Understand how to get information about a view
Introduction to Views View is a database object which contains query. A view is often referred to as a virtual table i.e.., a view looks like a table and is referred like a table. They do not contain data like tables. They can be used to join two  tables in a database and present  the underlying data as if the data was coming from a single table.
Introduction to Views (Continued) View can be used as a security mechanism to  restrict the data available to the end users Views can also be used  to store and reuse the complex query which generates the reports. Views hides the underlying complex database structure.
Advantages of Views A view provides following advantages: Focuses data for users. Hides data complexity. Simplifies permission management. Organizes data for export to other applications. Reduces object size.
Example of a Views   This example shows creating a view called ‘ViewPublishers’ from the Publishers table. This view contains two columns ‘Pubid’ and ‘Pubname’ which are taken from the table. The actual data will be in the Publishers  table, which can be  viewed and manipulated from the ‘ViewPublishers’ view. Publishers PARIS IL Chicago Five lake 1622 USA CA Berkely Algodata 1389 USA DC Washington Binnet  0877 CHICAGO MA Boston New Moon  0736 Country State City Pubname Pubid ViewPublishers Five lake 1389 Algodata 0877 Binnet 0736 Pubname Pubid
To Create  Views Syntax: Example to create view from publishers table Example to create a view from Publishers table for city “paris” CREATE VIEW <ViewName> AS <Select Statement> CREATE VIEW ViewPublishers As SELECT * FROM Publishers CREATE VIEW ViewPublishers  AS SELECT  pubid, pubname , city FROM Publishers WHERE city =‘paris’
Simple views  Simple View is also known as a normal view  It contains data only from one table  It won’t contain JOINS or GROUP By statements The underlying data from the base tables can be updated using an UPDATE statement. Example This example creates a simple view  ViewPublisher  which selects  all the data from Publishers Table CREATE VIEW ViewPublisher AS SELECT * FROM Publishers
Complex Views It will be accessed from multiple tables Normally a complex view will contain joins It contains group functions to display values from two tables.  It cannot be updated using UPDATE Clause The example creates a complex view Dept_vw which selects the department name, minimum salary, maximum salary, and average salary. It uses three Aggregate functions and it joins two tables Employee and Department. CREATE VIEW Dept_vw  (deptname,minSalary,maxSalary,avgSalary) AS  SELECT d.deptName,min(e.salary),max(e.salary),avg(e.salary) FROM  Employee e, Department d WHERE  e.deptId = d.deptId GROUP BY d.deptName
View With Check option With Check  option specifies the level of checking to be done while inserting or modifying data through view If the view in read-only view Check option cannot be used It allows integrity constraints and data validation checks to be enforced on data begin Inserted or Updated CREATE VIEW empdept_vw as select empno,ename,deptno from emp where deptno =30 With check option; The example uses ‘With Check Option’ clause, which will restrict the insertion of a row that cannot be selected by the view
To Alter Views View can be modified without dropping it. This ensures that the permissions on the view are not lost  View can be modified without affecting it’s dependent objects such as triggers and procedures Alters the view to add publishers’ country column of the Publishers table ALTER VIEW ViewPublishers As SELECT pubid, pubname, pubcity, country From publishers
To Drop Views View can be dropped from a database  by using DROP VIEW When a view is dropped, it has no side effects on the underlying table (s). DROP VIEW <ViewName>
To Rename Views View can be renamed without having to drop it. Only requirement is that views must be in current database A view can be renamed only by its owner It can also be renamed by the owner of the database sp_rename <Old_ViewName>,<New_ViewName> sp_rename Viewpublisher, VpubDetails
To Get  Information on view There are some system stored procedures that help retrieve information on a view : sp_help :  This can be used to find information about view or any object  in the database. It returns properties such as created date,column names , foreign-key constraints etc. sp_depends:  This displays the information about the database object dependencies. Here it can be used to find the dependencies of the View. sp_helptext :  This is used to display the actual text that was used to create an object in the database. This information is read from syscomments table.
To Get  Information on view (Continued) sp_columns-  It returns information for the columns referred in specified views. Sysobjects (table)  - stores the complete details of the view. Syscolumns(table)  - stores the names of the columns defined in the view.
Key Points Views are virtual tables  Views provide security of data  Views can be created with the CREATE VIEW  statement A view can be modified with the ALTER View statement  A view can be dropped with the DROP View  statement Views conceal complexity of queries which contain complicated queries or sub queries Normally a complex view will contain joins Views can be created using “with check option”
Activity Time (30 minutes) Activity: 5.1 Create a view named ‘VwDetails’ that retrieves the Title_ID, Title_Name, Type and Price from Title table and Publisher_ID, Publisher_Name from Publisher table. Also display the information of the view created and its dependencies. Activity: 5.2 Alter the view created in the Activity 1 to add another column Publisher_Email and after altering the view, display the definition of the altered view Activity: 5.3 Rename the view created in the Activity 1 as VwTitleDetails and at last drop the view.
Activity Time (30 minutes) Activity: 5.4 Follow the Instructions below: 1) Create a view which retrieves the data from books and publishers table by using the following script. create view VwTitleDetails2 as select Title_ID, Title_Name,Type, publishers.publisher_name, from Title join Publisher on Title.Publisher_ID = Publisher. Publisher_ID 2) Now, Update the data using the view by using the following statement: Update VwTitleDetails2 set price=21 where Title_ID = 'B001' 3) Notice the output.
Questions & Comments

Module05

  • 1.
  • 2.
    Introduction to viewsAdvantages of views To Create Views To Alter Views To Drop Views To Rename Views View with Check Option How to get the information on view Contents
  • 3.
    Objectives At theend of this presentation you should be able to: Understand the concept of views Understand why views are required in a database Understand how to create and work with the views Understand how to get information about a view
  • 4.
    Introduction to ViewsView is a database object which contains query. A view is often referred to as a virtual table i.e.., a view looks like a table and is referred like a table. They do not contain data like tables. They can be used to join two tables in a database and present the underlying data as if the data was coming from a single table.
  • 5.
    Introduction to Views(Continued) View can be used as a security mechanism to restrict the data available to the end users Views can also be used to store and reuse the complex query which generates the reports. Views hides the underlying complex database structure.
  • 6.
    Advantages of ViewsA view provides following advantages: Focuses data for users. Hides data complexity. Simplifies permission management. Organizes data for export to other applications. Reduces object size.
  • 7.
    Example of aViews This example shows creating a view called ‘ViewPublishers’ from the Publishers table. This view contains two columns ‘Pubid’ and ‘Pubname’ which are taken from the table. The actual data will be in the Publishers table, which can be viewed and manipulated from the ‘ViewPublishers’ view. Publishers PARIS IL Chicago Five lake 1622 USA CA Berkely Algodata 1389 USA DC Washington Binnet 0877 CHICAGO MA Boston New Moon 0736 Country State City Pubname Pubid ViewPublishers Five lake 1389 Algodata 0877 Binnet 0736 Pubname Pubid
  • 8.
    To Create Views Syntax: Example to create view from publishers table Example to create a view from Publishers table for city “paris” CREATE VIEW <ViewName> AS <Select Statement> CREATE VIEW ViewPublishers As SELECT * FROM Publishers CREATE VIEW ViewPublishers AS SELECT pubid, pubname , city FROM Publishers WHERE city =‘paris’
  • 9.
    Simple views Simple View is also known as a normal view It contains data only from one table It won’t contain JOINS or GROUP By statements The underlying data from the base tables can be updated using an UPDATE statement. Example This example creates a simple view ViewPublisher which selects all the data from Publishers Table CREATE VIEW ViewPublisher AS SELECT * FROM Publishers
  • 10.
    Complex Views Itwill be accessed from multiple tables Normally a complex view will contain joins It contains group functions to display values from two tables. It cannot be updated using UPDATE Clause The example creates a complex view Dept_vw which selects the department name, minimum salary, maximum salary, and average salary. It uses three Aggregate functions and it joins two tables Employee and Department. CREATE VIEW Dept_vw (deptname,minSalary,maxSalary,avgSalary) AS SELECT d.deptName,min(e.salary),max(e.salary),avg(e.salary) FROM Employee e, Department d WHERE e.deptId = d.deptId GROUP BY d.deptName
  • 11.
    View With Checkoption With Check option specifies the level of checking to be done while inserting or modifying data through view If the view in read-only view Check option cannot be used It allows integrity constraints and data validation checks to be enforced on data begin Inserted or Updated CREATE VIEW empdept_vw as select empno,ename,deptno from emp where deptno =30 With check option; The example uses ‘With Check Option’ clause, which will restrict the insertion of a row that cannot be selected by the view
  • 12.
    To Alter ViewsView can be modified without dropping it. This ensures that the permissions on the view are not lost View can be modified without affecting it’s dependent objects such as triggers and procedures Alters the view to add publishers’ country column of the Publishers table ALTER VIEW ViewPublishers As SELECT pubid, pubname, pubcity, country From publishers
  • 13.
    To Drop ViewsView can be dropped from a database by using DROP VIEW When a view is dropped, it has no side effects on the underlying table (s). DROP VIEW <ViewName>
  • 14.
    To Rename ViewsView can be renamed without having to drop it. Only requirement is that views must be in current database A view can be renamed only by its owner It can also be renamed by the owner of the database sp_rename <Old_ViewName>,<New_ViewName> sp_rename Viewpublisher, VpubDetails
  • 15.
    To Get Information on view There are some system stored procedures that help retrieve information on a view : sp_help : This can be used to find information about view or any object in the database. It returns properties such as created date,column names , foreign-key constraints etc. sp_depends: This displays the information about the database object dependencies. Here it can be used to find the dependencies of the View. sp_helptext : This is used to display the actual text that was used to create an object in the database. This information is read from syscomments table.
  • 16.
    To Get Information on view (Continued) sp_columns- It returns information for the columns referred in specified views. Sysobjects (table) - stores the complete details of the view. Syscolumns(table) - stores the names of the columns defined in the view.
  • 17.
    Key Points Viewsare virtual tables Views provide security of data Views can be created with the CREATE VIEW statement A view can be modified with the ALTER View statement A view can be dropped with the DROP View statement Views conceal complexity of queries which contain complicated queries or sub queries Normally a complex view will contain joins Views can be created using “with check option”
  • 18.
    Activity Time (30minutes) Activity: 5.1 Create a view named ‘VwDetails’ that retrieves the Title_ID, Title_Name, Type and Price from Title table and Publisher_ID, Publisher_Name from Publisher table. Also display the information of the view created and its dependencies. Activity: 5.2 Alter the view created in the Activity 1 to add another column Publisher_Email and after altering the view, display the definition of the altered view Activity: 5.3 Rename the view created in the Activity 1 as VwTitleDetails and at last drop the view.
  • 19.
    Activity Time (30minutes) Activity: 5.4 Follow the Instructions below: 1) Create a view which retrieves the data from books and publishers table by using the following script. create view VwTitleDetails2 as select Title_ID, Title_Name,Type, publishers.publisher_name, from Title join Publisher on Title.Publisher_ID = Publisher. Publisher_ID 2) Now, Update the data using the view by using the following statement: Update VwTitleDetails2 set price=21 where Title_ID = 'B001' 3) Notice the output.
  • 20.

Editor's Notes

  • #2 Faculty Notes: Views are virtual tables (tables which don’t occupy memory) whose contents are taken or derived from other tables. Just like other table, a view consists of rows with columns, and you can retrieve data from a view. Views can be used to join two tables in database and present the underlying data as if the data were coming from a single table Views can be used as a security mechanism to restrict the data available to end users Views can also be used to aggregate data
  • #5 Faculty Notes: A VIEW is basically a table that only exists when you use the view in a query. It is considered a virtual table because it acts like a table in the sense that the operations that can be performed on a table can be performed on a view. This virtual table does not exist in the database. It gets created when we use the view and then deleted. The named VIEW is the only thing that persists.
  • #6 Faculty Notes: Views make your life easier by simplifying your complex queries into a simple command. You won’t have to create complicated joins and subqueries repeatedly when you create a view instead. Your view hides the complexity of the underlying query. You can create views that hide information that isn’t needed by the user. You can allow users to see just the information they need, while keeping the sensitive information hidden using a view.
  • #7 Faculty Notes: Focuses data for users : It can be used as security mechanism by allowing user to access data through view, without granting permission to directly access the base table. Hides data complexity : Views hide the complexity of the database design from the user. This enables developers to change database design without affecting user interaction with the database. Simplifies permission management : Instead of granting permission to users for querying specific columns of the base tables,database owners can grant them permission for querying data only through views. Organizes data for export to other applications : User can create view based on a complex query that joins two or more tables. This appears to the user as single table. Reduces object size: views do not contain data, server stores only the definition of the view in the database
  • #8 Faculty Notes: Instructor has to Use inbuilt database table Database: Pubs Tables Publishers
  • #10 Faculty Notes: The example creates a simple view ViewPublisher which selects all the data from Publisher Table.
  • #11 Faculty Notes: The syntax of Complex view is similar to Create View Complex view is a view which normally contains GROUP BY as well as joins. Data in the underlying tables cannot be updated using Complex views.
  • #12 Faculty Notes: The “With Check option” clause specifies that Inserts and Updates performed through the View are not allowed to create rows which the view cannot select, and therefore allows integrity constraints and data validation checks to be enforced on data begin Inserted or Updated. For instance, Through this view you would not be able to insert data where deptno is anything other that 30.
  • #13 Faculty Notes: If you need to change the view itself without losing on it’s name along with the permissions set for the view, you may choose to alter the view rather than deleting the existing view and creating a new view with the same name.
  • #16 Faculty notes: There are some system stored procedures that help retrieve information on views . sp_help: This system stored procedure displays view related information. sp_depends: Determines the dependencies by looking at the sys.sql_dependencies view. sp_helptext: Retrieves and displays the view definition.The object must be in the current database.
  • #17 Faculty Notes: sp_columns: This stored procedure is equivalent to SQLCOLUMS in ODBC.