59

What is a view in Oracle?

4

5 回答 5

129

A View in Oracle and in other database systems is simply the representation of a SQL statement that is stored in memory so that it can easily be re-used. For example, if we frequently issue the following query

SELECT customerid, customername FROM customers WHERE countryid='US';

To create a view use the CREATE VIEW command as seen in this example

CREATE VIEW view_uscustomers
AS
SELECT customerid, customername FROM customers WHERE countryid='US';

This command creates a new view called view_uscustomers. Note that this command does not result in anything being actually stored in the database at all except for a data dictionary entry that defines this view. This means that every time you query this view, Oracle has to go out and execute the view and query the database data. We can query the view like this:

SELECT * FROM view_uscustomers WHERE customerid BETWEEN 100 AND 200;

And Oracle will transform the query into this:

SELECT * 
FROM (select customerid, customername from customers WHERE countryid='US') 
WHERE customerid BETWEEN 100 AND 200

Benefits of using Views

  • Commonality of code being used. Since a view is based on one common set of SQL, this means that when it is called it’s less likely to require parsing.
  • Security. Views have long been used to hide the tables that actually contain the data you are querying. Also, views can be used to restrict the columns that a given user has access to.
  • Predicate pushing

You can find advanced topics in this article about "How to Create and Manage Views in Oracle."

于 2008-11-02T09:36:55.643 回答
12

如果您喜欢 Views 的想法,但担心性能,您可以让 Oracle 创建一个缓存表来表示 oracle 保持最新的视图。
查看物化视图

于 2008-11-03T11:32:43.243 回答
4

regular view----->short name for a query,no additional space is used here

Materialised view---->similar to creating table whose data will refresh periodically based on data query used for creating the view

于 2014-01-17T07:58:43.400 回答
2

视图是一个虚拟表,它提供对一个或多个表中列的子集的访问。视图可以从一个或多个表中获取其数据。查询的输出可以存储为视图。View 就像一张小桌子,但实际上并不占用任何空间。视图是通过直接访问表来呈现特定用户的数据的好方法。oracle 中的视图不过是存储的 sql 脚本。视图本身不包含数据。

于 2011-02-03T10:16:04.470 回答
2

视图只是任何SELECT已指定名称并保存在数据库中的查询。因此,视图有时称为命名查询或存储查询。要创建视图,请使用 SQL 语法:

     CREATE OR REPLACE VIEW <view_name> AS
     SELECT <any valid select query>;
于 2011-05-03T09:54:40.447 回答