A view is, in essence, a virtual table. It does not physically exist.
Rather, it is created by a query joining one or more
tables. Creating a VIEW The syntax for creating a VIEW is: CREATE VIEW view_name AS SELECT columns FROM table WHERE predicates;
For example: CREATE VIEW sup_orders AS SELECT suppliers.supplier_id, orders.quantity, orders.price FROM suppliers, orders WHERE suppliers.supplier_id = orders.supplier_id and suppliers.supplier_name = 'IBM'; This would create a virtual table based on the result set of the select statement. You can now query the view as follows: SELECT * FROM sup_orders;
Updating a VIEW You can update a VIEW without dropping it by using the following syntax: CREATE OR REPLACE VIEW view_name AS SELECT columns FROM table WHERE predicates;
For example: CREATE or REPLACE VIEW sup_orders AS SELECT suppliers.supplier_id, orders.quantity, orders.price FROM suppliers, orders WHERE suppliers.supplier_id = orders.supplier_id and suppliers.supplier_name = 'Microsoft';
Dropping a VIEW The syntax for dropping a VIEW is: DROP VIEW view_name; For example: DROP VIEW sup_orders;
Frequently Asked Questions
Question: Can you update the data in a view? Answer: A view is created by joining one or more tables. When you update record(s) in a view, it updates the records in the underlying tables that make up the view. So, yes, you can update the data in a view providing you have the proper privileges to the underlying tables.
Question: Does the view exist if the table is dropped from the database? Answer: Yes, in Oracle, the view continues to exist even after one of the tables (that the view is based on) is dropped from the database. However, if you try to query the view after the table has been dropped, you will receive a message indicating that the view has errors. If you recreate the table (that you had dropped), the view will again be fine.
ANOTHER WEB PAGE Views in SQL Server By : Steve Manik May 17, 2004
View A view is a virtual table that consists of columns from one or more tables. Though it is similar to a table, it is stored in the database. It is a query stored as an object. Hence, a view is an object that derives its data from one or more tables. These tables are referred to as base or underlying tables. Once you have defined a view, you can reference it like any other table in a database. A view serves as a security mechanism. This ensures that users are able to retrieve and modify only the data seen by them. Users cannot see or access the remaining data in the underlying tables. A view also serves as a mechanism to simplify query execution. Complex queries can be stored in the form as a view, and data from the view can be extracted using simple queries. Example Consider the Publishers table below. If you want users to see only two columns in the table, you can create a view called vwPublishers that will refer to the Publishers table and the two columns required. You can grant Permissions to users to use the view and revoke Permissions from the base Publishers table. This way, users will be able to view only the two columns referred to by the view. They will not be able to query on the Publishers table. Publishers Publd PubName City State Country 0736 New Moon Books Boston MA USA 0877 Binnet & Hardly Washington DC USA 1389 Algodata Infosystems Berkeley CA USA 1622 Five Lakes Publishing Chicago IL USA VW Publishers Publd PubName 0736 New Moon Books 0877 Binnet & Hardly 1389 Algodata Infosystems 1622 Five Lakes Publishing Views ensure the security of data by restricting access to the following data: Specific rows of the tables. Specific columns of the tables. Specific rows and columns of the tables. Rows fetched by using joins. Statistical summary of data in a given tables. Subsets of another view or a subset of views and tables. Some common examples of views are: A subset of rows or columns of a base table. A union of two or more tables. A join of two or more tables. A statistical summary of base tables. A subset of another view, or some combination of views and base table.
Creating Views A view can be created by using the CREATE VIEW statement. Syntax CREATE VIEW view_name [(column_name[,column_name].)] [WITH ENCRYPTION] AS select_statement [WITH CHECK OPTION] Where: view_name specifies the name of the view and must follow the rules for identifiers. column_name specifies the name of the column to be used in view. If the column_name option is not specified, then the view is created with the same columns as specified in the select_statement. WITH ENCRYPTION encrypts the text for the view in the syscomments table. AS specifies the actions that will be performed by the view. select_statement specifies the SELECT Statement that defines a view. The view may use the data contained in other views and tables. WITH CHECK OPTION forces the data modification statements to fulfill the criteria given in the SELECT statement defining the view. It also ensures that the data is visible after the modifications are made permanent. The restrictions imposed on views are as follows: A view can be created only in the current database. The name of a view must follow the rules for identifiers and must not be the same as that of the base table. A view can be created only if there is a SELECT permission on its base table. A SELECT INTO statement cannot be used in view declaration statement. A trigger or an index cannot be defined on a view. The CREATE VIEW statement cannot be combined with other SQL statements in a single batch. Example CREATE VIEW vwCustomer AS SELECT CustomerId, Company Name, Phone FROM Customers Creates a view called vwCustomer. Note that the view is a query stored as an object. The data is derived from the columns of the base table Customers. You use the view by querying the view like a table. SELECT *FROM vwCUSTOMER The output of the SELECT statement is: CustomerId Company Name Phone ALFKI Alfreds Futterkiste 030-0074321 ANTON Antonio Moreno Taqueria (5)555-3932 (91 rows affected)
ANOTHER WEB PAGE
SQL Server Architecture (SQL Server 2000) SQL Views A view can be thought of as either a virtual table or a stored query. The data accessible through a view is not stored in the database as a distinct object. What is stored in the database is a SELECT statement. The result set of the SELECT statement forms the virtual table returned by the view. A user can use this virtual table by referencing the view name in Transact-SQL statements the same way a table is referenced. A view is used to do any or all of these functions: Restrict a user to specific rows in a table. For example, allow an employee to see only the rows recording his or her work in a labor-tracking table. Restrict a user to specific columns. For example, allow employees who do not work in payroll to see the name, office, work phone, and department columns in an employee table, but do not allow them to see any columns with salary information or personal information. Join columns from multiple tables so that they look like a single table. Aggregate information instead of supplying details. For example, present the sum of a column, or the maximum or minimum value from a column. Views are created by defining the SELECT statement that retrieves the data to be presented by the view. The data tables referenced by the SELECT statement are known as the base tables for the view. In this example, titleview in the pubsdatabase is a view that selects data from three base tables to present a virtual table of commonly needed data: CREATE VIEW titleview AS SELECT title, au_ord, au_lname, price, ytd_sales, pub_id FROM authors AS a JOIN titleauthor AS ta ON (a.au_id = ta.au_id) JOIN titles AS t ON (t.title_id = ta.title_id) You can then reference titleview in statements in the same way you would reference a table: SELECT * FROM titleview A view can reference another view. For example, titleview presents information that is useful for managers, but a company typically discloses year-to-date figures only in quarterly or annual financial statements. A view can be built that selects all the titleview columns except au_ord and ytd_sales. This new view can be used by customers to get lists of available books without seeing the financial information: CREATE VIEW Cust_titleview AS SELECT title, au_lname, price, pub_id FROM titleview Views can be used to partition data across multiple databases or instances of Microsoft SQL Server 2000. Partitioned views can be used to distribute database processing across a group of servers. The group of servers has the same performance benefits as a cluster of servers, and can be used to support the processing needs of the largest Web sites or corporate data centers. An original table is subdivided into several member tables, each of which has a subset of the rows from the original table. Each member table can be placed in databases on separate servers. Each server also gets a partitioned view. The partitioned view uses the Transact-SQL UNION operator to combine the results of selects against all the member tables into a single result set that behaves exactly like a copy of the full original table. For example, a table is partitioned across three servers. On the first server you define a partitioned view similar to this: CREATE VIEW PartitionedView AS SELECT * FROM MyDatabase.dbo.PartitionTable1 UNION ALL SELECT * FROM Server2.MyDatabase.dbo.PartitionTable2 UNION ALL SELECT * FROM Server3.MyDatabase.dbo.PartitionTable3 You define similar partitioned views on each of the other servers. With these three views, any Transact-SQL statements on any of the three servers that reference PartitionedView will see the same behavior as from the original table. It is as if a copy of the original table exists on each server, when in fact there is only one member table and a partitioned view on each table. For more information, see Scenarios for Using Views. Views in all versions of SQL Server are updatable (can be the target of UPDATE, DELETE, or INSERT statements), as long as the modification affects only one of the base tables referenced by the view, for example: -- Increase the prices for publisher '0736' by 10%. UPDATE titleview SET price = price * 1.10 WHERE pub_id = '0736' GO SQL Server 2000 supports more complex types of INSERT, UPDATE, and DELETE statements that reference views. INSTEAD OF triggers can be defined on a view to specify the individual updates that must be performed against the base tables to support the INSERT, UPDATE, or DELETE statement. Also, partitioned views support INSERT, UPDATE, and DELETE statements that modify multiple member tables referenced by the view. Indexed views are a SQL Server 2000 feature that greatly improves the performance of complex views of the type usually found in data warehouses or other decision support systems. Views are called virtual tables because the result set of a view is us not usually saved in the database The result set for a view is dynamically incorporated into the logic of the statement and the result set is built dynamically at run time. For more information, see View Resolution. Complex queries, such as those in decision support systems, can reference large numbers of rows in base tables, and aggregate large amounts of information into relatively concise aggregates such as sums or averages. SQL Server 2000 supports creating a clustered index on a view that implements such a complex query. When the CREATE INDEX statement is executed the result set of the view SELECT is stored permanently in the database. Future SQL statements that reference the view will have substantially better response times. Modifications to the base data are automatically reflected in the view. The SQL Server 2000 CREATE VIEW statement supports a SCHEMABINDING option that prevents the tables referenced by the view being changed without adjusting the view. You must specify SCHEMABINDING for any view on which you create an index.