Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Published in: Education
  • Be the first to like this


  1. 1. Introduction to Views Module 5
  2. 2. <ul><li>Introduction to views </li></ul><ul><li>Advantages of views </li></ul><ul><li>To Create Views </li></ul><ul><li>To Alter Views </li></ul><ul><li>To Drop Views </li></ul><ul><li>To Rename Views </li></ul><ul><li>View with Check Option </li></ul><ul><li>How to get the information on view </li></ul>Contents
  3. 3. Objectives <ul><li>At the end of this presentation you should be able to: </li></ul><ul><li>Understand the concept of views </li></ul><ul><li>Understand why views are required in a database </li></ul><ul><li>Understand how to create and work with the views </li></ul><ul><li>Understand how to get information about a view </li></ul>
  4. 4. Introduction to Views <ul><li>View is a database object which contains query. </li></ul><ul><li>A view is often referred to as a virtual table i.e.., a view looks like a table and is referred like a table. </li></ul><ul><li>They do not contain data like tables. </li></ul><ul><li>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. </li></ul>
  5. 5. Introduction to Views (Continued) <ul><li>View can be used as a security mechanism to restrict the data available to the end users </li></ul><ul><li>Views can also be used to store and reuse the complex query which generates the reports. </li></ul><ul><li>Views hides the underlying complex database structure. </li></ul>
  6. 6. Advantages of Views <ul><li>A view provides following advantages: </li></ul><ul><li>Focuses data for users. </li></ul><ul><li>Hides data complexity. </li></ul><ul><li>Simplifies permission management. </li></ul><ul><li>Organizes data for export to other applications. </li></ul><ul><li>Reduces object size. </li></ul>
  7. 7. 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
  8. 8. To Create Views <ul><li>Syntax: </li></ul><ul><li>Example to create view from publishers table </li></ul><ul><li>Example to create a view from Publishers table for city “paris” </li></ul>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. 9. Simple views <ul><li>Simple View is also known as a normal view </li></ul><ul><li>It contains data only from one table </li></ul><ul><li>It won’t contain JOINS or GROUP By statements </li></ul><ul><li>The underlying data from the base tables can be updated using an UPDATE statement. </li></ul><ul><li>Example </li></ul>This example creates a simple view ViewPublisher which selects all the data from Publishers Table CREATE VIEW ViewPublisher AS SELECT * FROM Publishers
  10. 10. Complex Views <ul><li>It will be accessed from multiple tables </li></ul><ul><li>Normally a complex view will contain joins </li></ul><ul><li>It contains group functions to display values from two tables. </li></ul><ul><li>It cannot be updated using UPDATE Clause </li></ul>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. 11. View With Check option <ul><li>With Check option specifies the level of checking to be done while inserting or modifying data through view </li></ul><ul><li>If the view in read-only view Check option cannot be used </li></ul><ul><li>It allows integrity constraints and data validation checks to be enforced on data begin Inserted or Updated </li></ul>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. 12. To Alter Views <ul><li>View can be modified without dropping it. </li></ul><ul><li>This ensures that the permissions on the view are not lost </li></ul><ul><li>View can be modified without affecting it’s dependent objects such as triggers and procedures </li></ul>Alters the view to add publishers’ country column of the Publishers table ALTER VIEW ViewPublishers As SELECT pubid, pubname, pubcity, country From publishers
  13. 13. To Drop Views <ul><li>View can be dropped from a database by using DROP VIEW </li></ul><ul><li>When a view is dropped, it has no side effects on the underlying table (s). </li></ul>DROP VIEW <ViewName>
  14. 14. To Rename Views <ul><li>View can be renamed without having to drop it. </li></ul><ul><li>Only requirement is that views must be in current database </li></ul><ul><li>A view can be renamed only by its owner </li></ul><ul><li>It can also be renamed by the owner of the database </li></ul>sp_rename <Old_ViewName>,<New_ViewName> sp_rename Viewpublisher, VpubDetails
  15. 15. To Get Information on view <ul><li>There are some system stored procedures that help retrieve information on a view : </li></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>sp_depends: This displays the information about the database object dependencies. Here it can be used to find the dependencies of the View. </li></ul></ul><ul><ul><li>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. </li></ul></ul>
  16. 16. To Get Information on view (Continued) <ul><ul><li>sp_columns- It returns information for the columns referred in specified views. </li></ul></ul><ul><ul><ul><li>Sysobjects (table) - stores the complete details of the view. </li></ul></ul></ul><ul><ul><ul><li>Syscolumns(table) - stores the names of the columns defined in the view. </li></ul></ul></ul>
  17. 17. Key Points <ul><li>Views are virtual tables </li></ul><ul><li>Views provide security of data </li></ul><ul><li>Views can be created with the CREATE VIEW statement </li></ul><ul><li>A view can be modified with the ALTER View statement </li></ul><ul><li>A view can be dropped with the DROP View statement </li></ul><ul><li>Views conceal complexity of queries which contain complicated queries or sub queries </li></ul><ul><li>Normally a complex view will contain joins </li></ul><ul><li>Views can be created using “with check option” </li></ul>
  18. 18. Activity Time (30 minutes) <ul><li>Activity: 5.1 </li></ul><ul><li>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. </li></ul><ul><li>Activity: 5.2 </li></ul><ul><li>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 </li></ul><ul><li>Activity: 5.3 </li></ul><ul><li>Rename the view created in the Activity 1 as VwTitleDetails and at last drop the view. </li></ul>
  19. 19. Activity Time (30 minutes) <ul><li>Activity: 5.4 </li></ul><ul><li>Follow the Instructions below: </li></ul><ul><li>1) Create a view which retrieves the data from books and publishers table by using the following script. </li></ul><ul><li>create view VwTitleDetails2 </li></ul><ul><li>as </li></ul><ul><li>select Title_ID, Title_Name,Type, publishers.publisher_name, from Title join Publisher on Title.Publisher_ID = Publisher. Publisher_ID </li></ul><ul><li>2) Now, Update the data using the view by using the following statement: </li></ul><ul><li>Update VwTitleDetails2 </li></ul><ul><li>set price=21 where Title_ID = 'B001' </li></ul><ul><li>3) Notice the output. </li></ul>
  20. 20. Questions & Comments