Difference between View and Cursor in SQL

1. View :
A view is a virtual table that not actually exist in the database but it can be produced upon request by a particular user. A view is an object that gives the user a logical view of data from a base table we can restrict to what user can view by allowing them to see an only necessary column from the table and hide the other database details. View also permits users to access data according to their requirements, so the same data can be access by a different user in a different way according to their needs.

2. Cursor :
A cursor is a temporary work area created in memory for processing and storing the information related to an SQL statement when it is executed. The temporary work area is used to store the data retrieved from the database and manipulate data according to need. It contains all the necessary information on data access by the select statement. It can hold a set of rows called active set but can access only a single row at a time. There are two different types of cursors –
1. Implicit Cursor
2. Explicit Cursor

Difference between View and Cursor in SQL :

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;

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.