0
0
PostgreSQLquery~5 mins

Cursor declaration and usage in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a cursor in PostgreSQL?
A cursor is a database object used to retrieve a few rows from a query result at a time. It allows you to process query results row by row instead of all at once.
Click to reveal answer
beginner
How do you declare a cursor in PostgreSQL?
You declare a cursor using the syntax: <br>DECLARE cursor_name CURSOR FOR SELECT_statement;<br>This prepares the cursor to fetch rows from the SELECT query.
Click to reveal answer
beginner
What command is used to fetch rows from a cursor?
The FETCH command is used to retrieve rows from a cursor. For example: FETCH NEXT FROM cursor_name; gets the next row.
Click to reveal answer
intermediate
Why should you close a cursor after use?
Closing a cursor releases the memory and resources it uses. Use CLOSE cursor_name; to close it when done.
Click to reveal answer
intermediate
Explain the typical steps to use a cursor in PostgreSQL.
1. Declare the cursor with a SELECT query.<br>2. Open the cursor (optional in PostgreSQL, DECLARE opens it).<br>3. Fetch rows one by one or in blocks.<br>4. Close the cursor to free resources.
Click to reveal answer
Which SQL command declares a cursor in PostgreSQL?
ADECLARE cursor_name CURSOR FOR SELECT * FROM table;
BOPEN cursor_name;
CFETCH cursor_name;
DCLOSE cursor_name;
What does the FETCH command do?
ARetrieves rows from the cursor
BCloses the cursor
CDeclares a new cursor
DOpens the cursor
Why is it important to CLOSE a cursor?
ATo start fetching rows
BTo declare the cursor
CTo release resources used by the cursor
DTo open the cursor
Which of these is NOT a typical step when using a cursor?
ADeclare the cursor
BInsert rows into the cursor
CFetch rows
DClose the cursor
In PostgreSQL, when is a cursor opened?
AWhen you fetch from it
BWhen you commit a transaction
CWhen you close it
DWhen you declare it
Describe how to declare, fetch from, and close a cursor in PostgreSQL.
Think about the three main commands: DECLARE, FETCH, CLOSE.
You got /3 concepts.
    Explain why cursors are useful when working with large query results.
    Consider memory and performance when handling big data.
    You got /3 concepts.