Difficulty: Easy
Correct Answer: A sequence is a schema object that generates unique numeric values, often used for surrogate keys, created with syntax like CREATE SEQUENCE seq_name START WITH n INCREMENT BY m and optionally other options.
Explanation:
Introduction / Context:
Sequences are widely used in Oracle databases to generate unique numeric values, most commonly as primary keys for rows. They provide a flexible and efficient way to assign numbers without locking the table. This question asks for a definition of sequences, their purpose, and the basic syntax for creating one.
Given Data / Assumptions:
Concept / Approach:
An Oracle sequence is a database object that generates a series of numeric values according to defined rules. It is independent of any particular table, so the same sequence can be used for multiple tables if required. Sequences can increment or decrement, cycle or not cycle, and use caching for performance. Common attributes in the CREATE SEQUENCE statement include START WITH for the initial value, INCREMENT BY for step size, MINVALUE, MAXVALUE, CACHE, NOCACHE, CYCLE, and NOCYCLE.
Step-by-Step Solution:
Step 1: Define the purpose of a sequence as generating unique, typically increasing numbers that can serve as primary keys or other identifiers.Step 2: Describe basic syntax such as CREATE SEQUENCE customer_seq START WITH 1 INCREMENT BY 1; which starts at one and increments by one for each new value.Step 3: Explain that you can retrieve values using sequence_name.NEXTVAL in SQL statements, and view the current value using sequence_name.CURRVAL after the first NEXTVAL in a session.Step 4: Note that sequences are designed to be highly concurrent, allowing many sessions to obtain new values without serialization on the table.Step 5: Summarise that option A correctly states that a sequence is a schema object for unique number generation and shows the basic CREATE SEQUENCE syntax with START WITH and INCREMENT BY options.
Verification / Alternative check:
To verify, create a simple sequence and then run SELECT sequence_name.NEXTVAL FROM dual; multiple times, observing that the returned values follow the configured pattern. Use the sequence in INSERT statements for primary keys and confirm that each row receives a distinct identifier even under concurrent inserts, demonstrating the purpose and correct behaviour of sequences.
Why Other Options Are Wrong:
Option B treats a sequence as a text file and not a numeric generator, which is incorrect. Option C confuses sequences with PL/SQL record types. Option D mistakenly describes a sequence as an index that rebuilds itself, ignoring its real function of generating numbers.
Common Pitfalls:
Developers sometimes expect sequences to produce perfectly gap free series, which may not hold if transactions roll back after using NEXTVAL. Another pitfall is misunderstanding caching, which can cause gaps after instance restarts. Good practice is to treat sequences as generators of unique values rather than as accounting tools requiring continuous sequences without gaps.
Final Answer:
A sequence is a schema object that generates unique numeric values, often used for surrogate keys, created with syntax like CREATE SEQUENCE seq_name START WITH n INCREMENT BY m and optionally other options.
Discussion & Comments