0% found this document useful (0 votes)
8 views6 pages

SQL Notes

The document compares implicit and explicit cursors in SQL, highlighting that implicit cursors are automatically created and less efficient, while explicit cursors require user definition and offer more control and efficiency. It also explains the MERGE statement in Oracle, which combines INSERT and UPDATE operations, and details SQL sequences as user-defined objects for generating unique numeric values across multiple tables. Key features of SQL sequences include automatic primary key generation, flexibility in usage, and performance enhancements through cache management.

Uploaded by

shivani chougule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

SQL Notes

The document compares implicit and explicit cursors in SQL, highlighting that implicit cursors are automatically created and less efficient, while explicit cursors require user definition and offer more control and efficiency. It also explains the MERGE statement in Oracle, which combines INSERT and UPDATE operations, and details SQL sequences as user-defined objects for generating unique numeric values across multiple tables. Key features of SQL sequences include automatic primary key generation, flexibility in usage, and performance enhancements through cache management.

Uploaded by

shivani chougule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Implicit Cursors Explicit Cursors

Implicit cursors are automatically Explicit cursors needs to be defined


created when select statements are explicitly by the user by providing a
executed. name.

They are capable of fetching a


Explicit cursors can fetch multiple rows.
single row at a time.

Closes automatically after


Need to close after execution.
execution.

They are more vulnerable to errors They are less vulnerable to errors(Data
such as Data errors, etc. errors etc.)

Provides less programmatic control


User/Programmer has the entire control.
to the users

Comparative to Implicit cursors, explicit


Implicit cursors are less efficient.
cursors are more efficient.

Explicit cursors are defined as:


Implicit Cursors are defined as:
DECLARE
BEGIN
CURSOR cur_name IS
SELECT attr_name from
SELECT attr_name from table_name
table_name
where CONDITION;
where CONDITION;
BEGIN
END

Implicit cursors requires


Explicit cursors use user-defined memory
anonymous buffer memory for
space for storage purpose
storage purpose.

Cursor attributes use prefix “SQL”.


Structure for explicit cursors: cur_name
Structure for implicit cursors: SQL
%attr_name
%attr_name
Few explicit cursors are: cur_name
Few implicit cursors attributes are:
%FOUND, cur_name%NOTFOUND,
SQL%FOUND, SQL%NOTFOUND,
cur_name%ROWCOUNT
SQL%ROWCOUNT
The MERGE statement in Oracle combines the INSERT and UPDATE
operations into a single statement.

It compares the data in the Source Table (or staging table) with the
data in the Target Table (table to be updated). Then, based on
specified conditions, the MERGE statement either inserts new rows or
updates existing rows in the target table.

The basic syntax of the MERGE UPDATE command in Oracle:


MERGE INTO target_table a
USING source_table b
ON (condition)
WHEN MATCHED THEN
UPDATE SET a.column1 = b.value1, a.column2 =
b.value2, ...
WHEN NOT MATCHED THEN
INSERT (a.column1, a.column2, ...)
VALUES (b.value1, b.value2, ...)

/* CREATE A SEQUENCE HERE FOR SUPORTING THE INSERT CONDITION


CREATE SEQUENCE LEAD_SEQ;*/

MERGE INTO LEAD A


USING (SELECT ID_CUSTOMER, NAME, EMAIL FROM CUSTOMER) B
ON (B.ID_CUSTOMER = A.ID_CUSTOMER)
WHEN MATCHED THEN
UPDATE SET A.NAME = B.NAME, A.EMAIL = B.EMAIL
WHEN NOT MATCHED THEN
INSERT (A.ID_LEAD, A.NAME, A.EMAIL, A.ID_CUSTOMER)
VALUES (LEAD_SEQ.NEXTVAL, B.NAME, B.EMAIL,
B.ID_CUSTOMER);
What Are SQL Sequences?
SQL sequences are user-defined database objects designed to generate a
series of numeric values. Unlike identity columns, which are tightly bound to
specific tables, sequences are independent objects and can be used
across multiple tables. They allow applications to retrieve the next number in
a sequence whenever needed, offering a simple and efficient way to
generate unique numbers on demand.
The values in a sequence can be configured to be generated
in ascending or descending order, and the sequence can be set
to restart (cycle) once the maximum value is exceeded. This makes SQL
sequences particularly useful in scenarios where there is a need for
continuous, unique values, such as generating primary keys or serial
numbers.

