📜  SQL中的视图和游标之间的区别

📅  最后修改于: 2021-07-05 06:13:52             🧑  作者: Mango

1.查看:
视图是虚拟表,实际上并不存在于数据库中,但可以根据特定用户的请求生成。视图是一个对象,它为用户提供来自基本表的数据的逻辑视图,我们可以通过允许他们查看表中唯一必要的列并隐藏其他数据库详细信息,来限制用户可以查看的内容。 View还允许用户根据他们的需求访问数据,因此不同的用户可以根据他们的需求以不同的方式访问相同的数据。

2.游标:
游标是在内存中创建的临时工作区,用于在执行时处理和存储与SQL语句有关的信息。临时工作区用于存储从数据库中检索到的数据,并根据需要处理数据。它包含有关select语句访问数据的所有必要信息。它可以保存一组称为活动集的行,但一次只能访问一行。有两种不同类型的游标–
1.隐式游标
2.显式游标

SQL中的View和Cursor之间的区别:

S.No. View Cursor
1. A view is a virtual table that gives logical view of data from base table. A cursor is a temporary workstation create in database sever when SQL statement is executed.
2. Views are dynamic in nature which means any changes made in base table are immediately reflected in view. Cursor can be static as well as dynamic in nature.
3. We can perform CRUD operations on view like create, insert, delete and update. There are some steps for creating a Explicit cursor –
  • Declare the cursor in declaration section.
  • Open the cursor in execution section.
  • Fetch the cursor to retrieve data into PL/SQL variable.
  • Close the cursor to release allocated memory.
4. There are two types of view i.e. Simple View (created from single table) and Complex Cursor (created from multiple tables). Cursor has two types i.e. Implicit Cursor (pre-defined) and Explicit Cursor(user defined).
5. View is a database object similar to table so it can be used with both SQL and PL/SQL. Cursor is defined and used within the block of stored procedure which means it can be only used with PL/SQL.
6. General Syntax of Creating View :
CREATE VIEW “VIEW_NAME” AS “SQL Statement”;
General Syntax of Creating View :
CURSOR cursor_name IS select_statement;