Instructor: Dr. Mohsin Dar
Position: Assistant Professor, Cloud & Software Operations Cluster (SOCS)
Institution: UPES
Course: Database Systems - MTech First Semester
A sequence is a database object that generates a sequence of numbers in ascending or descending order. It's commonly used to generate unique identifiers for primary key columns in database tables.
Let's create a sequence named EMPID_SEQ that starts at 100 and increments by 1.
CREATE SEQUENCE empid_seq START WITH 100 INCREMENT BY 1;
To verify that the sequence was created successfully:
SELECT sequence_name, min_value, max_value, increment_by, last_number, cache_size FROM user_sequences WHERE sequence_name = 'EMPID_SEQ';
To check the current and next values of a sequence, you can use the following commands:
-- Get the next value without consuming it SELECT empid_seq.NEXTVAL FROM dual; -- Get the current value (without incrementing) SELECT empid_seq.CURRVAL FROM dual; -- View sequence details SELECT sequence_name, last_number, cache_size, increment_by, min_value, max_value FROM user_sequences WHERE sequence_name = 'EMPID_SEQ';
-- Get the next value SELECT nextval('empid_seq'); -- Get the current value SELECT currval('empid_seq'); -- View sequence details SELECT * FROM information_schema.sequences WHERE sequence_name = 'empid_seq';
Let's modify the EMPID_SEQ to change its cache value to 20 and set the maximum value to 1000.
ALTER SEQUENCE empid_seq CACHE 20 MAXVALUE 1000;
Let's verify that our changes were applied successfully:
SELECT sequence_name, cache_size, max_value, last_number, increment_by FROM user_sequences WHERE sequence_name = 'EMPID_SEQ';
-- Change increment value ALTER SEQUENCE empid_seq INCREMENT BY 5; -- Enable/disable cycling ALTER SEQUENCE empid_seq CYCLE; -- Start over after reaching max ALTER SEQUENCE empid_seq NOCYCLE; -- Default, stops at max -- Restart sequence (Oracle 12c+) ALTER SEQUENCE empid_seq RESTART START WITH 1;
Let's create an EMPLOYEES table that uses our sequence for the employee_id column.
CREATE TABLE employees ( employee_id NUMBER PRIMARY KEY, first_name VARCHAR2(50), last_name VARCHAR2(50), email VARCHAR2(100), hire_date DATE DEFAULT SYSDATE );
Now let's insert some records using our sequence:
-- Insert a single record INSERT INTO employees (employee_id, first_name, last_name, email) VALUES (empid_seq.NEXTVAL, 'John', 'Doe', 'john.doe@example.com'); -- Insert multiple records INSERT ALL INTO employees (employee_id, first_name, last_name, email) VALUES (empid_seq.NEXTVAL, 'Jane', 'Smith', 'jane.smith@example.com') INTO employees (employee_id, first_name, last_name, email) VALUES (empid_seq.NEXTVAL, 'Robert', 'Johnson', 'robert.j@example.com') SELECT 1 FROM dual;
In Oracle 12c and later, you can set a sequence as the default value for a column:
CREATE TABLE employees ( employee_id NUMBER DEFAULT empid_seq.NEXTVAL PRIMARY KEY, first_name VARCHAR2(50), last_name VARCHAR2(50), email VARCHAR2(100), hire_date DATE DEFAULT SYSDATE ); -- Now you can insert without specifying employee_id INSERT INTO employees (first_name, last_name, email) VALUES ('Alice', 'Williams', 'alice.w@example.com');
Sequence values may have gaps in the following scenarios:
This is normal behavior and generally not a problem for most applications.
Let's drop the EMPID_SEQ sequence we created earlier:
DROP SEQUENCE empid_seq;
Now, let's create a new sequence called REVERSE that counts down from 10000 to 1000 in steps of 5:
CREATE SEQUENCE reverse_seq START WITH 10000 INCREMENT BY -5 MINVALUE 1000 MAXVALUE 10000 NOCYCLE; -- Stop after reaching 1000
Let's generate some values from our reverse sequence:
-- Generate 5 values from the reverse sequence SELECT reverse_seq.NEXTVAL AS sequence_value FROM dual CONNECT BY LEVEL <= 5;
To view all sequences in your schema and their properties:
SELECT sequence_name, min_value, max_value, increment_by, cycle_flag, cache_size, last_number FROM user_sequences ORDER BY sequence_name;