Key Features of SQL Sequences


 Automatic Primary Key Generation: Sequences automatically generate
unique values that can be used for primary or unique keys in database
tables.
 Ascending or Descending Order: Sequences can be configured to
generate numbers in either ascending or descending order.
 Multiple Table Usage: A single sequence can be used to generate
values for multiple tables, making it flexible and reusable.
 Independent of Tables: Unlike identity columns, sequences are
independent and can be used across different tables.
 Efficient: Sequences reduce the complexity and overhead of manually
generating unique values, which saves time and reduces application
code.

How SQL Sequences Work


When creating a sequence, we specify the starting point,
the increment (how much the sequence increases with each step), and
optionally the minimum and maximum values. Sequences can be set to
cycle, which means they restart from the beginning when they reach
the maximum value.
Syntax:
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;
Example 1: Creating a Sequence in Ascending Order
CREATE SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;

Using SQL Sequences in Database Operations


Once a sequence is created, it can be used across multiple tables to
generate unique values, such as primary key identifiers or serial numbers.
This allows for consistent, efficient value generation, reducing the need
for manual input and ensuring uniqueness across different rows and
tables.
Example: Using a Sequence to Insert Values
Let’s create a students table and use the sequence to automatically generate
unique student IDs. In this example, the NEXTVAL function is used to retrieve
the next value in the sequence ( sequence_1) and insert it into the ID column
for each student.
Query:
CREATE TABLE students
(
ID number(10),
NAME char(20)
);

INSERT into students VALUES


(sequence_1.nextval,'Shubham');
INSERT into students VALUES
(sequence_1.nextval,'Aman');

Cache Management in SQL Sequences


To enhance performance, SQL Server employs cache management for
sequence numbers. The CACHE argument pre-allocates a set number of
sequence values in memory, which reduces disk access during normal
operations.
Example of Cache Management
For instance, if a sequence starts at 1 with a cache size of 15, SQL Server
will allocate values 1-15 in memory. When the 15th value is used, a new
cache with values 16-30 is allocated.
In the event of a server restart, the next sequence number will be the first
unused value in the cache. This approach ensures that sequence numbers
are generated quickly but may cause gaps in the sequence after a server
crash, which is normal.
If we want to avoid gaps, we can set the sequence to NOCACHE, though this
might reduce performance as it requires constant disk access.
Creating a Sequence with Cache
CREATE SEQUENCE sequence_3
START WITH 1
INCREMENT BY 1
CACHE 10;

Practical Use Cases for SQL Sequences


1. Primary Key Generation
Sequences are commonly used to generate unique primary key values for
database tables. This is especially useful in applications where a large
volume of records needs to be inserted into a table, and each record
requires a unique identifier.
2. Serial Numbers and Order IDs
SQL sequences can be used to generate serial numbers for products or
order IDs in e-commerce systems, ensuring that each order has a unique
identifier.
3. Auditing and Tracking
Sequences are also useful for tracking events or transactions that require
unique identifiers, such as logging system activities or generating unique
reference numbers for transactions.
Advantages of Using SQL Sequences
 Efficiency: Sequences simplify the process of generating unique values
without requiring complex application logic.
 Independence: Sequences can be used across multiple tables, ensuring
that unique values are consistently generated without tying them to a
specific table.
 Performance: By using the CACHE feature, sequences can enhance
performance by reducing disk access during value generation.
 Flexibility: SQL sequences can be configured to generate values in
ascending or descending order and can be set to cycle when reaching the
maximum value.

You might also like